GZipStream 或 DeflateStream 类?
MSDN 文档告诉我以下内容:
GZipStream 类使用 gzip 数据格式,其中包括循环 用于检测的冗余校验值 数据损坏。 gzip 数据格式 使用相同的压缩算法 DeflateStream 类。
看来 GZipStream 在输出中添加了一些额外的数据(相对于 DeflateStream)。我想知道,在什么类型的场景中必须使用 GZipStream 而不是 DeflateStream?
The MSDN documentation tells me the following:
The GZipStream class uses the gzip
data format, which includes a cyclic
redundancy check value for detecting
data corruption. The gzip data format
uses the same compression algorithm as
the DeflateStream class.
It seems GZipStream adds some extra data to the output (relative to DeflateStream). I'm wondering, in what type of a scenario would it be essential to use GZipStream and not DeflateStream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Deflate 只是压缩算法。 GZip 实际上是一种格式。
如果您使用
GZipStream
压缩文件(并使用扩展名.gz
保存),则结果实际上可以由 WinZip 或等归档程序打开gzip 工具。如果您使用
DeflateStream
进行压缩,这些工具将无法识别该文件。如果压缩文件设计为由这些工具打开,则必须使用 GZipStream 而不是 DeflateStream。
如果您通过不可靠的介质(即互联网连接)传输大量数据并且不使用 TCP/IP 等纠错协议,我也认为这一点至关重要。例如,您可能通过串行端口、原始套接字或 UDP 进行传输。在这种情况下,您肯定需要在 GZip 格式中嵌入 CRC 信息,以确保数据正确。
Deflate is just the compression algorithm. GZip is actually a format.
If you use the
GZipStream
to compress a file (and save it with the extension.gz
), the result can actually be opened by archivers such as WinZip or thegzip
tool. If you compress with aDeflateStream
, those tools won't recognize the file.If the compressed file is designed to be opened by these tools, then it is essential to use
GZipStream
instead ofDeflateStream
.I would also consider it essential if you're transferring a large amount of data over an unreliable medium (i.e. an internet connection) and not using an error-correcting protocol such as TCP/IP. For example, you might be transmitting over a serial port, raw socket, or UDP. In this case, you would definitely want the CRC information that is embedded in the GZip format in order to ensure that the data is correct.
GZipStream 与 DeflateStream 相同,但它添加了一些 CRC 以确保数据没有错误。
GZipStream is the same as DeflateStream but it adds some CRC to ensure the data has no error.
好吧,我的第一个答案完全错误。我查找了 Mono 源代码,发现 GZipStream 类实际上将其读/写(以及几乎任何其他)调用重定向到内部 DeflateStream 对象的方法的适当调用:
唯一的区别是它总是创建一个 DeflateStream 对象gzip 标志设置为 true。
这当然不是您问题的答案,但也许会有所帮助。
Well, i was completely wrong in my first answer. I have looked up in Mono source code and found that GZipStream class actually redirects its read/write(and almost any other) calls to an appropriate calls of methods of an internal DeflateStream object:
The only difference, is that it always creates a DeflateStream object with a gzip flag set to true.
This is certainly not an answer to you question, but maybe it'll help a bit.
虽然 GZipStream 似乎使用 DeflateStream 进行解压缩,但这两种算法似乎不能互换。以下测试代码会给你一个例外:
While GZipStream seems to be using DeflateStream to do decompression, the two algorithms don't seem to be interchangeable. Following test code will give you an exception:
迪托(Aaronaught)
请注意另一个重要区别,如
http://www.webpronews.com/gzip-vs -deflate-压缩和性能-2006-12:
我没有测量速度,但我测量了文件大小约为appx。相同。
Dito as per Aaronaught
Note one other important difference as per
http://www.webpronews.com/gzip-vs-deflate-compression-and-performance-2006-12:
I didn't measure the speed, but I measured the file size to be appx. the same.