I have a web site in asp.NET 4 (C#).
I’m trying to find a way to better optimize bandwidth for my website.
I read many articles saying that DEFLATE is faster and smaller that GZIP because GZIP (based on DEFLATE) adds some extra data.
Checking the headers of bing.com and google.com it seems that they both send GZIP-encoded data.
Assuming what I read is true, I miss the advantage of GZIP in this case. So I suspect there should be a good reason to prefer GZIP to DEFLATE.
My questions:
Here’s the code I’m using to send DEFLATE (from Global.asax):
protected 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 (!(app.Context.CurrentHandler is Page || app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") || app.Request["HTTP_X_MICROSOFTAJAX"] != null) return; 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"); } }
Compression is a CPU-intensive process, and the more you compress a file, the longer it takes. Because of this, gzip offers a range of compression levels from 1 to 9; 1 offers the fastest compression speed but at a lower ratio, and 9 offers the highest compression ratio but at a lower speed.
GZip is a form of server-side data compression that's helpful in reducing page loading time. In other words, it takes a set of data and makes it smaller for more streamlined, efficient delivery to a user's computer. Gzip compression reduces the size of your HTML, stylesheets, and JavaScript files.
GZIP is better at compressing dynamic data because of its often superior compression speed.
Gzip is the more reliable because it is deflate plus a few headers and a check sum. In other words gzip is deflate, and extra headers and check sum. Deflate is checked with adler32, which is also part of gzip. Because the gzip payload is a DEFLATE-compressed payload.
Deflate info
Gzip info
a gzip file/stream contains:
- a 10-byte header, containing a magic number, a version number and a time stamp - optional extra headers, such as the original file name, - a body, containing a DEFLATE-compressed payload - an 8-byte footer, containing a CRC-32 checksum and the length of the original uncompressed data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With