title: GZipStream - write not writing all compressed data even with flush?
body: <p>I've got a pesky problem with gzipstream targeting .Net 3.5.  This is my first time working with gzipstream, however I have modeled after a number of tutorials including <a href="https://stackoverflow.com/questions/1590846/net-gzipstream-compress-and-decompress-problem">here</a> and I'm still stuck.</p>
  
  <p>My app serializes a datatable to xml and inserts into a database, storing the compressed data into a varbinary(max) field as well as the original length of the uncompressed buffer.  Then, when I need it, I retrieve this data and decompress it and recreates the datatable.  The decompress is what seems to fail.</p>
  
  <p><strong>EDIT:</strong> Sadly after changing the GetBuffer to ToArray as suggested, my issue remains.  Code Updated below</p>
  
  <p><strong>Compress code:</strong></p>
  
  <pre><code>DataTable dt = new DataTable("MyUnit");
  //do stuff with dt
  //okay...  now compress the table
  using (MemoryStream xmlstream = new MemoryStream())
  {
      //instead of stream, use xmlwriter?
      System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
      settings.Encoding = Encoding.GetEncoding(1252);
      settings.Indent = false;
      System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(xmlstream, settings);
      try
      {
          dt.WriteXml(writer);
          writer.Flush();
      }
      catch (ArgumentException)
      {
          //likely an encoding issue...  okay, base64 encode it
          var base64 = Convert.ToBase64String(xmlstream.ToArray());
          xmlstream.Write(Encoding.GetEncoding(1252).GetBytes(base64), 0, Encoding.GetEncoding(1252).GetBytes(base64).Length);
      }
  
      using (MemoryStream zipstream = new MemoryStream())
      {
          GZipStream zip = new GZipStream(zipstream, CompressionMode.Compress);
          log.DebugFormat("Compressing commands...");
          zip.Write(xmlstream.GetBuffer(), 0, xmlstream.ToArray().Length);
          zip.Flush();
          float ratio = (float)zipstream.ToArray().Length / (float)xmlstream.ToArray().Length;
          log.InfoFormat("Resulting compressed size is {0:P2} of original", ratio);
  
          using (SqlCommand cmd = new SqlCommand())
          {
              cmd.CommandText = "INSERT INTO tinydup (lastid, command, compressedlength) VALUES (@lastid,@compressed,@length)";
              cmd.Connection = db;
              cmd.Parameters.Add("@lastid", SqlDbType.Int).Value = lastid;
              cmd.Parameters.Add("@compressed", SqlDbType.VarBinary).Value = zipstream.ToArray();
              cmd.Parameters.Add("@length", SqlDbType.Int).Value = xmlstream.ToArray().Length;
              cmd.ExecuteNonQuery();
  
          }
      }
  </code></pre>
  
  <p><strong>Decompress Code:</strong></p>
  
  <pre><code>/* This is an encapsulation of what I get from the database
   public class DupUnit{
      public uint lastid;
      public uint complength;
      public byte[] compressed;
  }*/
    //I have already retrieved my list of work to do from the database in a List&lt;Dupunit&gt; dupunits
  foreach (DupUnit unit in dupunits)
  {
      DataSet ds = new DataSet();
      //DataTable dt = new DataTable();
      //uncompress and extract to original datatable
      try
      {
          using (MemoryStream zipstream = new MemoryStream(unit.compressed))
          {
              GZipStream zip = new GZipStream(zipstream, CompressionMode.Decompress);
              byte[] xmlbits = new byte[unit.complength];
              //WHY ARE YOU ALWAYS 0!!!!!!!!
              int bytesdecompressed = zip.Read(xmlbits, 0, unit.compressed.Length);
              MemoryStream xmlstream = new MemoryStream(xmlbits);
              log.DebugFormat("Uncompressed XML against {0} is: {1}", m_source.DSN, Encoding.GetEncoding(1252).GetString(xmlstream.ToArray()));
              try{
                 ds.ReadXml(xmlstream);
              }catch(Exception)
              {
                  //it may have been base64 encoded...  decode first.
                 ds.ReadXml(Encoding.GetEncoding(1254).GetString(
                   Convert.FromBase64String(
                   Encoding.GetEncoding(1254).GetString(xmlstream.ToArray())))
                   );
              }
              xmlstream.Dispose();
          }
      }
      catch (Exception e)
      {
          log.Error(e);
          Thread.Sleep(1000);//sleep a sec!
          continue;
      }
  </code></pre>
  
  <p>Note the comment above...  bytesdecompressed is always 0.  Any ideas?  Am I doing it wrong?</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>So this is weird.  I added the following debug code to the decompression routine:</p>
  
  <pre><code>   GZipStream zip = new GZipStream(zipstream, CompressionMode.Decompress);
     byte[] xmlbits = new byte[unit.complength];
     int offset = 0;
     while (zip.CanRead &amp;&amp; offset &lt; xmlbits.Length)
     {
         while (zip.Read(xmlbits, offset, 1) == 0) ;
         offset++;
     }
  </code></pre>
  
  <p>When debugging, sometimes that loop would complete, but other times it would hang.  When I'd stop the debugging, it would be at byte 1600 out of 1616.  I'd continue, but it wouldn't move at all.</p>
  
  <p><strong>EDIT 3:</strong> The bug appears to be in the compress code.  For whatever reason, it is not saving all of the data.  When I try to decompress the data using a third party gzip mechanism, I only get part of the original data.</p>
  
  <p>I'd start a bounty, but I really don't have much reputation to give as of now :-(</p>