C# 我在“System.IO.Compression”命名空间中找不到“ZipFile”类

C# 我在“System.IO.Compression”命名空间中找不到“ZipFile”类,c#,zip,C#,Zip,我无法在名称空间System.IO中使用Zipfile类。压缩我的代码是: using System; using System.IO; using System.IO.Compression; namespace ConsoleApplication { class Program { static void Main(string[] args) { string startPath = @"c:\example\st

我无法在名称空间System.IO中使用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”


我如何解决这个问题?

你需要额外的参考资料;最方便的方法是通过NuGet包

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


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

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

using Ionic.Zip;

你可以在这里下载,免费的

对于.NET中的绿色程序员,要按说明添加DLL引用,请执行以下步骤:

在Visual C中添加引用的步骤

在解决方案资源管理器中,在项目节点上单击鼠标右键,然后单击“添加引用”。 在“添加参照”对话框中,选择指示要参照的零部件类型的选项卡。 选择要参照的零部件,然后单击“确定”。
从MSDN文章中,.

只需转到参考资料并添加System.IO.Compression.FileSystem。

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

要绕过使用4.5+的框架问题。。。他们是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();
}

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

System.IO.Compression现在以Microsoft维护的方式提供


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

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

您需要在.NET4.5或更高版本上执行此操作,因为旧版本上不存在此功能

我刚刚在TechNet上发布了一个脚本,也许有人会发现它很有用,因为它需要.NET4.5或4.7


将System.IO.Compression.ZipFile添加为nuget参考它正在工作

这是一个帮助我的解决方案: 转到工具>NuGet Package Manager>管理NuGet Packaged for Solution…>浏览>
搜索System.IO.Compression.ZipFile并安装它

我正在使用.NET 4.0。。。我没有找到这个dllfile@MohamedKamal事实上,除非您使用.NET4.5,否则您不会这样做,因为在那之前它并不存在;请参阅:4中支持的版本信息。5@MarcGravell我也有同样的错误。我查看了Visual Studio->帮助->关于Microsoft Visual Studio,它显示了.NET版本4.5.51650。接下来我应该检查什么?我没有文件系统引用。只有系统IO压缩。谢谢奇怪的是,它需要一个不在类命名空间中的文件。“这是对传统的一种奇怪的偏离。”克里斯本纳德我发现了同样的问题。包括对System.IO.Compression的引用是不够的,我还需要文件系统。真的很有趣。或者通过NuGet安装它。这与问题有什么关系?他的问题是我如何修复它?这是修复它的一种方法,因为他的.net版本不支持它。遗憾的是,有时升级不是一种选择。投票表决。如果您正在向2000台安装了XP的电脑写信,而您的程序无法进行任何安装来安装.net 4.5 frameworksee,则这与此相关。Rick Strahl感谢您提供的信息。我已经使用过System.IO.Compression;但随后必须添加一个对使用System.IO.Compression.FileSystem的引用,正如您上面描述的那样,以使ZipFile在没有OP原始错误的情况下进行编译。你能给我更多的提示在哪里找到这个吗?