C# 在 .NET 中创建 ZIP 存档的最佳/最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/384955/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:27:05  来源:igfitidea点击:

What is the best/easiest way to create ZIP archive in .NET?

c#.netzip

提问by Bill

Which method do you think is the "best".

您认为哪种方法是“最好的”。

  • Use the System.IO.Packagingnamespace?
  • Use interop with the Shell?
  • Third party library for .NET?
  • Interop with open source unmanaged DLL?
  • 使用System.IO.Packaging命名空间?
  • 使用与 Shell 的互操作?
  • .NET 的第三方库?
  • 与开源非托管 DLL 互操作?

[I can target Framework 3.5; best = easiest to design, implement, and maintain.]

[我可以针对 Framework 3.5;最好 = 最容易设计、实施和维护。]

I am mostly interested in why you think the chosen approach is best.

我最感兴趣的是为什么您认为所选择的方法是最好的。

采纳答案by Greg Dean

I've always used SharpZipLib

我一直使用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.

忘记了为什么部分:主要是因为它已经存在很长时间了。它有很好的文档记录,并且有一个直观的 API。另外,如果你喜欢这种类型的东西,它是开源的。

回答by Matthew

The GZipStream and DeflateStream classes under System.IO.Compression namespace make it pretty easy to zip things up.

System.IO.Compression 命名空间下的 GZipStream 和 DeflateStream 类使压缩内容变得非常容易。

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 articleby dmihailescu which details their use and provides sample code.

更新:评论正确指出这些类不允许您操作 Zip 文件。但是,J# dll 在 java.util 和 java.io 命名空间中包含所需的功能。有关更多详细信息,请参阅dmihailescu的Code Project 文章,其中详细介绍了它们的用法并提供了示例代码。

回答by configurator

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.

由于您的目标是 Framework 3.5,我会选择System.IO.Packaging. 它是一个专为压缩而设计的库,并随框架提供,因此产品甚至不需要额外的 DLL。但是,使用示例很糟糕。

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).

不推荐使用 shell。首先,它仅适用于 Windows XP 及更高版本。但即使你不在乎,它的陷阱也很多(我以前做过,但只是因为我真的别无选择)。

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.

如果您的目标不是 3.5,我会使用SharpZipLib. 这是一个相当不错的库,即使您使用的是 3.5,您也可能会考虑使用它。

回答by RedawgTS

I've found the DotNetZip Libraryto be this easiest way to work with zip files. SharpZipLibis a far more powerful and flexible solution.

我发现DotNetZip 库是处理 zip 文件的最简单方法。SharpZipLib是一个更加强大和灵活的解决方案。

回答by Berkshire

No one has mentioned the XceedZip 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.

没有人提到过XceedZip 组件。我以前使用过它,它非常适合创建 zip 文件和压缩数据流。我强烈推荐它,但可能需要权衡许可费。

回答by Cheeso

DotNetZipis 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.

DotNetZip使用起来非常简单。我喜欢它,因为它是完全托管的 - 不需要 shell 交互。编程模型比 shell 更简单、更清晰。此外,它比 SharpZipLib 以及在 .NET 3.0 中添加的 Packaging 类更简单。它是免费的、小巧的、积极维护的。

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#.

它比一张海报提供的 J# 选项要好得多 - J# 是一个巨大的运行时和一个巨大的药丸,只为获得 ZIP 支持。此外,J# 支持也将停止。在 J# 上引入新的依赖项可能不是一个好主意。

Example code for DotNetZip:

DotNetZip 的示例代码:

  try
  {
      using (ZipFile zip = new ZipFile(args[0], System.Console.Out))
      {
          zip.AddDirectory(args[1]); // recurses subdirectories
          zip.Save();
      }
  }
  catch (System.Exception ex1)
  {
      System.Console.Error.WriteLine("exception: " + ex1);
  }

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.

DotNetZip 适用于 .NET v2.0、3.0、3.5 以及 Compact Framework v2.0 和 3.5。它可以处理 ZIP 文件、Unicode 文件名、注释、密码。它支持 ZIP64 和自解压档案。它很快。尝试一下。

回答by Sheridan

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 ZipFileclass.

我意识到这是一个老问题,但我只是想我会添加一个更新的答案。如果您使用的是 .NET 4.5,那么您可以使用新的内置ZipFile以编程方式将多个文件轻松保存到 zip 文件夹中。

First, you'll need to prepare your files into a new folder (Directory.CreateDirectory, File.Move) and then you can simply use the ZipFile.CreateFromDirectoryMethod(adapted from the linked page):

首先,您需要将文件准备到一个新文件夹 ( Directory.CreateDirectory, File.Move) 中,然后您可以简单地使用ZipFile.CreateFromDirectory方法(改编自链接页面):

string filePathOfNewFolder = @"c:\example\start";
string zipFilePath = @"c:\example\result.zip";

ZipFile.CreateFromDirectory(filePathOfNewFolder, zipFilePath);

回答by brunoid

Ultimate ZIP is a high-performance and reliable compression component which lets you easily create and manipulate ZIP, TAR, TGZ, GZ archives on-the-fly as well as browse the contents of a previously created archive right in your .NET cross-platform apps. Written purely in C# with the best design patterns and low-level .NET optimizations applied, it offers performance, reliability and extensibility to your .NET applications. The package comes with a number of code snipets and fully documented examples in VB.NET and C# illustrating how to create, zip and unzip archives.

Ultimate ZIP 是一种高性能且可靠的压缩组件,可让您轻松地即时创建和操作 ZIP、TAR、TGZ、GZ 档案,以及直接在 .NET 跨平台中浏览先前创建的档案的内容应用。它完全用 C# 编写,并应用了最佳设计模式和低级 .NET 优化,可为您的 .NET 应用程序提供性能、可靠性和可扩展性。该包附带了许多代码片段和 VB.NET 和 C# 中完整记录的示例,说明了如何创建、压缩和解压缩档案。

Ultimate ZIP component for .NET

.NET 的终极 ZIP 组件