【发布时间】:2016-04-18 19:43:20
【问题描述】:
以下问题的答案似乎概述了如何使用 System.IO.Commpression.ZipFile.ExtractToDirectory 方法调用来提取文件。添加对 System.IO.Compression 的引用时,.NET 4.5 中似乎不存在“ZipFile”。如何从 .NET 4.5 中的 *.zip 文件中提取文件?
How to Unzip all .Zip file from Folder using C# 4.0 and without using any OpenSource Dll?
这似乎显示了如何压缩文件。但我正在寻找相反的结果。
即使这个问题在源代码中也引用了“ZipFile”。但是我好像找不到这个类。
How to extract just the specific directory from a zip archive in C# .NET 4.5?
编辑:
请注意 7z.exe(来自 7zip 包)如何不起作用。 .NET 和 7zip 肯定有冲突。 ZipFile 现在似乎可以正常工作了。
private void extract_Click(object sender, EventArgs e)
{
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
exePath = @"C:\test"; // path during troubleshooting
////var cmd1 = "cd \"" + exePath + "\"";
////ExecuteCommand(cmd1, 100, exePath);
//var cmd2 = "\"" + exePath + "\\7z.exe\" x \"" + exePath + "\\source.zip\"";
//ExecuteCommand(cmd2, 100, exePath);
string zipPath = exePath + "\\source.zip";
string extractPath = exePath;
// needed explicit reference to System.IO.Compression.FileSystem
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
private static int ExecuteCommand(string command, int timeout, string dir)
{
var processInfo = new ProcessStartInfo("cmd.exe", " /C " + command)
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = dir,
};
var process = Process.Start(processInfo);
process.WaitForExit(timeout);
var exitCode = process.ExitCode;
process.Close();
return exitCode;
}
【问题讨论】:
-
ZipeFile.ExtractToDirectory MSDN applies to .net 4.5 有什么问题和/或问题..?
-
我认为你需要参考
System.IO.Compression.FileSystem -
using System; using System.IO; using System.IO.Compression; -
petelids - 文件系统获得红色波浪标记。我的错误是“当前上下文中不存在名称‘ZipFile’”。我正在使用 Visual Studio 2013 Community Edition,我的项目类型是使用 .NET 4.5 的 Windows 窗体应用程序。 MethodMan - 适当注意;仍然收到错误
-
@MacGyver - 是的,
ZipFile类在 namespace System.IO.Commpression 但它在 assembly System.IO 中。 Compression.FileSystem,因此您需要引用 System.IO.Compression.FileSystem.dll 才能使用该类
标签: c# zip .net-4.5 7zip compression