Posted on Monday July 13, 2009

GZip and Deflate page compression in ASP.NET


Having written a GZip compression class in PHP many moons ago, I was interested to find out if anyone had done something similar in ASP.NET/C#. I found it strange that I couldn’t find much out there until I stumbled upon Bart De Smet’s article on MSDN (no longer available). I can’t take much credit for the code below/download besides packaging it up into a download, Bart’s article saved me re-reading the IHttpModule docs and figuring out where to get the stream from that’s for sure.

Most people who have access to their server through terminal services and the IIS manager won’t need to bother with a GZip compression module, as page caching is built into IIS 6 upwards. This probably explains why nobody has done a commercial or OS GZip module for ASP.NET. There’s also tools out there like port’s page compression ISAPI filter that does the job. However for those of us using shared hosting, a GZip compression module is pretty handy for speeding up the responsiveness of the site and saving bandwidth. Most modern browsers support GZip and Deflate (infact I’d guess that all do by now, maybe with the exception of Mobiles/Pocket PCs), and as the module only sends zip’d content back if the browser supports it, so there’s nothing to lose.

The only draw backs of using IHttpModule compared to IIS is that you’re restricted to compressing files that are mapped to the ISAPI filter. This typically means aspx files but a few others can be included. So your images, css files and javascript files won’t be compressed down. There is a solution to this on if you want your script and css files compressed too.

The Zip format lends itself to HTML pages very nicely as the algorithm works best with text files. All that white space you have in your source that makes it humanly readable is left out, squeezing the pages down.

The good things about ASP.NET 2.0 is it makes the whole process so easy it can be done in about 10 lines of code thanks to the IHttpModule interface and the new System.IO.Compression namespace which contains Deflate and GZIP as standard. Previously you would need to have used the Sharp libraries for zipping but now it’s just one assembly.

To install the module, simply copy the download above to your Bin directory, and alter your web.config file to include the following in the node:

I couldn’t think of an original namespace so I settled with Compression. If you want to check that your ASPX pages are being GZip’d or Deflated, install the Live HTTP Headers addon for Firefox, press ALT+L and watch the accept header. As far as I can remember, IE has a similar tool for viewing headers with its web developer toolbar.


Having finished this mini article, I realised Bart’s code does have source code to download, doh!

There is also an open-source compression library for ASP.NET called