What is the advantage of GZIP vs DEFLATE compression?

27,785

Solution 1

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

Solution 2

The other answer is mostly wrong. The "deflate" value for the Content-Encoding HTTP header is a misnomer that actually means ZLIB. Given that, both have checksums and the same compressed content. They only differ in their headers/footer and which checksum they use.

gzip "deflate" (zlib)
Header size 10 bytes 2 bytes
Footer size 4 bytes 0
Checksum CRC32 Adler-32
Compression algorithm DEFLATE DEFLATE
Specification RFC1952 RFC1950

Historically, "deflate" was problematic because early Microsoft IIS servers would send raw deflate data instead of zlib data (see https://stackoverflow.com/a/9186091/1218408). To work around that, it became common to just use gzip.

Deflate is very slightly faster because Adler-32 is typically faster to compute than CRC32, but the majority of the time is spent actually compressing data and not calculating the checksum.

Share:
27,785

Related videos on Youtube

GibboK
Author by

GibboK

A professional and enthusiastic Senior Front End Developer. Listed as top 2 users by reputation in Czech Republic on Stack Overflow. Latest open source projects Animatelo - Porting to JavaScript Web Animations API of Animate.css (430+ stars on GitHub) Industrial UI - Simple, modular UI Components for Shop Floor Applications Frontend Boilerplate - An opinionated boilerplate which helps you build fast, robust, and adaptable single-page application in React Keyframes Tool - Command line tool which convert CSS Animations to JavaScript objects gibbok.coding📧gmail.com

Updated on December 03, 2021

Comments

  • GibboK
    GibboK over 2 years

    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:

    • Does GZIP offer any advantage over DEFLATE I'm not aware of?
    • Any clue why major search engines use GZIP?

    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");
            }
        }
    
  • GibboK
    GibboK over 12 years
    To summarise.. do you mean GZIP is more browser compatible?
  • Peter
    Peter over 12 years
    GZip will enable the browser to check for errors, deflate won't
  • Pacerier
    Pacerier almost 12 years
    @peer This is wrong, deflate will check for errors using adler32.
  • Peter
    Peter almost 11 years
    @Pacerier you are right the DEFLATE payload is checked by a checksum.
  • david_adler
    david_adler over 2 years
    don't you mean adler-32 is slight faster than crc32?
  • Phil P
    Phil P over 2 years
    I think that you've confused DEFLATE for ZLIB. ZLIB is RFC1950, your reference, and mostly matches your description. DEFLATE is RFC1951.
  • ZachB
    ZachB over 2 years
    @PhilP I didn't. I used the word "deflate" because that's the ill-named value of the Content-Encoding header in the HTTP spec. You're right that it's actually ZLIB, and therein lies the problem I discussed with Microsoft IIS servers. I edited the answer to emphasize this.