What is the best/easiest way to create ZIP archive

2020-02-02 04:55发布

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.

标签: c# .net zip
8条回答
SAY GOODBYE
2楼-- · 2020-02-02 05:02

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:

  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.

查看更多
迷人小祖宗
3楼-- · 2020-02-02 05:07

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.

查看更多
霸刀☆藐视天下
4楼-- · 2020-02-02 05:17

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 the ZipFile.CreateFromDirectory Method (adapted from the linked page):

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

ZipFile.CreateFromDirectory(filePathOfNewFolder, zipFilePath);
查看更多
何必那么认真
5楼-- · 2020-02-02 05:19

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.

查看更多
我只想做你的唯一
6楼-- · 2020-02-02 05:19

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.

查看更多
ら.Afraid
7楼-- · 2020-02-02 05:21

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.

查看更多
登录 后发表回答