Home » Articles


Sponsored links

CodeDigest Navigation

Technology News
No News Feeds available at this time.

Community News
No News Feeds available at this time.
Compress and DeCompress of files using GZipStream in Asp.Net

By Suresh Kumar Goudampally
Posted On Dec 18,2010
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

Compress and Decompress of files using GZipStream in Asp.Net


In general we all know that we compress the data and post it to the web server and the server decompresses the data to process the request. This is done to optimize the server performance and gain the bandwidth benefit. In this article I will be explaining how to compress a file using GZipStream utility in .Net and post it to the server. And also how the server application decompresses the data and executes the request. I will be demonstrating the whole process using a windows form application which posts the compressed data to the Asp.Net application which is hosted on the IIS server.


Before further delving into the details, I would like to explain what GZipStream is. GZipStream is used to compress the data in gzip format. GZipStream compress with namespace System.IO.Compress. Sending the gzip compressed data to server reduces the file size and gets the bandwidth benefit. Using the GZipStream class we can work in such a way with data in memory, as well as in disk.


Client Code for compressing the file using C#.Net

Take a windows form application and drag a button on to the form. On the click event of the button write the following code.

string url = "http://server/GzipSample/Handler2.ashx";

            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);           

            myHttpWebRequest.Method = "POST";           

            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";                       

            myHttpWebRequest.Headers.Add("Content-Encoding", "gzip");

            //the above code creates an httpwebrequest and adds the required Headers to transfer the compressed data


            FileStream infile = new FileStream("E:\\sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);

            byte[] buffer = new byte[infile.Length];

            int count = infile.Read(buffer, 0, buffer.Length);

            if (count != buffer.Length)






            //the above code converts the filestream to bytes


            Stream newStream = myHttpWebRequest.GetRequestStream();

            GZipStream compressedzipStream = new GZipStream(newStream, CompressionMode.Compress);           

            compressedzipStream.Write(buffer, 0, buffer.Length);


            //The above code send the gzip compressed data the server as mentioned in the url


            HttpWebResponse httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();


Take a sample file sample.txt  and put some data in the following format

Suresh Kumar

Satheesh Babu

Sravan Kumar



When you click the button and observe the fiddler the http request is sent in the following format

POST http://server/GzipSample/Handler2.ashx HTTP/1.1

Content-Type: application/x-www-form-urlencoded

Content-Encoding: gzip

Host: server

Content-Length: 145

Expect: 100-continue

Connection: Keep-Alive




Server code on the Asp.Net application to decompress the data

Write an HttpHandler Handler1.ashx to process the request. Write the following code to decompress the data.

public void ProcessRequest(HttpContext context)


            String File = "C:\\Temp\\decompress.txt";

            using (StreamReader textReader = GetStreamReader(context))

            using (StreamWriter textWriter = new StreamWriter(File))


                String line = "";




                    line = textReader.ReadLine();

                } while (!line.Contains("End"));




            context.Response.ContentType = "text/plain";

            context.Response.Write("Hello World");


private static StreamReader GetStreamReader (HttpContext context)


            if (context.Request.Headers["Content-Encoding"] != null && context.Request.Headers["Content-Encoding"].Contains("gzip"))


                return new StreamReader(

                    new GZipStream(context.Request.InputStream, CompressionMode.Decompress));




                return new StreamReader(context.Request.InputStream);



Check the output file and compare with original file.



Though the Compression and Decompression is the familiar concept known to all, this article mainly speaks about basics of how to compress the data using GzipStream on the client application and post to the server.  And how the server application developed in Asp.Net receives the data and decompresses it.


Similar Articles
You can contribute to CodeDiget.Com:
Donate to
Article Feedback