views:

9039

answers:

6

I know there are libraries out there for working with ZIP files. And, you can alternatively use the functionality built into Windows for working ZIP files.

But, I'm wondering if anyone has worked out how to use the tools built into the System.IO.Compression namespace within .NET for reading/writing ZIP files? Or, is it not possible using only this namespace?

UPDATED: I've seem someone comment that the System.IO.Packaging namespace might be usefull with this also. Does anyone know exactly how to do it?

+7  A: 

MSDN has a complete example http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx using the ZipPackage class. Requires .NET 3.5.

Dave Moore
+3  A: 

Dave, very nice!! I didn't know that was in there.

Now that I know what to look for, I was able to find an article with a small code sample on how to use it: http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

On a related note, I also found the DotNetZip project that looks extremely easy to use.

Chris Pietschmann
A: 
Yadyn
Your solution does not read or write to the ZIP file format, but simply to the raw deflate compression, or GZip simple header-footer extension around the deflate compression.
Martin Plante
+2  A: 

You will want to use a third-party library, like http://www.codeplex.com/DotNetZip, rather than trying to use GZipStream or DeflateStream to read a zip file.

The ***Stream classes in .NET can let you read or write compressed streams of bytes. These classes DO NOT read or write zip files. The zip file is compressed data surrounded by "an envelope" or a header. Think of it as metadata - it includes the name of the file, timestamp, CRC, and a bunch of other stuff. The **Stream classes produce only the stream of compressed data, and do not know how to produce or consume the metadata, which is described in the PKZip format specification maintained by PKWare.

Third party libraries like DotNetZip handle the metadata in a ZIP archive. They may or may not use the System.IO.Compression.DeflateStream() class to produced the compressed stream of bytes. In previous releases, for example, DotNetZip used the built-in DeflateStream. As of v1.7, DotNetZip includes its own DeflateStream which is more efficient than the one shipped in the .NET Framework. As an added benefit, the embedded DeflateStream in DotNetZip allows DotNetZip to be used on the .NET Compact Framework 2.0, which lacks a System.IO.Compression.DeflateStream. (it was added in Compact Framework 3.5)

There's a good forum on the DotNetZip site if you have more questions. Example C# code:

    try
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.AddDirectory(DirectoryToZip); // recurses subdirs
            zip.Save(Filename);
        }
    }
    catch (System.Exception ex1)
    {
        System.Console.Error.WriteLine("exception: " + ex1);
    }
Cheeso
A: 

have a look to: zipstorer.codeplex.com