title: Zlib compression imcompatible between Ionic.Zip, System.IO.Compression and SharpCompress
body: <p>I am working on porting a file format (OMF) into C#. Part of the storage in the file is an array of zlib compressed data. An existing version of the file formatter uses a static method from Ionic.Zip to read the file, as follows:</p>
  
  <pre><code>public static byte[] Uncompress(this byte[] value)
      {
          // Uncompress
          return ZlibStream.UncompressBuffer(value);
      }
  </code></pre>
  
  <p>The project I am working on currently already uses SharpCompress and using 2 different compression libraries seemed wasteful, so I figured I would rewrite it to use SharpCompress. SharpCompress does not have the UncompressBuffer static function that Ionic does, so I implemented it as follows which seemed to be a pretty standard approach in my reading:</p>
  
  <pre><code>using (var originalStream = new MemoryStream(value))
          {
              using (var decompressedStream = new MemoryStream())
              {
                  using (var decompressor = new ZlibStream(originalStream, CompressionMode.Decompress))
                  {
                      decompressor.CopyTo(decompressedStream);
                      return decompressedStream.ToArray();
                  }
              }
          }
  </code></pre>
  
  <p>I have also tried a similiar approach using the <strong>System.IO.Compression.DeflateStream</strong> class, following the MSDocs provided pattern. However, in both cases, at the CopyTo function call, I get an exception indicating there is an issue with the data:</p>
  
  <p>For Zlib:  <em>'Zlib Exception: Bad state (incorrect data check)'</em></p>
  
  <p>For Windows: <em>'Block length does not match with its complement'</em></p>
  
  <p>It could be something I am missing that differentiates the function of the UncompressBuffer function from this method of decompression, but it seems like the UncompressBuffer function works with internal portions of the Zlib Class.</p>
  
  <p>What am I doing wrong here? Is there a difference between the implementations of the 2 zip libraries that makes them incompatible?</p>