How to use System.IO.Compression to read/write ZIP files?

后端 未结 8 1570
灰色年华
灰色年华 2020-12-08 08:13

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

8条回答
  •  萌比男神i
    2020-12-08 08:31

    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);
        }
    

提交回复
热议问题