C# 中的 Zip 文件夹
如何在 C# 中压缩文件夹的示例(简单代码)是什么?
更新:
我没有看到命名空间 ICSharpCode
。 我下载了 ICSharpCode.SharpZipLib.dll,但我不知道在哪里复制该 DLL 文件。 我需要做什么才能看到这个命名空间?
您是否有压缩文件夹的 MSDN 示例的链接,因为我阅读了所有 MSDN,但找不到任何内容。
好的,但我需要下一个信息。
我应该在哪里复制 ICSharpCode.SharpZipLib.dll
才能在 Visual Studio 中查看该命名空间?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这个答案随着 .NET 4.5 的改变而改变。 创建 zip 文件 变为非常简单。 不需要第三方库。
This answer changes with .NET 4.5. Creating a zip file becomes incredibly easy. No third-party libraries will be required.
从 DotNetZip 帮助文件中,http://dotnetzip.codeplex.com/releases/
From the DotNetZip help file, http://dotnetzip.codeplex.com/releases/
BCL 中没有任何内容可以为您执行此操作,但有两个出色的 .NET 库确实支持该功能。
两者我都用过,可以说两者都非常完善,并且有设计良好的 API,所以主要看个人喜好了。
我不确定他们是否明确支持添加文件夹而不仅仅是将单个文件添加到zip文件中,但是使用<创建在目录及其子目录上递归迭代的东西应该很容易a href="http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx" rel="noreferrer">
DirectoryInfo
和FileInfo
类。There's nothing in the BCL to do this for you, but there are two great libraries for .NET which do support the functionality.
I've used both and can say that the two are very complete and have well-designed APIs, so it's mainly a matter of personal preference.
I'm not sure whether they explicitly support adding Folders rather than just individual files to zip files, but it should be quite easy to create something that recursively iterated over a directory and its sub-directories using the
DirectoryInfo
andFileInfo
classes.在 .NET 4.5 中,ZipFile.CreateFromDirectory(startPath, zipPath); 该方法不包括您希望压缩多个文件和子文件夹而不必将它们放在文件夹中的情况。 当您希望解压缩将文件直接放入当前文件夹中时,此选项有效。
这段代码对我有用:
不需要在 zip 存档中“创建”文件夹。 CreateEntryFromFile 中的第二个参数“entryName”应该是相对路径,当解压 zip 文件时,将检测并创建相对路径的目录。
In .NET 4.5 the ZipFile.CreateFromDirectory(startPath, zipPath); method does not cover a scenario where you wish to zip a number of files and sub-folders without having to put them within a folder. This is valid when you wish the unzip to put the files directly within the current folder.
This code worked for me:
Folders don't need to be "created" in the zip-archive. The second parameter "entryName" in CreateEntryFromFile should be a relative path, and when unpacking the zip-file the directories of the relative paths will be detected and created.
System.IO.Packaging 命名空间中有一个 ZipPackage 类,它内置于 .NET 3、3.5 和 4.0 中。
http://msdn.microsoft.com/en-us /library/system.io.packaging.zippackage.aspx
下面是如何使用它的示例。
http://www.codeproject.com/KB/files/ZipUnZipTool.aspx ?显示=打印
There is a ZipPackage class in the System.IO.Packaging namespace which is built into .NET 3, 3.5, and 4.0.
http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx
Here is an example how to use it.
http://www.codeproject.com/KB/files/ZipUnZipTool.aspx?display=Print
MSDN 上有一篇文章 有一个示例应用程序,用于纯粹用 C# 压缩和解压缩文件和文件夹。 我已经成功使用其中的一些课程很长时间了。 如果您需要了解此类信息,该代码是根据 Microsoft 许可许可证发布的。
编辑:感谢 Cheeso 指出我有点落后于时代。 我指出的 MSDN 示例实际上是使用 DotNetZip 并且现在功能确实非常齐全。 根据我之前版本的经验,我很乐意推荐它。
SharpZipLib 也是一个相当成熟的库,受到人们的高度评价,可以在GPL 许可证。 这实际上取决于您的压缩需求以及您如何查看每个压缩的许可条款。
富有的
There's an article over on MSDN that has a sample application for zipping and unzipping files and folders purely in C#. I've been using some of the classes in that successfully for a long time. The code is released under the Microsoft Permissive License, if you need to know that sort of thing.
EDIT: Thanks to Cheeso for pointing out that I'm a bit behind the times. The MSDN example I pointed to is in fact using DotNetZip and is really very fully-featured these days. Based on my experience of a previous version of this I'd happily recommend it.
SharpZipLib is also quite a mature library and is highly rated by people, and is available under the GPL license. It really depends on your zipping needs and how you view the license terms for each of them.
Rich
使用 DotNetZip(以 nuget 包形式提供):
用法:
或者
using DotNetZip (available as nuget package):
Usage:
Or
以下代码使用第三方 来自 Rebex 的 ZIP 组件:
或者如果您想添加更多文件夹,无需多次打开和关闭存档:
您可以在此处下载 ZIP 组件。
使用免费的 LGPL 许可的 SharpZipLib 是一种常见的替代方案。
免责声明:我为 Rebex 工作
Following code uses a third-party ZIP component from Rebex:
Or if you want to add more folders without need to open and close archive multiple times:
You can download the ZIP component here.
Using a free, LGPL licensed SharpZipLib is a common alternative.
Disclaimer: I work for Rebex
“我应该在哪里复制 ICSharpCode.SharpZipLib.dll 才能在 Visual Studio 中查看该命名空间?”
您需要将 dll 文件添加为项目中的引用。 右键单击“解决方案资源管理器”中的“引用”->“添加引用”->“浏览”,然后选择 dll。
最后,您需要将其作为 using 语句添加到您想要使用它的任何文件中。
"Where should I copy ICSharpCode.SharpZipLib.dll to see that namespace in Visual Studio?"
You need to add the dll file as a reference in your project. Right click on References in the Solution Explorer->Add Reference->Browse and then select the dll.
Finally you'll need to add it as a using statement in whatever files you want to use it in.
ComponentPro ZIP 可以帮助您完成该任务。 以下代码片段将文件和目录压缩到文件夹中。 您也可以使用通配符掩码。
http://www.componentpro.com/doc/zip 有更多示例
ComponentPro ZIP can help you achieve that task. The following code snippet compress files and dirs in a folder. You can use wilcard mask as well.
http://www.componentpro.com/doc/zip has more examples