如何将 zip 文件内容提取到 .NET 4.5 中的文件夹中

How to extract zip file contents into a folder in .NET 4.5

以下问题的答案似乎概述了如何使用 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"。但我似乎找不到这个 class.

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 程序集的引用。

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

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