title: Compressed content in a byte array to uncompressed in a string
body: <p><strong>How can we create a simple function to take the compressed content from an array of byte (compressed with Deflate method, ANSI encoded) and express it in a string?</strong></p>
  <p>I go with this one :</p>
  <pre><code>public string UnzipString3(byte[] byteArrayCompressedContent)
      {
          int compressedSize = byteArrayCompressedContent.Length;
          try
          {
              using (MemoryStream inputMemoryStream = new MemoryStream(byteArrayCompressedContent))
              {
                  //I need to consume the first 2 bytes to be able read the stream
                  //If I consume 0 or 4, I can't exploit it                        
                  inputMemoryStream.Seek(2, SeekOrigin.Begin);
                  using (DeflateStream deflateStream = new DeflateStream(inputMemoryStream, System.IO.Compression.CompressionMode.Decompress))
                  {
                      //deflateStream.BaseStream.Position = 0;
                      using (StreamReader reader = new StreamReader(deflateStream, System.Text.Encoding.UTF8))
                      {
                          string uncompressedContent = reader.ReadToEnd();
                          reader.Close();
                          return uncompressedContent;
                      }
                  }
              }
          }
          catch (Exception e)
          {
              return &quot;&quot;;
          }
      }
  </code></pre>
  <p>I can see read the compressed data of inputMemoryStream if I want but <strong>the uncompressedContent given by StreamReader.ReadToEnd() always return an empty string</strong>. According to MSDN (<a href="https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(v=vs.110).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(v=vs.110).aspx</a>), it is supposed to happen when it is read and the position is already at the end of the &quot;Stream&quot; (I get confused with which stream), but a StreamReader hasn't a position and I can't change the position of the DeflateStream, because it's a stream without a position and the length, etc.</p>
  <p>Putting the compressStream position to 0 before to copy leads to a Block length does not match with its complement&quot; error <strong>in any case.</strong></p>
  <p><strong>EDIT:</strong></p>
  <p>When I use the solution of RaTruong :</p>
  <pre><code>public static string UnzipString3(byte[] byteArrayCompressedContent)
  {
      using (var outputStream = new MemoryStream())
      {
          using (var compressStream = new MemoryStream(byteArrayCompressedContent))
          {
              using (var deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
              {
                  deflateStream.CopyTo(outputStream);
              }
          }
          return Encoding.UTF8.GetString(outputStream.ToArray());
      }
  }
  </code></pre>
  <p>It leads to the dilemma I always had: either I do like this and the DeflateStream.CopyTo function returns a <strong>&quot;Block length does not match with its complement&quot; error</strong>, or I consume then two first bytes and it actually does not bring any error but still <strong>copy nothing and then returns an empty string</strong>...</p>
  <p>If I try to decompress a FileStream of a File instead of its array of bytes, the same situation happens.
  I use the decompress function given in MSDN (<a href="https://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream(v=vs.110).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream(v=vs.110).aspx</a>) :</p>
  <pre><code>public string ExtractContent()
      {
          try
          {
              FileInfo fileInfo = new FileInfo(&quot;22CC0001.23U&quot;);
              // Get the stream of the source file.
              using (FileStream inFile = fileInfo.OpenRead())
              {
                  // Get original file extension, for example
                  // &quot;doc&quot; from report.doc.gz.
                  string curFile = fileInfo.FullName;
                  string origName = curFile.Remove(curFile.Length -
                          fileInfo.Extension.Length);
                  //Create the decompressed file.
                  using (FileStream outFile = File.Create(origName))
                  {
                      using (DeflateStream decompressDeflateStream = new DeflateStream(inFile,
                              CompressionMode.Decompress))
                      {
                          // Copy the decompression stream 
                          // into the output file.
                          decompressDeflateStream.CopyTo(outFile);
  
                          Console.WriteLine(&quot;Decompressed: {0}&quot;, fileInfo.Name);
                      }
                  }
              }
          }
          catch (Exception e)
          {
              return &quot;errorelole&quot;;
          }
          return &quot;&quot;;
      }
  </code></pre>
  <p>Same &quot;Block length does not match with its complement&quot; error...</p>
  <p>One thing is that the extension of my file is not a &quot;.zip&quot; added at the end of a file, but another extension (.23U in that case).
  When I create a new file having the same extension (.23U instead of no extension in that case), the same problem occurs.</p>