【问题标题】:How to extract zip file contents into a folder in .NET 4.5如何将 zip 文件内容提取到 .NET 4.5 中的文件夹中
【发布时间】: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?

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

Zipping files in .NET 4.5

即使这个问题在源代码中也引用了“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


【解决方案1】:

您需要添加对System.IO.Compression.FileSystem 程序集的引用。

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

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

【讨论】:

  • 有时让我感到困惑的是如何判断一个库是否需要显式添加(作为 .NET 引用)以及何时是隐式的,这意味着“开箱即用”并且只需要一个简单的 using 声明...?
  • 这取决于您开始使用的模板。他们对您最有可能需要什么做出有根据的猜测。其余的这些“标准”程序集都没有什么特别之处。
  • 查看我的问题中的编辑。 7zip 最初不起作用。从 .NET 与从命令行启动进程时,.NET 必须与某些内容发生冲突。
  • 这是一个完全不同的问题。完成查找现有的步骤并在需要时发布新的。
  • Henk,我发现如果您的 C# 库通过在源代码中启动进程来从命令行调用命令,并且该命令 (exe) 是一个已编译的 C# 程序,它引用了一个 C++ 托管 DLL (使用 extern 关键字),.NET 不允许这样做。但是,如果第二个 C# 应用程序源代码的源代码在您的第一个 C# 应用程序的实际源代码中,则允许这样做。我现在有两个例子。我觉得很有趣。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
相关资源
最近更新 更多