title: C# networkstream compression - Sharpziplib, DotNetZip, gzipstream all give errors on my stream
body: <p>I have a pair of C# client-server programs that communicate using a networkstream.
  Everything works fine as it is without compression.
  Now I'd like to get the bandwidth-usage down, so I want to use a compressing wrapperstream around my networkstream.</p>
  
  <p>I have tried SharpZipLib, DotNetZip, C#'s own GZipStream - but I can get none of them to work.</p>
  
  <p>SharpZipLib has problems flushing, and applying the fix specified here: <a href="http://community.sharpdevelop.net/forums/p/7855/22139.aspx" rel="nofollow">http://community.sharpdevelop.net/forums/p/7855/22139.aspx</a> results in an exception "Header checksum illegal".</p>
  
  <p>Using DotNetZip's DeflateStream results in a ZLibException("Bad state (invalid stored block lengths)");</p>
  
  <p>GZipStream gives me a System.IO.InvalidDataException stating "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.".</p>
  
  <p>The way I've implemented it is that everytime an array of byte has to be sent by my framework, I create a new Compression stream wrapper around the existing networkstream, write the bytes to the compression stream, and then flush, close &amp; dispose it.
  This to make sure that each WriteMessage(byte[] blah) uses it's own state-independent compressionstream that will be flushed immediately.
  I've taken care to not let any of the streams close the original network stream.</p>
  
  <pre><code>                using (System.IO.Stream outputStream = CreateOutputStreamWrapper(_networkStream))
                  {
                      outputStream.Write(messageBytes, 0, messageBytes.Length);
                      outputStream.Flush();
                      outputStream.Close();
                      outputStream.Dispose();
                  }
  </code></pre>
  
  <p>Basicly, my DecompressionStream is created as follows (optionals commented out)</p>
  
  <pre><code>protected System.IO.Stream CreateInputStreamWrapper(System.IO.Stream inInputStream)
  {
          //return new DeflateStream(inInputStream, CompressionMode.Decompress, true);
          //return new BZip2InputStream(inInputStream, true);
          return new GZipStream(inInputStream, System.IO.Compression.CompressionMode.Decompress, true);
  }
  </code></pre>
  
  <p>and started as</p>
  
  <pre><code>_inputStream.BeginRead(_buffer, 0, _buffer.Length, new AsyncCallback(ReceiveCallback), null);
  </code></pre>
  
  <p>then in the ReceiveCallback, the data is read, the stream is flushed, closed and disposed:</p>
  
  <pre><code>            //Get received bytes count
              var bytesRead = _inputStream.EndRead(ar);
              _inputStream.Flush();
              _inputStream.Close();
              _inputStream.Dispose();
  </code></pre>
  
  <p>and immediately create a new inputStream by calling CreateInputStreamWrapper again.</p>
  
  <p>So what's going on ?
  Since all compression-stream implementations are failing with errors that come down to "there's an error in the datastream" I have a hunch it must be me and my code.
  On the other hand, if I remove the compression and just use the networkstream there's no problem, which makes me think the problem must lie with the compression-code.</p>
  
  <p>Does this sound familiar to anyone ?
  And while we're at it, does anyone know of any (other) compression stream implementations that are suited to wrap around a networkstream ?</p>