How to compress and decompress files in C#

Files are useful for applications to communicate with others. For instance, web applications can communicate with Google's crawlers via sitemaps on the Internet. A web application write urls to its pages to its sitemap. When Google's crawlers come to this web applications, they will download these sitemaps to index urls to the pages of .

To reduce the time needed for files to be trasmitted over the network, it is a good idea for applications to compress files before sending them out to the network, or for a web application, making them available for download via HTTP.

In this post, I document how we can compress and decompress files via the GZip implementation in C# - System.IO.Compression.GZipStream.

GZipStream is a wrapper which wraps around other derivatives of the System.IO.Stream to provide for data compression and decompression. It runs in one of two modes which is specified by one of System.IO.Compression.CompressionMode enumeration members:

  • CompressionMode.Compress - Configures the GZipStream instance to compress data. When a GZipStream instance is being configured to compress data, we will read from a file and write the contents of the file to the GZipStream instance.
  • CompressionMode.Decompress - Configures the GZipStream instance to decompress data. When a GZipStream instance is being configured to decompress data, we will read data from it and write the data to another file.

Compressing a file using GZipStream

FileStream sourceFileStream = File.OpenRead("sitemap.xml");
FileStream destFileStream = File.Create("sitemap.xml.gz");

GZipStream compressingStream = new GZipStream(destFileStream, 
    CompressionMode.Compress);

byte[] bytes = new byte[2048];
int bytesRead;
while ((bytesRead = sourceFileStream.Read(bytes, 0, bytes.Length)) != 0)
{
    compressingStream.Write(bytes, 0, bytesRead);
}

sourceFileStream.Close();
compressingStream.Close();
destFileStream.Close();

The code segment begins by first gaining read access to sitemap.xml, via sourceFileStream, which we are going to compress. After that, we gain write access to sitemap.xml.gz, via destFileStream, which we are going to have our GZipStream instance write the compressed data into.

When we had gained access to the relevant files, we create a GZipStream instance by passing in destFileStream and the CompressionMode.Compress enumeration option into one of its constructors. We set this instance to compressingStream.

Once we have the necessary Stream objects, we begin to compress sitemap.xml. To do that, we simply read from sourceFileStream and write its contents to compressingStream. Note that we are writing 2048 bytes at a time to compressingStream instead of writing byte by byte. This is because if we write byte by byte to compressingStream, our GZipStream instance won't be able to compress our file well.

When we are done with compressing sitemap.xml, we relinquish accesses to sourceFileStream, destFileStream and compressingStream.

Decompressing a file using GZipStream

FileStream sourceFileStream = File.OpenRead("sitemap.xml.gz");
FileStream destFileStream = File.Create("sitemap.xml");

GZipStream decompressingStream = new GZipStream(sourceFileStream, 
    CompressionMode.Decompress);
int byteRead;
while((byteRead = decompressingStream.ReadByte()) != -1) 
{
    destFileStream.WriteByte((byte)byteRead);
}

decompressingStream.Close();
sourceFileStream.Close();
destFileStream.Close();

The code segment begins by first gaining code access to sitemap.xml.gz, via sourceFileStream, which we are going to decompress. After that, we gain write access to sitemap.xml, via destFileStream, which we are going to write decompressed data read from our GZipStream instance.

When we had gained code access to the relevant files, we create a GZipStream instance, passing sourceFileStream and the CompressionMode.Decompress enumeration option into one of its constructors. We set this instance to decompressingStream.

Once we have the necessary Stream objects, we begin to decompress sitemap.xml.gz. To do that, we simply read from decompressingStream and write its contents to destFileStream.

When we are done with decompressing sitemap.xml.gz, we relinquish accesses to decompressingStream, sourceFileStream and destFileStream.

Other posts that may interest you

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

4 Comments

  • Kalu Singh
    June 7, 2014 at 8:37 pm

    Thank you very much for this code. I search all google but i didn’t find this simple code. Keep it up and god with you. Again thank you for this great stuffs.

    • Clivant
      June 9, 2014 at 11:31 am

      Hi Kalu,

      Thank you for dropping by, glad you had found this code useful. 🙂

  • jmlaks
    October 24, 2014 at 2:43 pm

    Thanks Clivant. This code was useful to me. Clear and to the point.

    • Clivant
      October 25, 2014 at 10:49 am

      Hi jmlaks, thanks for dropping by and glad that the code was useful to you.