GZIP 与 DEFLATE 压缩相比有何优势?

发布于 2024-12-02 06:04:30 字数 1653 浏览 2 评论 0原文

我有一个使用 asp.NET 4 (C#) 的网站。

我正在尝试找到一种方法来更好地优化我的网站的带宽。

我读过很多文章说 DEFLATE 比 GZIP 更快、更小,因为 GZIP(基于 DEFLATE)添加了一些额外的数据。

检查 bing.com 和 google.com 的标头,似乎它们都发送 GZIP 编码的数据。

假设我读到的内容是真实的,我就错过了 GZIP 在这种情况下的优势。所以我怀疑应该有充分的理由选择 GZIP 而不是 DEFLATE。

我的问题:

  • GZIP 是否比 DEFLATE 有任何我不知道的优势?
  • 知道为什么主要搜索引擎使用 GZIP 吗?

这是我用来发送 DEFLATE 的代码(来自 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");
        }
    }

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");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-12-09 06:04:30

Gzip 更可靠,因为它是 deflate 加上一些标头和校验和。换句话说,gzip 是 deflate,还有额外的标头和校验和。 Deflate 通过 adler32 进行检查,它也是 gzip 的一部分。因为 gzip 有效负载是 DEFLATE 压缩的有效负载。

压缩信息

Gzip 信息

gzip 文件/流包含:

 - 一个 10 字节的标头,包含一个幻数、一个版本号和一个时间戳
- 可选的额外标头,例如原始文件名,
- 主体,包含 DEFLATE 压缩的有效负载
- 8 字节页脚,包含 CRC-32 校验和以及原始未压缩数据的长度

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
德意的啸 2024-12-09 06:04:30

另一个答案大多是错误的。 Content-Encoding HTTP 标头的“deflate”值是一个用词不当,实际上意味着 ZLIB。鉴于此,两者都有校验和和相同的压缩内容。它们仅在页眉/页脚以及使用的校验和方面有所不同。

gzip"deflate" (zlib)
标头大小10 字节2 字节
页脚大小4 字节0
校验和CRC32Adler-32
压缩算法DEFLATEDEFLATE
规范RFC1952RFC1950

从历史上看,“deflate”是有问题的,因为早期的 Microsoft IIS 服务器会发送原始 deflate 数据而不是 zlib数据(参见https://stackoverflow.com/a/9186091/1218408)。为了解决这个问题,通常只使用 gzip。

Deflate 的速度稍快一些,因为 Adler-32 的计算速度通常比 CRC32 快,但大部分时间都花在实际压缩数据而不是计算校验和上。

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 size10 bytes2 bytes
Footer size4 bytes0
ChecksumCRC32Adler-32
Compression algorithmDEFLATEDEFLATE
SpecificationRFC1952RFC1950

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文