title: Using Gzip to compress/decompress an array of bytes
body: <p>I need to compress an array of bytes. So I wrote this snippet :</p>
  
  <pre><code> class Program
      {
          static void Main()
          {
              var test = "foo bar baz";
  
              var compressed = Compress(Encoding.UTF8.GetBytes(test));
              var decompressed = Decompress(compressed);
              Console.WriteLine("size of initial table = " + test.Length);
              Console.WriteLine("size of compressed table = " + compressed.Length);
              Console.WriteLine("size of  decompressed table = " + decompressed.Length);
              Console.WriteLine(Encoding.UTF8.GetString(decompressed));
              Console.ReadKey();
          }
  
          static byte[] Compress(byte[] data)
          {
              using (var compressedStream = new MemoryStream())
              using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
              {
                  zipStream.Write(data, 0, data.Length);
                  zipStream.Close();
                  return compressedStream.ToArray();
              }
          }
  
          static byte[] Decompress(byte[] data)
          {
              using (var compressedStream = new MemoryStream(data))
              using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
              using (var resultStream = new MemoryStream())
              {
                  zipStream.CopyTo(resultStream);
                  return resultStream.ToArray();
              }
          }
      }
  </code></pre>
  
  <p>The problem is that I get this output :</p>
  
  <p><a href="https://i.stack.imgur.com/xFGQs.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/xFGQs.jpg" alt="output"></a></p>
  
  <p>I don't understand why the size of the compressed array is greater than the decompressed one !</p>
  
  <p>Any ideas?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>after @spender's comment: if I change <code>test</code> string for example :</p>
  
  <pre><code>var test = "foo bar baz very long string for example hdgfgfhfghfghfghfghfghfghfghfghfghfghfhg";
  </code></pre>
  
  <p>I get different result. So what is the minimum size of the initial array to be compressed ? </p>