在 .NET 中创建 ZIP 存档的最佳/最简单方法是什么?
您认为哪种方法是“最好的”。
- 使用 System.IO.Packaging 命名空间?
- 使用与 Shell 的互操作?
- .NET 的第三方库?
- 与开源非托管 DLL 互操作?
[我可以针对 Framework 3.5; 最好 = 最容易设计、实现和维护。]
我最感兴趣的是为什么您认为所选方法是最好的。
Which method do you think is the "best".
- Use the
System.IO.Packaging
namespace? - Use interop with the Shell?
- Third party library for .NET?
- Interop with open source unmanaged DLL?
[I can target Framework 3.5; best = easiest to design, implement, and maintain.]
I am mostly interested in why you think the chosen approach is best.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我一直使用 SharpZipLib
忘记了原因部分:主要是因为它已经存在很长时间了。 它有完善的文档,并具有直观的 API。 另外,如果您喜欢这种类型的东西,它是开源的。
I've always used SharpZipLib
Forgot the why part: Mainly because its been around for a long time. It is well documented, and has an intuitive API. Plus it's open source if you're into that type of thing.
我发现 DotNetZip Library 是处理 zip 文件的最简单方法。 SharpZipLib 是一个更强大、更灵活的解决方案。
I've found the DotNetZip Library to be this easiest way to work with zip files. SharpZipLib is a far more powerful and flexible solution.
DotNetZip 使用起来非常简单。 我喜欢它,因为它是完全托管的——不需要 shell 交互。 编程模型比 shell 的编程模型更简单、更清晰。 而且它比 SharpZipLib 以及 .NET 3.0 中添加的打包类更简单。 它是免费的、小型的、积极维护的。
它比一位发帖人提供的 J# 选项要好得多 - J# 是一个巨大的运行时,并且是一个需要吞下的巨大药丸才能获得 ZIP 支持。 此外,J# 支持也已停止。 引入对 J# 的新依赖项可能不是一个好主意。
DotNetZip 的示例代码:
DotNetZip 适用于 .NET v2.0、3.0、3.5 以及 Compact Framework v2.0 和 3.5。 它可以处理 ZIP 文件、Unicode 文件名、注释、密码。 它支持 ZIP64 以及自解压档案。 它很快。 尝试一下。
DotNetZip is very simple to use. I like it because it is fully-managed - no shell interaction required. The programming model is simpler and cleaner than that for the shell. Also it is simpler than SharpZipLib as well as the Packaging classes added in .NET 3.0. It is free, small, actively maintained.
It is much better than the J# option that one poster offered - J# is a huge runtime and a giant pill to swallow to get just ZIP support. Also J# support is being discontinued. Probably not a good idea to introduce new dependencies on J#.
Example code for DotNetZip:
DotNetZip works with .NET v2.0, 3.0, 3.5 as well as Compact Framework v2.0 and 3.5. It does ZIP files, Unicode filenames, comments, passwords. It does ZIP64 as well as Self-extracting archives. It's FAST. Try it.
我意识到这是一个老问题了,但我只是想添加一个更新的答案。 如果您使用的是 .NET 4.5,则可以使用新的内置
ZipFile
类。首先,您需要将文件准备到一个新文件夹(
Directory.CreateDirectory
、File.Move
),然后您可以简单地使用ZipFile.CreateFromDirectory
方法(改编自链接页面) :I realise that this is an old question now, but I just thought I'd add a more up to date answer. If you are using .NET 4.5, then you can programmatically save multiple files into a zip folder easily using the new built in
ZipFile
class.First, you'll need to prepare your files into a new folder (
Directory.CreateDirectory
,File.Move
) and then you can simply use theZipFile.CreateFromDirectory
Method (adapted from the linked page):System.IO.Compression 命名空间下的 GZipStream 和 DeflateStream 类使压缩变得非常容易。
更新:
注释正确地指出这些类不允许您操作 Zip 文件。 但是,J# dll 包含 java.util 和 java.io 命名空间中所需的功能。 有关更多详细信息,请参阅 dmihailescu 的代码项目文章,其中详细介绍了它们的使用并提供了示例代码。
The GZipStream and DeflateStream classes under System.IO.Compression namespace make it pretty easy to zip things up.
UPDATE:
Comments correctly state that these classes do not allow you to manipulate a Zip file. However, the J# dlls contain the required functionality in java.util and java.io namespaces. For more details see the Code Project article by dmihailescu which details their use and provides sample code.
没有人提到 Xceed Zip 组件。 我以前用过它,它在创建 zip 文件和压缩数据流方面效果非常好。 我强烈推荐它,但可能需要权衡许可费。
No one has mentioned the Xceed Zip component. I've used it before and it worked excellent for creating zip files and compressing data streams. I highly recommend it but there may be a license fee tradeoff.
由于您的目标是 Framework 3.5,因此我会选择 System.IO.Packaging。 它是一个专门为压缩而设计的库,并且附带框架,因此该产品甚至不需要额外的 DLL。 不过,这个用法示例很糟糕。
不建议使用 shell。 首先,它只能在 Windows XP 及更高版本上运行。 但即使你不关心这一点,它的陷阱也相当多(我以前也这样做过,但只是因为我真的别无选择)。
如果您的目标不是 3.5,我会使用
SharpZipLib
。 这是一个非常好的库,即使您使用 3.5,您也可能会考虑它。Since you're targetting Framework 3.5, I'd go with
System.IO.Packaging
. It's a library designed specifically for zipping, and comes with the framework so there's not even an extra DLL needed by the product. The usage example is atrocious, though.Using the shell is unrecommended. First, it would only work on Windows XP and above. But even if you don't care about that, it's pitfalls are quite numerous (I've done it before, but only because I really had no choice).
if you're not targetting 3.5, I'd use
SharpZipLib
. It's quite a good library, and even if you are using 3.5 you might consider it.