title: Decompression error : The magic number in GZip header is not correct
body: <p>I am new to <a href="https://msdn.microsoft.com/en-us/library/system.io.compression(v=vs.110).aspx" rel="nofollow noreferrer">System.IO.Compression</a></p>
  
  <p>I am trying to compress and decompress some information.</p>
  
  <p>For compression I used <a href="https://www.codeproject.com/Articles/27203/GZipStream-Compress-Decompress-a-string" rel="nofollow noreferrer">code project</a> and it seems to work. I am compressing string at the moment.</p>
  
  <p>For decompressing I would like to docompress a <a href="https://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx" rel="nofollow noreferrer"><code>Stream</code></a></p>
  
  <p>This is what I have at the moment</p>
  
  <pre><code>        var zipString = _compressor.Compress(request);
          using (var sw = new StreamWriter(req.GetRequestStream()))
          {
              sw.Write(zipString);
              sw.Close();
          }
  
          WebResponse respStream = req.GetResponse();
          Stream resp = respStream.GetResponseStream();                      
          resp = _compressor.UnCompress(resp);
  </code></pre>
  
  <p>Here is the code for <strong>compressing</strong> </p>
  
  <pre><code>    public string Compress(string s)
      {
          //Transforming string into byte
          var byteArray = new byte[s.Length];
          var indexBa = 0;
          foreach (var item in s.ToCharArray())
          {
              byteArray[indexBa++] = (byte) item;
          }
  
          //prepare for compress
          var ms = new MemoryStream();
          var sw = new GZipStream(ms, CompressionMode.Compress);
  
          //compress
          sw.Write(byteArray,0,byteArray.Length);
  
          //close sw, DO NOT FLUSH because byes will go missing...
          sw.Close();
  
          //transform byte[] zip data to string
          byteArray = ms.ToArray();
          var sb = new StringBuilder(byteArray.Length);
          foreach (var item in byteArray)
          {
              sb.Append((char) item);
          }
          ms.Close();
          sw.Dispose();
          ms.Dispose();
          return sb.ToString();
      }
  </code></pre>
  
  <p>And here is the code for <strong>decompressing</strong></p>
  
  <pre><code>    public Stream UnCompress(Stream s)
      {
          int readCount;
          MemoryStream mem = new MemoryStream();
          byte[] tmp = new byte[128];
          while ((readCount = s.Read(tmp, 0, 128)) != 0)
          {
              mem.Write(tmp, 0, readCount);
          }
          StreamReader reader = new StreamReader(mem);
          char[] buffer = new char[mem.Length];
          mem.Position = 0;
          reader.ReadBlock(buffer, 0, (int)mem.Length);
  
          byte[] zip = Encoding.UTF8.GetBytes(buffer, 0, (int)mem.Length);
          Stream wr = new MemoryStream();
          wr.Write(zip, 0, zip.Length);
          wr.Position = 0;
          using (var stream = new GZipStream(wr, CompressionMode.Decompress))
          {                
              mem = new MemoryStream();
              while ((readCount = stream.Read(tmp, 0, 128)) != 0)
              {
                  mem.Write(tmp, 0, readCount);
              }
              mem.Position = 0;
              return mem;
          }        
      }
  </code></pre>
  
  <p>At this point I get error while decompressing in following line:</p>
  
  <pre><code>while ((readCount = stream.Read(tmp, 0, 128)) != 0)
  {
       mem.Write(tmp, 0, readCount);
  }
  </code></pre>
  
  <p>It just does not enter while loop and says:</p>
  
  <p><em>The magic number in GZip header is not correct. Make sure you are passing in a GZip stream</em></p>
  
  <p>My decompress method is based on following stackoverflow <a href="https://stackoverflow.com/questions/1581694/gzipstream-and-decompression">post</a></p>
  
  <p>Try 2:</p>
  
  <pre><code>        public Stream UnCompress(Stream s)
          {
              byte[] byteArray = null;
              using (var m = new MemoryStream())
              {
                  int count;
                  do
                  {
                      byte[] buf = new byte[1024];
                      count = s.Read(buf, 0, 1024);
                      m.Write(buf, 0, count);
                  } while (s.CanRead &amp;&amp; count &gt; 0);
                  byteArray = m.ToArray();
              }
              var indexBa = 0;
              foreach (var item in s.ToString().ToCharArray())
              {
                  byteArray[indexBa++] = (byte) item;
              }
              //Prepare for decompress
              var ms = new MemoryStream(byteArray);
              var sr = new GZipStream(ms,
                  CompressionMode.Decompress);
  
              //Reset variable to collect uncompressed result
              byteArray = new byte[byteArray.Length];
  
              //Decompress
              var rByte = sr.Read(byteArray, 0, byteArray.Length);
  
              //Transform byte[] unzip data to string
              var sB = new MemoryStream(rByte);
  
              for (var i = 0; i &lt; rByte; i++)
              {
                  sB.Write(byteArray,0,rByte);
              }
              sr.Close();
              ms.Close();
              sr.Dispose();
              ms.Dispose();
              return sB;
          }
  </code></pre>
  
  <p>Result : same error in line  <code>var rByte = sr.Read(byteArray, 0, byteArray.Length);</code>
  Update 1:</p>
  
  <p>As given in code project decompressing string perfectly works.</p>
  
  <pre><code>public static string UnZip(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 decompress
      System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
      System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
          System.IO.Compression.CompressionMode.Decompress);
  
      //Reset variable to collect uncompressed result
      byteArray = new byte[byteArray.Length];
  
      //Decompress
      int rByte = sr.Read(byteArray, 0, byteArray.Length);
  
      //Transform byte[] unzip data to string
      System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
      //Read the number of bytes GZipStream red and do not a for each bytes in
      //resultByteArray;
      for (int i = 0; i &lt; rByte; i++)
      {
          sB.Append((char)byteArray[i]);
      }
      sr.Close();
      ms.Close();
      sr.Dispose();
      ms.Dispose();
      return sB.ToString();
  }
  </code></pre>