tags:

views:

966

answers:

6

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.

+15  A: 

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.

Greg Dean
+1  A: 

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.

Matthew
IIRC: These are just compression algorithm, and do not allow for archiving (multiple compressed entries)
Greg Dean
Like Greg said. They don't create ZIP archives. Only GZipped streaming, which is useful for http file transfer, compressing your own file formats, etc. But not for creating zip files.
configurator
Don't use the J# dlls to read/write zip files. Why? Let me count the ways: J# is out of support. The J# runtime is HUGE. The library is not very .NET-oriented (no events, no properties, no Dispose, etc). The zip implementation is old, incomplete (no support for zip passwords, no ZIP64, etc), and buggy. Use a 3rd party library.
Cheeso
+2  A: 

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.

configurator
System.IO.Packaging is for reading/writing new Office 2007 formats and was developed with this in mind. E.g. it is not a general zip implementation and has a couple of limitations for that matter.
liggett78
Can you elaborate on these limitations? I haven't yet used that library and would like some more information.
configurator
+7  A: 

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.

RedawgTS
+1  A: 

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.

Berkshire
Indeed, the license fee is why I didn't recommend it - while it works great, so does SharpZipLib, which is free.
configurator
The SharpZipLib is licensed under modified GPL which can cause problems in some scenarios.
Martin Vobr
+2  A: 

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.

Cheeso