Click here to Skip to main content
15,845,565 members
Articles / Web Development / IIS

Utilize gzip Compression in IIS

Rate me:
Please Sign up or sign in to vote.
4.88/5 (5 votes)
25 Apr 2011CPOL4 min read 61.5K   16   3
Describes the importance of enabling GZIP compression in Web application

GZIP format is developed by GNU Project and standardized by IETF in RFC 1952, which MUST be considered by web developers to improve their websites' performance, there are several Quintessential articles documented using gzip compression. They are:

A gzip compressed HTTP package can significantly save bandwidth, thus speed up browser rendering after use hitting enter, and user experience got improved finally, nowadays most of the popular browsers such as Internet Explorer, Firefox, Chrome, Opera support gzip encoded content (please refer to this link).

PS: the other compression encoding is deflate, "but it's less effective and less popular" (refer to this link). Yahoo uses gzip compression and suggests developers do that:

Image 1

Compression in IIS

For ASP.NET developers who host website$More$ on IIS like me, to achieve this is fairly easy, open IIS manager and select your website, then go to Compression Module. (Apache admins refer here.)

Image 2

Double click and you will see:

Image 3

IIS supports two kinds of compression:

  • Static Compression IIS compress a specific file at first time and only the first time, afterward every time IIS receives request on this file, it will return the compressed data. This is usually used on the files not frequently changing such as a static HTML file, a rarely changed XML, a Word document or any file doesn't change frequently.
  • Dynamic Compress IIS will do compression EVERY time a client's request on one specific file, this usually is used in some content that often changes, for example, there is a large CSV file generating engine located on the server back end, and suppose to transfer to the client side, we can use dynamica compress.

Scott Forsyth's pointed out:"Compression is a trade-off of CPU for Bandwidth.", one of the new features in IIS 7 is web masters can customize IIS compression strategy based on the actual need, you can modify the applicationHost.config under "%windir%\System32\inetsrv\config", below is my sample:

<httpCompression staticCompressionEnableCpuUsage="80" 
    directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="*/*" enabled="false" />

More details are described here. P.S.: Dynamic Compress is not installed by default, we need to install it by turning on Windows Features:

Image 4

My server host provider uses IIS 6.0 and does NOT enabling compression, I checked compression status for by using the free tool provided by port80software and the result is:

Image 5

WOW, I must convince them to enable gzip compression!!

Programmatically Compress using C#

We can also programmatically compress the ASP.NET http response, for example I want to transfer a large CSVfile to the client, a simple ASPX page named ReturnGzipPackage.aspx:

protected void Page_Load(object sender, EventArgs e)
    Response.Headers.Add("Content-Disposition", "attachment; filename=IAmLarge.csv");
    Response.ContentType = "text/csv";

If the request was submitted by a client browser, browser will automatically decompress the Http package, but in the Windows client application or Windows Service, we developers can also adopt gzip compression to save bandwidth, once received gzip Http package from the server, we can programmatically decompress. I wrote a client console application to submit the Http request and receive/decompress the Http response package.

/* Submit Http request to server with Accept-Encoding: gzip */
WebClient client = new WebClient();
// It is mandatory by the client, if this header information is not specified, 
// server will return the original content type, in my case, it is: text/csv
//client.Headers.Add("Accept-Encoding", "gzip");

using(Stream gzipData = client.OpenRead(
    WebHeaderCollection responseHeaders = client.ResponseHeaders;

    using(GZipStream gzip = new GZipStream(gzipData, CompressionMode.Decompress))
        using(StreamReader reader= new StreamReader(gzip))
            String content = reader.ReadToEnd();
            File.WriteAllText("D:\\Downloaded.csv", content);

Please be aware of one thing: "Accept-Encoding: gzip" is supported by all browsers by default, i.e., browser will automatically decompresses compressed Http package, so in the code, we MUST explicitly specify "Accept-Encoding: gzip", below is what I investigated: First time, I explicitly set "Accept-Encoding: gzip", the Http response header contains "Content-Encoding: gzip", and the depression/file saving operations complete without any issues.

Image 6

Second time, I commented out the code, the result is, received Http headers does NOT contain content encoding information, since the server deemed you don't accept gzip encoded content, it won't return you compressed file, instead, it returned the original file, in my case, the CSV file itself.

Image 7

Conclusion & Hints

Using Http Compression is one of the best practices to speed up the web sites, usually consider compress files like below:

  1. Doesn't change frequently
  2. Has a significant compress-rate such as HTML, XML, CSV, etc.
  3. Dynamically generated and the server CPU has availability

Please be aware that you do NOT compress JPG, PNG, FLV, XAP and those kind of image/media files, since they are already compressed, compress them will waste CPU resources and you got a compressed copy with few KBs reduced. :)

This article was originally posted at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) SAP Labs Shanghai
China China
Wayne is a software developer, Tech Lead and also a geek. He has more than 6 years' experience in Web development(server: ASP.NET (MVC), Web Service, IIS; Client: HTML/CSS/JavaScript/jQuery/AJAX), Windows development (Winform, Windows Service, WPF/Silverlight, Win32 API and WMI) and SQL Server. Deep understanding of GOF Design Patterns, S.O.L.i.D principle, MVC, MVVM, Domain Driven Design, SOA, REST and AOP.

Wayne's Geek Life

Infinite passion on programming!

Comments and Discussions

QuestionSample project Pin
venkatabc20-Feb-12 0:00
venkatabc20-Feb-12 0:00 
GeneralGood Work Pin
Irwan Hassan4-May-11 6:21
Irwan Hassan4-May-11 6:21 
GeneralRe: Good Work Pin
Wayne Ye4-May-11 17:49
Wayne Ye4-May-11 17:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.