Home > Code Examples > Simple Gzip / Deflate in .Net

Simple Gzip / Deflate in .Net

As promised this blog post is going to be a really short one. It’s just a quick win to enable your web forms or MVC application to utilise Gzip / Deflate compression and thus speed up the users experience of your web-site.

Why do this?

If a page takes a while to download, many visitors will simply click elsewhere.

Some of the advantages of compression:

  • Better experience for the user, better user retention
  • Reduces your bandwidth and hosting costs
  • Faster loading for slower connections

But as you are reading this post, I’m guessing you already know all this and more so lets get on with the code.

The code

OK, now that is all out of the way lets stand on the shoulders of giants and quickly enable compression for our application.

Add the following section of code to your Global.asax

First we import the namespaces we need, Gzip and Deflate Stream classes as of .net 2.0 are now present in System.IO.Compression

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.IO.Compression" %>

Next we need to declare the handle for the life cycle event in the Global.asax, PreRequestHandlerExecute

void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{

}

Finally drop in the reset of the code and let the magic happen.

Enable Gzip or Deflate depending on Encoding
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    HttpApplication app = sender as HttpApplication;
    string acceptEncoding = app.Request.Headers["Accept-Encoding"];
    Stream prevUncompressedStream = app.Response.Filter;

    if (acceptEncoding == null || acceptEncoding.Length == 0)
        return;

    acceptEncoding = acceptEncoding.ToLower();

    if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
    {
        // defalte
        app.Response.Filter = new DeflateStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "deflate");

    }
    else if (acceptEncoding.Contains("gzip"))
    {
        // gzip
        app.Response.Filter = new GZipStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "gzip");

    }
    app.Response.AppendHeader("Vary", "Accept-Encoding");

}

Now, I would like to claim credit for this, however I can’t. I found this little gem over at www.stardeveloper.com by Faisal Khan.

If you would like to know more about Gzip / Deflate take a look at the following web-sites:

Finally

Using Gzip with minify is now really starting to speed up our pages. Next time we will look at Caching and donut caching your pages for faster users experience. 

Achieving at least a 95/100 on Google’s Page Speed rating and (A /97) on Yahoo! YSlow rating in V2 mode on a graphically, interactive heavy page is straight forward to obtain even without using a CDN (Content delivery network).

I hope you found this helpful, and can progress on it.

If you found this helpful and would like to buy me a beer to say thanks, please

@ArranM

Advertisement
Categories: Code Examples Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 323 other followers