Chris's coding blog

GZip and Deflate page compression in ASP.NET

July 13, 2009

Download

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 80.com’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 asp.net 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 codeproject.com 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.

Reference

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
HttpCompress.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.IO.Compression;
namespace Compression
{
public class PageCompressionModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
//
// Get the application object to gain access to the request's details
//
HttpApplication app = (HttpApplication)sender;
//
// Accepted encodings
//
string encodings = app.Request.Headers.Get("Accept-Encoding");
//
// No encodings; stop the HTTP Module processing
//
if (encodings == null)
return;
//
// Current response stream
//
Stream baseStream = app.Response.Filter;
//
// Check the browser accepts gzip or deflate (gzip takes preference)
//
encodings = encodings.ToLower();
if (encodings.Contains("gzip"))
{
app.Response.Filter = new GZipStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
app.Context.Trace.Warn("GZIP compression on");
}
else if (encodings.Contains("deflate"))
{
app.Response.Filter = new DeflateStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
app.Context.Trace.Warn("Deflate compression on");
}
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

asp.netcsharp

I'm Chris Small, a software engineer working in London. This is my tech blog. Find out more about me via GithubStackoverflowResume