GZIP 与 DEFLATE 压缩相比有何优势?
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Gzip 更可靠,因为它是 deflate 加上一些标头和校验和。换句话说,gzip 是 deflate,还有额外的标头和校验和。 Deflate 通过 adler32 进行检查,它也是 gzip 的一部分。因为 gzip 有效负载是 DEFLATE 压缩的有效负载。
压缩信息
Gzip 信息
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
另一个答案大多是错误的。
Content-Encoding
HTTP 标头的“deflate”值是一个用词不当,实际上意味着 ZLIB。鉴于此,两者都有校验和和相同的压缩内容。它们仅在页眉/页脚以及使用的校验和方面有所不同。从历史上看,“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.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.