C# 我在“System.IO.Compression”命名空间中没有找到“ZipFile”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15241889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I didn't find "ZipFile" class in the "System.IO.Compression" namespace
提问by Mohamed Kamal
I can't use "Zipfile" class in the name space "System.IO.Compression" my code is :
我不能在命名空间“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);
}
}
}
the error is :
错误是:
The name 'zipfile' does not exist in the current context
当前上下文中不存在名称“zipfile”
How I can solve it ?
我该如何解决?
采纳答案by Marc Gravell
You need to add a dll reference to the assembly, "System.IO.Compression.FileSystem.dll" - and ensure you are using .NET 4.5 (since it doesn't exist in earlier frameworks).
您需要添加对程序集“System.IO.Compression.FileSystem.dll”的 dll 引用 - 并确保您使用的是 .NET 4.5(因为它在早期框架中不存在)。
For info, you can find the assembly and .NET version(s) from MSDN
有关信息,您可以从 MSDN 中找到程序集和 .NET 版本
回答by John Faulkner
you can use an external package if you cant upgrade to 4.5. One such is Ionic.Zip.dll from DotNetZipLib.
如果您无法升级到 4.5,则可以使用外部软件包。其中之一是来自 DotNetZipLib 的 Ionic.Zip.dll。
using Ionic.Zip;
you can download it here, its free. http://dotnetzip.codeplex.com/
你可以在这里下载它,它是免费的。http://dotnetzip.codeplex.com/
回答by Will Ediger
For those who are green programmers in .NET, to add the DLL reference as MarcGravellnoted, you follow these steps:
对于 .NET 中的绿色程序员,要添加MarcGravell指出的 DLL 引用,请按照以下步骤操作:
To add a reference in Visual C#
在 Visual C# 中添加引用
- In Solution Explorer, right-click the project node and click Add Reference.
- In the Add Reference dialog box, select the tab indicating the type of component you want to reference.
- Select the components you want to reference, and then click OK.
- 在解决方案资源管理器中,右键单击项目节点,然后单击添加引用。
- 在“添加引用”对话框中,选择指示要引用的组件类型的选项卡。
- 选择要引用的组件,然后单击“确定”。
From the MSDN Article, How to: Add or Remove References By Using the Add Reference Dialog Box.
来自 MSDN 文章,如何:使用添加引用对话框添加或删除引用。
回答by user6604144
Just go to References and add "System.IO.Compression.FileSystem".
只需转到参考并添加“System.IO.Compression.FileSystem”。
回答by Burgo855
I know this is an old thread, but I just cannot steer away from posting some useful info on this. I see the Zip question come up a lot and this answers nearlly most of the common questions.
我知道这是一个旧线程,但我无法避免发布一些关于此的有用信息。我看到 Zip 问题出现很多,这几乎回答了大多数常见问题。
To get around framework issues of using 4.5+... Their is a ZipStorer class created by jaime-olivares: https://github.com/jaime-olivares/zipstorer, he also has added an example of how to use this class as well and has also added an example of how to search for a specific filename as well.
为了解决使用 4.5+ 的框架问题......他们是由 jaime-olivares 创建的 ZipStorer 类:https: //github.com/jaime-olivares/zipstorer,他还添加了一个示例,说明如何使用此类作为以及还添加了一个如何搜索特定文件名的示例。
And for reference on how to use this and iterate through for a certain file extension as example you could do this:
并参考如何使用它并迭代某个文件扩展名作为示例,您可以这样做:
#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();
}
回答by Erik Rausch
In solution explorer, right-click References, then click to expand assemblies, find System.IO.Compression.FileSystem and make sure it's checked. Then you can use it in your class - using System.IO.Compression;
在解决方案资源管理器中,右键单击“引用”,然后单击以展开程序集,找到 System.IO.Compression.FileSystem 并确保选中它。然后你可以在你的课堂上使用它 -using System.IO.Compression;
回答by Micha? Jarzyna
System.IO.Compression
is now available as a nuget packagemaintained by Microsoft.
System.IO.Compression
现在可以作为Microsoft 维护的nuget 包使用。
To use ZipFile
you need to download System.IO.Compression.ZipFile
nuget package.
要使用,ZipFile
您需要下载System.IO.Compression.ZipFile
nuget 包。
回答by Jose Ortega
The issue here is that you just Added the reference to System.IO.Compression it is missing the reference to System.IO.Compression.Filesystem.dll
这里的问题是您刚刚添加了对 System.IO.Compression 的引用,它缺少对 System.IO.Compression.Filesystem.dll 的引用
And you need to do it on .net 4.5 or later (because it doesn't exist on older versions).
并且您需要在 .net 4.5 或更高版本上执行此操作(因为它在旧版本中不存在)。
I just posted a script on TechNet Maybe somebody would find it useful it requires .net 4.5 or 4.7
我刚刚在 TechNet 上发布了一个脚本 也许有人会发现它很有用它需要 .net 4.5 或 4.7
https://gallery.technet.microsoft.com/scriptcenter/Create-a-Zip-file-from-a-b23a7530
https://gallery.technet.microsoft.com/scriptcenter/Create-a-Zip-file-from-a-b23a7530
回答by prathesh p
Add System.IO.Compression.ZipFile as nuget reference it is working
添加 System.IO.Compression.ZipFile 作为 nuget 参考它正在工作
回答by Hanssss
A solution that helped me: Go to Tools > NuGet Package Manager > Manage NuGet Packaged for Solution... > Browse > Search for System.IO.Compression.ZipFile and install it
一个对我有帮助的解决方案:转到工具 > NuGet 包管理器 > 管理 NuGet 打包解决方案... > 浏览 > 搜索 System.IO.Compression.ZipFile 并安装它