GZipStream 或 DeflateStream 类?

发布于 2024-08-28 11:16:48 字数 260 浏览 5 评论 0原文

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 技术交流群。

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

发布评论

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

评论(5

陈甜 2024-09-04 11:16:48

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 the gzip tool. If you compress with a DeflateStream, 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 of DeflateStream.

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.

愿与i 2024-09-04 11:16:48

GZipStream 与 DeflateStream 相同,但它添加了一些 CRC 以确保数据没有错误。

GZipStream is the same as DeflateStream but it adds some CRC to ensure the data has no error.

一花一树开 2024-09-04 11:16:48

好吧,我的第一个答案完全错误。我查找了 Mono 源代码,发现 GZipStream 类实际上将其读/写(以及几乎任何其他)调用重定向到内部 DeflateStream 对象的方法的适当调用:

public override int Read (byte[] dest, int dest_offset, int count)
{
    return deflateStream.Read(dest, dest_offset, count);
}

public override void Write (byte[] src, int src_offset, int count)
{
    deflateStream.Write (src, src_offset, count);
}

唯一的区别是它总是创建一个 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:

public override int Read (byte[] dest, int dest_offset, int count)
{
    return deflateStream.Read(dest, dest_offset, count);
}

public override void Write (byte[] src, int src_offset, int count)
{
    deflateStream.Write (src, src_offset, count);
}

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.

忆伤 2024-09-04 11:16:48

虽然 GZipStream 似乎使用 DeflateStream 进行解压缩,但这两种算法似乎不能互换。以下测试代码会给你一个例外:

        MemoryStream wtt=new MemoryStream();
        using (var gs=new GZipStream(wtt,CompressionMode.Compress,true))
        {
            using (var sw=new StreamWriter(gs,Encoding.ASCII,1024,true))
            {
                sw.WriteLine("Hello");
            }
        }
        wtt.Position = 0;
        using (var ds = new DeflateStream(wtt, CompressionMode.Decompress, true))
        {
            using (var sr=new StreamReader(ds,Encoding.ASCII,true,1024,true))
            {
                var txt = sr.ReadLine();
            }
        }

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:

        MemoryStream wtt=new MemoryStream();
        using (var gs=new GZipStream(wtt,CompressionMode.Compress,true))
        {
            using (var sw=new StreamWriter(gs,Encoding.ASCII,1024,true))
            {
                sw.WriteLine("Hello");
            }
        }
        wtt.Position = 0;
        using (var ds = new DeflateStream(wtt, CompressionMode.Decompress, true))
        {
            using (var sr=new StreamReader(ds,Encoding.ASCII,true,1024,true))
            {
                var txt = sr.ReadLine();
            }
        }
兮颜 2024-09-04 11:16:48

迪托(Aaronaught)

请注意另一个重要区别,如
http://www.webpronews.com/gzip-vs -deflate-压缩和性能-2006-12

我测量 DeflateStream 比 GZip 快 41%。

我没有测量速度,但我测量了文件大小约为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 measured the DeflateStream to 41% faster than GZip.

I didn't measure the speed, but I measured the file size to be appx. the same.

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