如何在.NET 4.5中将zip文件内容提取到文件夹中
问题描述:
以下问题的答案似乎概述了如何使用System.IO.Commpression.ZipFile.ExtractToDirectory方法调用来提取文件。在添加对System.IO.Compression的引用时,“.NETFile”似乎不存在于.NET 4.5中。我如何从.NET 4.5中的* .zip文件中提取文件?如何在.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;
}
答
您将需要添加对System.IO.Compression.FileSystem
组件的引用。
每个库类都有一个MSDN页面。这是the one for ZipFile。
请注意名称空间和程序集顶部的规范。
[ZipeFile.ExtractToDirectory MSDN applicable to .net 4.5](https://msdn.microsoft.com/en-us/library/hh485723(v = vs.110).aspx)问题和/或问题是什么。 ? – MethodMan
我想你需要一个参考'System.IO.Compression.FileSystem' – petelids
'using System;使用System.IO的 ; 使用System.IO.Compression;' – MethodMan