开发者

GZipStream or DeflateStream class?

The MSDN documentation tells me the following:

The GZipStream class uses the g开发者_JAVA技巧zip 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?


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.


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜