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