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.