【问题标题】:I didn't find "ZipFile" class in the "System.IO.Compression" namespace我没有在“System.IO.Compression”命名空间中找到“ZipFile”类
【发布时间】:2013-02-20 22:07:22
【问题描述】:

我不能在命名空间“System.IO.Compression”中使用“Zipfile”类,我的代码是:

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest,true);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

错误是:

当前上下文中不存在名称“zipfile”

我该如何解决?

【问题讨论】:

标签: c# zip


【解决方案1】:

您需要为此提供额外的参考;最方便的方法是通过 NuGet 包System.IO.Compression.ZipFile

<!-- Version here correct at time of writing, but please check for latest -->
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />

如果您在没有 NuGet 的情况下使用 .NET Framework,则需要添加对程序集“System.IO.Compression.FileSystem.dll”的 dll 引用 - 并确保您至少使用 .NET 4.5(因为它在早期的框架中不存在)。

有关信息,您可以找到程序集和 .NET 版本from MSDN

【讨论】:

  • 我正在使用 .NET 4.0 ...但我没有找到这个 dll 文件
  • @MohamedKamal 确实,除非您使用 .NET 4.5,否则您不会这样做,因为它直到那时才存在;请参阅“版本信息”信息 - “支持:4.5”
  • @MarcGravell 我遇到了同样的错误。我检查了 Visual Studio-->帮助-->关于 Microsoft Visual Studio,它使用 .NET 版本 4.5.51650 显示。接下来我应该检查什么?
  • 我没有FileSystem 参考。只有System.IO.Compression。谢谢。奇怪的是它需要一个不在类命名空间中的文件。这是一个奇怪的偏离惯例。
  • @ChrisBenard 我发现了同样的问题。包括对 System.IO.Compression 的引用是不够的,我还需要 FileSystem。确实很有趣。
【解决方案2】:

对于那些在 .NET 中的绿色程序员,要添加 DLL 引用为MarcGravell,请按照以下步骤操作:

在 Visual C# 中添加引用

  1. 在解决方案资源管理器中,右键单击项目节点,然后单击添加引用。
  2. 在“添加引用”对话框中,选择指示要引用的组件类型的选项卡。
  3. 选择要引用的组件,然后单击“确定”。

来自 MSDN 文章,How to: Add or Remove References By Using the Add Reference Dialog Box

【讨论】:

  • 感谢您的信息。我已经使用过 System.IO.Compression;但随后必须添加对使用 System.IO.Compression.FileSystem 的引用,如上所述,以使 ZipFile 在没有 OP 原始错误的情况下进行编译。
【解决方案3】:

如果您无法升级到 4.5,您可以使用外部软件包。 DotNetZipLib 中的 Ionic.Zip.dll 就是其中之一。

using Ionic.Zip;

你可以在这里下载它,它是免费的。 http://dotnetzip.codeplex.com/

【讨论】:

  • 或通过 NuGet 安装
  • 这与问题有什么关系?
  • 他的问题是“我该如何解决?”这是修复它的一种方法 - 因为他的 .net 版本不支持它。
  • 遗憾的是,升级有时不是一种选择。投赞成票。
  • 如果您正在向 2000 台安装了 XP 的 PC 写入数据并且您的程序无法进行任何安装(安装 .net 4.5 框架),这与此相关
【解决方案4】:

只需转到参考并添加“System.IO.Compression.FileSystem”。

【讨论】:

  • 我有无限的地方可以找到“参考”。你能提供更多关于在哪里可以找到它的提示吗?
【解决方案5】:

对我有帮助的解决方案: 转到工具 > NuGet 包管理器 > 管理 NuGet Packaged for Solution... > 浏览 > 搜索 System.IO.Compression.ZipFile 并安装它

【讨论】:

  • 简单而且有效... +1
【解决方案6】:

System.IO.Compression 现在以 nuget package 的形式提供,由 Microsoft 维护。

要使用ZipFile,您需要下载System.IO.Compression.ZipFilenuget package

【讨论】:

    【解决方案7】:

    我知道这是一个旧线程,但我无法避免发布一些有用的信息。我看到 Zip 问题出现了很多,这几乎回答了大多数常见问题。

    为了解决使用 4.5+ 的框架问题...他们是由 jaime-olivares 创建的 ZipStorer 类:https://github.com/jaime-olivares/zipstorer,他还添加了一个如何使用该类的示例,还添加了一个示例以及如何搜索特定文件名。

    作为参考如何使用它并迭代某个文件扩展名,你可以这样做:

    #region
    /// <summary>
    /// Custom Method - Check if 'string' has '.png' or '.PNG' extension.
    /// </summary>
    static bool HasPNGExtension(string filename)
    {
        return Path.GetExtension(filename).Equals(".png", StringComparison.InvariantCultureIgnoreCase)
            || Path.GetExtension(filename).Equals(".PNG", StringComparison.InvariantCultureIgnoreCase);
    }
    #endregion
    
    private void button1_Click(object sender, EventArgs e)
    {
        //NOTE: I recommend you add path checking first here, added the below as example ONLY.
        string ZIPfileLocationHere = @"C:\Users\Name\Desktop\test.zip";
        string EXTRACTIONLocationHere = @"C:\Users\Name\Desktop";
    
        //Opens existing zip file.
        ZipStorer zip = ZipStorer.Open(ZIPfileLocationHere, FileAccess.Read);
    
        //Read all directory contents.
        List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();
    
        foreach (ZipStorer.ZipFileEntry entry in dir)
        {
            try
            {
                //If the files in the zip are "*.png or *.PNG" extract them.
                string path = Path.Combine(EXTRACTIONLocationHere, (entry.FilenameInZip));
                if (HasPNGExtension(path))
                {
                    //Extract the file.
                    zip.ExtractFile(entry, path);
                }
            }
            catch (InvalidDataException)
            {
                MessageBox.Show("Error: The ZIP file is invalid or corrupted");
                continue;
            }
            catch
            {
                MessageBox.Show("Error: An unknown error ocurred while processing the ZIP file.");
                continue;
            }
        }
        zip.Close();
    }
    

    【讨论】:

      【解决方案8】:

      在解决方案资源管理器中,右键单击“引用”,然后单击以展开程序集,找到 System.IO.Compression.FileSystem 并确保已选中。然后你可以在你的课堂上使用它 - using System.IO.Compression;

      Add Reference Assembly Screenshot

      【讨论】:

        【解决方案9】:

        添加 System.IO.Compression.ZipFile 作为 nuget 引用它正在工作

        【讨论】:

          【解决方案10】:

          这里的问题是您刚刚添加了对 System.IO.Compression 的引用它缺少对 System.IO.Compression.Filesystem.dll 的引用

          您需要在 .net 4.5 或更高版本上执行此操作(因为旧版本不存在)。

          我刚刚在 TechNet 上发布了一个脚本,也许有人会觉得它很有用,它需要 .net 4.5 或 4.7

          https://gallery.technet.microsoft.com/scriptcenter/Create-a-Zip-file-from-a-b23a7530

          【讨论】:

            猜你喜欢
            • 2016-02-21
            • 1970-01-01
            • 2016-07-02
            • 2022-11-10
            • 2019-02-25
            • 1970-01-01
            • 2019-03-03
            相关资源
            最近更新 更多