63

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?

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
Captain Sensible
  • 4,946
  • 4
  • 36
  • 46

5 Answers5

84

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.

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
13

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

agnain
  • 147
  • 1
  • 2
5

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
n535
  • 4,983
  • 4
  • 23
  • 28
3

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();
            }
        }
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
1

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.

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • FYI: I would not trust that quote stating DeflateStream is 41% faster. That test is flawed in my opinion: the test was compressing a byte array filled with zeros. Not very realistic, you will get widely varying compression speeds and rations depending on the type of data. The tests in this article seem more realistic though I can’t attest to the accuracy https://ianqvist.blogspot.com/2014/12/net-compression-libraries-benchmark.html – BernieP May 29 '18 at 18:48
  • 2
    Dead link at webpronews.com. I don't see how a 41% difference is possible unless the author was writing data to the streams one byte at a time. If you look at the source of `System.IO.Compression.GzipStream` with dot Peek, .NET Reflector, etc., you can see that `GzipStream` is just a wrapper around `DeflateStream`, the only difference being that it invokes `DeflateStream.SetFileFormatWriter((IFileFormatWriter) new GZipFormatter());` in its constructor. All `GZipFormatter` does is format a 10 byte header, format an 8 byte footer and update a CRC32 checksum as reads/writes progress. – AlwaysLearning Sep 01 '19 at 23:55