.net 中的 deflate、gzip 和 zlib 的一个库
首先,让我们定义一些常见的混淆术语:
deflate = compression_algorithm;
zlib = header + deflate + trailer;
gzip = header + deflate + trailer;
我正在寻找一个库,它基本上可以让我执行以下操作:
if(method == "gzip"){
Response.Filter = new CompressionLibrary.OutputStream(Response.Filter, CompressionLibrary.Formats.GZIP);
}
else if(method == "deflate"){
Response.Filter = new CompressionLibrary.OutputStream(Response.Filter, CompressionLibrary.Formats.DEFLATE);
}
else if(method == "zlib"){
Response.Filter = new CompressionLibrary.OutputStream(Response.Filter, CompressionLibrary.Formats.ZLIB);
}
我正在寻找一种方法来比较测试在网络上使用的 3 种压缩格式。我希望每种格式的 deflate 压缩算法具有相同的精确实现。我已经破解了 zlib.net,强制它根据命令给我原始放气(通过“未记录的功能”)……但是,添加 gzip 标头和预告片几乎超出了我的范围。
有人知道有一个 .net 库可以做到这一点吗?
说明:
HTTP 1.1 的deflate压缩格式实际上是zlib压缩格式。 Zlib 是 deflate 的包装器;它有一个 2 字节标头和一个 4 字节标尾,始终(当压缩方法和级别相同时)。
Gzip 在内部使用与 zlib 相同的压缩数据格式...即 deflate(原始 deflate,而不是 HTTP 1.1 deflate [即 zlib])。根据我自己的初步测试,gzip 压缩的数据比 zlib 大 12 倍中的 11 倍。
deflate是一种用于压缩数据的压缩算法。当紧缩数据周围没有包装器方法(例如,标头或尾部)时,我将其称为“deflate” - 也许我应该将其称为“原始deflate”。
我正在分析这些压缩方法及其在网络浏览器中的支持,并且需要对所有三种类型使用单一压缩方法。
First, let's define some commonly confused terms:
deflate = compression_algorithm;
zlib = header + deflate + trailer;
gzip = header + deflate + trailer;
I'm looking for a library that will basically let me do the following:
if(method == "gzip"){
Response.Filter = new CompressionLibrary.OutputStream(Response.Filter, CompressionLibrary.Formats.GZIP);
}
else if(method == "deflate"){
Response.Filter = new CompressionLibrary.OutputStream(Response.Filter, CompressionLibrary.Formats.DEFLATE);
}
else if(method == "zlib"){
Response.Filter = new CompressionLibrary.OutputStream(Response.Filter, CompressionLibrary.Formats.ZLIB);
}
I'm looking for a way to comparably test the 3 compression formats for use on the web. I would like for the deflate compression algorthims for each format to be the same exact implementation. I've already hacked away at zlib.net to force it to give me raw deflate on command (via an "undocumented feature")...however, adding the gzip header and trailer are little out of my league.
Anyone know of a .net library that does this?
Clarification:
HTTP 1.1's deflate compression format is actually the zlib compression format. Zlib is a wrapper around the deflate; it has a 2 byte header and a 4 byte trailer, always (when the compression methods and levels are identical).
Gzip uses the same compressed data format internally as zlib...which is deflate (raw deflate, not HTTP 1.1 deflate [which is zlib]). From my own preliminary testing, gzipped data is 11 out of 12 times larger than zlib.
deflate is a compression algorithm that is used to compress data. When there are no wrapper methods (e.g., headers or trailers) around deflated data, I call it "deflate" - perhaps I should have called it "raw deflate" instead.
I am doing an analysis of these compression methods and their support within web browsers and need to use a single compression method for all three types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DotNetZip 确实 RFC 1950(ZLIB),RFC 1951(DEFLATE)和 RFC 1952 (GZIP)。
它对所有三个都使用相同的底层压缩引擎。
DotNetZip 还可以处理 ZIP 文件。
DotNetZip does RFC 1950 (ZLIB), RFC 1951 (DEFLATE), and RFC 1952 (GZIP).
It uses the same underlying compression engine for all three.
DotNetZip also does ZIP files.
根据我对标准文档的阅读以及我对 zlib、.NET gzip 和 deflate 实现以及其他几个 .NET 压缩包所做的工作,我确定:
1) “raw deflate”始终小于你所说的“HTTP 1.1 deflate”,它总是比 gzip 小。假设您使用相同的库来生成所有三个。也就是说,对于任何特定的压缩库, deflate <兹库gzip。
2)尺寸差异非常小。 deflate 和 zlib 之间的差异通常只是几个字节。 deflate 和 gzip 之间的差异最多只有几十个字节。无论文件大小如何,都是如此。
3) 不同的 deflate 实现具有广泛不同的压缩比和执行时间。例如,zlib 实现比 .NET 3.5 实现提供更好的压缩和更快的执行速度。
4) 不同实现之间的互操作性几乎是100%。也就是说,由一个库创建的 deflate(或 gzip)文件可以由任何其他库解压缩。我听说过一些情况并非如此,但我无法构建一个。
5) 由于 CRC 计算,创建 gzip 所需的时间比创建 zlib 所需的时间要长得多。
我不知道 C# 库允许您在给定原始 deflate 数据的情况下生成 zlib 或 gzip 文件,但如果您研究标准文档,您应该能够相当容易地构建它们。
我也不知道有哪个浏览器支持“raw deflate”。但我不能说我真的尝试过。我一直使用“HTTP 1.1 deflate”。
Based on my reading of the standards documents and the work I've done with zlib, the .NET gzip and deflate implementations, and several other compression packages for .NET, I've determined:
1) "raw deflate" is always smaller than what you call "HTTP 1.1 deflate", which is always smaller than gzip. Assuming that you used the same library to generate all three. That is, for any particular compression library, deflate < zlib < gzip.
2) The differences in size are very small. The difference between deflate and zlib is usually just a few bytes. The difference between deflate and gzip is, at most, a few dozen bytes. This is true regardless of the file size.
3) Different deflate implementations have widely varying compression ratios and execution times. The zlib implementation, for example, gives better compression and faster execution than the .NET 3.5 implementation.
4) Interoperability between the different implementations is almost 100%. That is, a deflate (or gzip) file created by one library can be decompressed by any other library. I have heard of cases where this is not true, but I was unable to construct one.
5) It takes significantly longer to create gzip than it does zlib, because of the CRC calculation.
I do not know of a C# library that allows you to generate a zlib or gzip file, given the raw deflate data, but you should be able to construct them fairly easily if you study the standards documents.
I also do not know of any browser that supports "raw deflate". But then, I can't say that I've actually tried it. I've always used the "HTTP 1.1 deflate".