title: compression string in c#
body: <p>I am using the following code to compress 2 strings, it works well in compression. However, when I try to decompress the second strings and if the first one is short (about 4 characters) I got Block length does not match with its complement exception.
  Here is the class I used for compression:</p>
  
  <pre><code>    using System;
      using System.IO;
      using System.IO.Compression;
      using System.Text;
  
      namespace CompressString {
      internal static class StringCompressor
  {
      /// &lt;summary&gt;
      /// Compresses the string.
      /// &lt;/summary&gt;
      /// &lt;param name="text"&gt;The text.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static string CompressString(string value)
      {
          //Transform string into byte[] 
          byte[] byteArray = new byte[value.Length];
          int indexBA = 0;
          foreach (char item in value.ToCharArray())
          {
              byteArray[indexBA++] = (byte)item;
          }
  
          //Prepare for compress
          System.IO.MemoryStream ms = new System.IO.MemoryStream();
          System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
          System.IO.Compression.CompressionMode.Compress);
  
          //Compress
          sw.Write(byteArray, 0, byteArray.Length);
          //Close, DO NOT FLUSH cause bytes will go missing...
          sw.Close();
  
          //Transform byte[] zip data to string
          byteArray = ms.ToArray();
          System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
          foreach (byte item in byteArray)
          {
              sB.Append((char)item);
          }
          ms.Close();
          sw.Dispose();
          ms.Dispose();
  
          return sB.ToString();
      }
  
      /// &lt;summary&gt;
      /// Decompresses the string.
      /// &lt;/summary&gt;
      /// &lt;param name="compressedText"&gt;The compressed text.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static string DecompressString(string sData)
      {
          byte[] byteArray = new byte[sData.Length];
  
          int indexBa = 0;
          foreach (char item in sData)
              byteArray[indexBa++] = (byte)item;
  
          MemoryStream memoryStream = new MemoryStream(byteArray);
          GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
  
          byteArray = new byte[1024];
  
          StringBuilder stringBuilder = new StringBuilder();
  
          int readBytes;
          while ((readBytes = gZipStream.Read(byteArray, 0, byteArray.Length)) != 0)
          {
              for (int i = 0; i &lt; readBytes; i++) stringBuilder.Append((char)byteArray[i]);
          } gZipStream.Close(); memoryStream.Close(); gZipStream.Dispose(); memoryStream.Dispose(); return stringBuilder.ToString();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I got the exception in DecompressString method in the line:</p>
  
  <pre><code>while ((readBytes = gZipStream.Read(byteArray, 0, byteArray.Length)) != 0)
  </code></pre>