<p>Are System.IO.Compression.GZipStream or System.IO.Compression.Deflate compatible with zlib compression?</p>

[{'answer_id': 70359, 'author': 'Andreas Bakurov', 'author_id': 7400, 'author_profile': 'https://Stackoverflow.com/users/7400', 'pm_score': 2, 'selected': False, 'text': '<p>They just compressing the data using zlib or deflate algorithms , but does not provide the output for some specific file format. This means that if you store the stream as-is to the hard drive most probably you will not be able to open it using some application (gzip or winrar) because file headers (magic number, etc ) are not included in stream an you should write them yourself.</p>\n'}, {'answer_id': 70372, 'author': 'configurator', 'author_id': 9536, 'author_profile': 'https://Stackoverflow.com/users/9536', 'pm_score': 0, 'selected': False, 'text': "<p>I agree with andreas. You probably won't be able to open the file in an external tool, but if that tool expects a stream you might be able to use it. You would also be able to deflate the file back using the same compression class.</p>\n"}, {'answer_id': 70388, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 2, 'selected': False, 'text': "<p>gzip is deflate + some header/footer data, like a checksum and length, etc. So they're not compatible in the sense that one method can use a stream from the other, but they employ the same compression algorithm.</p>\n"}, {'answer_id': 70658, 'author': 'Rasmus Faber', 'author_id': 5542, 'author_profile': 'https://Stackoverflow.com/users/5542', 'pm_score': 4, 'selected': True, 'text': '<p>From <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx" rel="noreferrer">MSDN</a> about System.IO.Compression.GZipStream:</p>\n\n<blockquote>\n  <p>This class represents the gzip data format, which uses an industry standard algorithm for lossless file compression and decompression.</p>\n</blockquote>\n\n<p>From the <a href="http://www.gzip.org/zlib/zlib_faq.html" rel="noreferrer">zlib FAQ</a>:</p>\n\n<blockquote>\n  <p>The gz* functions in zlib on the other hand use the gzip format.</p>\n</blockquote>\n\n<p>So zlib and GZipStream should be interoperable, but only if you use the zlib functions for handling the gzip-format.</p>\n\n<p>System.IO.Compression.Deflate and zlib are reportedly not interoperable.</p>\n\n<p>If you need to handle zip files (you probably don\'t, but someone else might need this) you need to use <a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx" rel="noreferrer">SharpZipLib</a> or another third-party library.</p>\n'}, {'answer_id': 72820, 'author': 'Isak Savo', 'author_id': 8521, 'author_profile': 'https://Stackoverflow.com/users/8521', 'pm_score': 3, 'selected': False, 'text': "<p>I've used GZipStream to compress the output from the .NET XmlSerializer and it has worked perfectly fine to decompress the result with gunzip (in cygwin), winzip and another GZipStream.</p>\n\n<p>For reference, here's what I did in code:</p>\n\n<pre><code>FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);\nusing (GZipStream gzStream = new GZipStream(fs, CompressionMode.Compress))\n{\n  XmlSerializer serializer = new XmlSerializer(typeof(MyDataType));\n  serializer.Serialize(gzStream, myData);\n}\n</code></pre>\n\n<p>Then, to decompress in c#</p>\n\n<pre><code>FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);\nusing (Stream input = new GZipStream(fs, CompressionMode.Decompress))\n{\n   XmlSerializer serializer = new XmlSerializer(typeof(MyDataType));\n   myData = (MyDataType) serializer.Deserialize(input);\n}\n</code></pre>\n\n<p>Using the 'file' utility in cygwin reveals that there is indeed a difference between the same file compressed with GZipStream and with GNU GZip (probably header information as others has stated in this thread). This difference, however, seems to not matter in practice.</p>\n"}, {'answer_id': 619381, 'author': 'Cheeso', 'author_id': 48082, 'author_profile': 'https://Stackoverflow.com/users/48082', 'pm_score': 4, 'selected': False, 'text': '<p><a href="http://dotnetzip.codeplex.com" rel="noreferrer">DotNetZip</a> includes a DeflateStream, a ZlibStream, and a GZipStream, to handle RFC 1950, 1951, and 1952.  The all use the DEFLATE Algorithm but the framing and header bytes are different for each one.</p>\n\n<p>As an advantage, the streams in DotNetZip do not exhibit the <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=93930" rel="noreferrer">anomaly of expanding data size</a> under compression, reported against the built-in streams.  Also, there is no built-in ZlibStream, whereas DotNetZip gives you that, for good interop with zlib.  </p>\n'}, {'answer_id': 2331025, 'author': 'Blake Ramsdell', 'author_id': 234999, 'author_profile': 'https://Stackoverflow.com/users/234999', 'pm_score': 4, 'selected': False, 'text': '<p>I ran into this issue with Git objects. In that particular case, they store the objects as deflated blobs with a Zlib header, which is documented in <a href="http://www.ietf.org/rfc/rfc1950.txt" rel="noreferrer">RFC 1950</a>. You can make a compatible blob by making a file that contains:</p>\n\n<ul>\n<li>Two header bytes (CMF and FLG from RFC 1950) with the values <code>0x78 0x01</code>\n\n<ul>\n<li><code>CM</code> = 8 = deflate</li>\n<li><code>CINFO</code> = 7 = 32Kb window</li>\n<li><code>FCHECK</code> = 1 = checksum bits for this header</li>\n</ul></li>\n<li>The output of the C# <code>DeflateStream</code></li>\n<li>An Adler32 checksum of the input data to the <code>DeflateStream</code>, big-endian format (MSB first)</li>\n</ul>\n\n<p>I made my own Adler implementation</p>\n\n<pre><code>public class Adler32Computer\n{\n    private int a = 1;\n    private int b = 0;\n\n    public int Checksum\n    {\n        get\n        {\n            return ((b * 65536) + a);\n        }\n    }\n\n    private static readonly int Modulus = 65521;\n\n    public void Update(byte[] data, int offset, int length)\n    {\n        for (int counter = 0; counter &lt; length; ++counter)\n        {\n            a = (a + (data[offset + counter])) % Modulus;\n            b = (b + a) % Modulus;\n        }\n    }\n}\n</code></pre>\n\n<p>And that was pretty much it.</p>\n'}, {'answer_id': 29936729, 'author': 'VirtualVDX', 'author_id': 2386688, 'author_profile': 'https://Stackoverflow.com/users/2386688', 'pm_score': 1, 'selected': False, 'text': '<p>Starting from .NET Framework 4.5 the <code>System.IO.Compression.DeflateStream</code> class uses the zlib library.</p>\n\n<p>From the class\'s <a href="https://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream(v=vs.110).aspx" rel="nofollow noreferrer">MSDN article</a>:</p>\n\n<blockquote>\n  <p>This class represents the Deflate algorithm, which is an industry-standard algorithm for lossless file compression and decompression. Starting with the .NET Framework 4.5, the DeflateStream class uses the zlib library. As a result, it provides a better compression algorithm and, in most cases, a smaller compressed file than it provides in earlier versions of the .NET Framework.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/70347', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3279/']