如何在.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?

这似乎显示了如何压缩文件。但我正在寻找相反的东西。

Zipping files in .NET 4.5

即使这个问题引用 “的ZipFile” 的源代码。但我似乎无法找到这个班。

How to extract just the specific directory from a zip archive in C# .NET 4.5?

enter image description here

编辑:

通知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; 
} 
+0

[ZipeFile.ExtractToDirectory MSDN applicable to .net 4.5](https://msdn.microsoft.com/en-us/library/hh485723(v = vs.110).aspx)问题和/或问题是什么。 ? – MethodMan

+0

我想你需要一个参考'System.IO.Compression.FileSystem' – petelids

+0

'using System;使用System.IO的 ; 使用System.IO.Compression;' – MethodMan

您将需要添加对System.IO.Compression.FileSystem组件的引用。

每个库类都有一个MSDN页面。这是the one for ZipFile

请注意名称空间和程序集顶部的规范。

+0

有时候我困惑的是如何判断一个库是否需要显式添加(作为一个.NET引用),以及何时它是隐式的,意思是“开箱即用”,只需要一个简单的使用声明......? – MacGyver

+0

这取决于您从哪个模板开始。他们对你最需要的东西进行了有根据的猜测。对于其他人来说,这些'标准'组件没有什么特别之处。 –

+0

请参阅我的问题中的编辑。 7zip原本没有工作。 .NET从.NET启动进程或从命令行启动进程时,.NET必须与某些内容发生冲突。 – MacGyver