ASP.NET GZIP Output

This C# article shows how to send GZIP-compressed files in ASP.NET.

GZIP output. ASP.NET can send a GZIP-compressed file to the output.

This is the easiest way to send compressed files over HTTP using ASP.NET. We can use 7-Zip for the best compression of the GZIP data.

 

 

Example. Sending compressed HTML or other files over the network is far faster, but there are configuration hassles on many servers. For this reason, it is sometimes best to pre-compress the file and send it through an ASP.NET response.

 

Sending GZIP in ASP.NET: C#

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
	string f = Request.PhysicalApplicationPath + "Anthem.gz"; // A
	Response.WriteFile(f); // B
	Response.AddHeader("Content-Encoding", "gzip"); // C
    }
}

Adding an HTML document. Go to Website -> Add New Item and select the HTML page you want to compress. We use a static HTML page. I use the story Anthem by Ayn Rand, available in HTML form at Project Gutenberg. The file is 123 KB.

Compressing with 7-Zip. Next, use 7-Zip or your favorite compression utility to create the "Anthem.gz" file, for example. You can simply add the GZ suffix to the compressed file.

7-Zip Command-Line Examples

Changing ASPX code. This is where we read in the GZIP-compressed file in ASP.NET and then write it to the response. In your code-behind file set up the code to look similar to this.

Description of step A. The code gets the correct physical path to the file on the server using PhysicalApplicationPath. This method is much faster and easier to use than other methods.

Step B. It writes the binary file to the Response. This call here simply dumps the entire "Anthem.gz" file to the response. In step C, the code sets the Content-Encoding header.

Response.WriteFile

Note: This is really, really important and if you don't set this, nothing will work.

 

Page. When you access the Default.aspx page, now, you should see the original HTML in your browser. However, the page size is much smaller. Check this by right-clicking in Firefox and selecting Page Info.

 

And: What Firefox shows is that Default.aspx is 41049 bytes. However, the original Anthem file is 126593 bytes.

 

Discussion. First, this is the sort of article you won't find in many books on ASP.NET. It is more focused on browsers and file types, not Web Forms. You can write file data of any type to the Response, and the browser will render it as instructed.

 

This is ideal for static web sites, which comprise much of the web. This strategy results in performance that I feel is optimal using current technologies. It combines the 7-Zip Deflate algorithm and some ASP.NET C# code.

7-Zip DEFLATE

Tip: You can add output caching, but it won't help and might reduce performance.

Output Cache

Also, please see my research on the Windows file system cache for information about how the GZIP file will be stored in memory between requests. No caching on your part is necessary.

Windows File Cache: Performance

 

Summary. We sent GZIP content in ASP.NET. Many alternative GZIP approaches are available. This method accomplishes compression with nearly no overhead. It also has a higher compression ratio than most GZIP algorithms.