GZipStream or DeflateStream class?

C#.NetCompression

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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

Solution 5 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCaptain SensibleView Question on Stackoverflow
Solution 1 - C#AaronaughtView Answer on Stackoverflow
Solution 2 - C#agnainView Answer on Stackoverflow
Solution 3 - C#n535View Answer on Stackoverflow
Solution 4 - C#Arsen ZahrayView Answer on Stackoverflow
Solution 5 - C#Stefan SteigerView Answer on Stackoverflow