Understanding Compression and Decompression in ASP.NET 2.0
page 2 of 6
by SANJIT SIL
Feedback
Average Rating: 
Views (Total / Last 10 Days): 34657/ 73

Compressing Files

We can wrap a file stream for compressing data as it is written to disk. Following is the example showing compression of a .doc file using GzipStream class. We can take a button (say btnCompress) in form and on button click event we can call the Compress method. After executing the method we will get a compressed file and we can see that the size of the same has been reduced a lot. If we want to get back the original file from the compressed file we will call the DeCompress method. After executing this method we will get the source file. We can take another button (say btnDeCompress) in form and on its click event we can call the DeCompress method. The following code listing contains both Compress and DeCompress method.

Listing 1

private void DeCompress(string srcPath, string dstPath)
{
  using(FileStream srcStream = File.OpenRead(srcPath))
  {
    using(FileStream dstStream = File.OpenWrite(dstPath));
    {
      using(GZipStream dcStream = new GZipStream(srcStream,
        CompressMode.Decompress))int data;
      while ((data = dcStream.ReadByte()) !=  - 1)
        dstStream.WriteByte((byte)data);
    }
  }
}
 
private void Compress(string srcPath, string dstPath)
{
  using(FileStream srcStream = File.OpenRead(srcPath))
  {
    using(FileStream dstStream = File.OpenWrite(dstPath));
    {
      using(GZipStream cStream = new GZipStream(srcStream,
        CompressMode.Compress))
      {
        byte[]data = new byte[srcStream.Length];
        srcStream.Read(data, 0, data.Length);
        cStream.Write(data, 0, data.Length);
      }
    }
  }
}

Difference between DeflateStream and GZipStream

The GZipStream class adds a Cyclic Redundancy Check (CRC) to detect the data corruption. It makes GZipStream more reliable than DeflateStream. An InvalidDataException is thrown with the statement, "The CRC in GZip footer does not match the CRC calculated from the decompressed data." if data has been corrupted.  Due to CRC overhead, GZipStream always creates a larger file after compression compared to DeflateStream, although the difference in size is not very significant. For more information on format, readers can see RFC 1951: DEFLATE 1.3 specification for DeflateStream and RFC 1952: GZIP 4.3 specification for GZipStream.

Limitation

DeflateStream and GZipStream classes cannot be used to decompress files which are compressed by other compression techniques because of difference in header metadata definition. We can use a maximum 4 GB of streams for compression purpose in case of both the archive methods.


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 6:41:47 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search