title: Can't decompress data with GZipStream without running into exceptions, can't determine cause
body: <p>In a Blazor WebAssembly project I'm working on, I am compressing data at a back-end WebAPI server and passing it up to the front-end Blazor server (running Blazor WebAssembly, not &quot;Blazor Server,&quot; yay for confusing terminology), to be delivered to the user who then decompresses it. I'm running into the following exception:</p>
  <blockquote>
  <p>System.IO.IOException: Corrupted data ReadInternal at
  System.IO.Compression.DeflateStreamNative.CheckResult (System.Int32
  result, System.String where) &lt;0x34fa860 + 0x000f0&gt; in :0 at System.IO.Compression.DeflateStreamNative.ReadZStream
  (System.IntPtr buffer, System.Int32 length) &lt;0x34f9c60 + 0x00024&gt; in
  :0 at
  System.IO.Compression.DeflateStream.ReadInternal (System.Byte[] array,
  System.Int32 offset, System.Int32 count) &lt;0x34f9a90 + 0x00066&gt; in
  :0 at System.IO.Compression.DeflateStream.Read
  (System.Byte[] array, System.Int32 offset, System.Int32 count)
  &lt;0x34f96e8 + 0x000cc&gt; in :0 at
  System.IO.Compression.GZipStream.Read (System.Byte[] array,
  System.Int32 offset, System.Int32 count) &lt;0x34f33b0 + 0x0001e&gt; in
  :0 at
  OptionalyticsFrontend.Shared.Services.Compression.DecompressString
  (System.Byte[] input) [0x0001b] in
  C:\Users\codef\source\repos\OptionalyticsFrontend\OptionalyticsFrontend\Shared\Services\Compression.cs:24
  at OptionalyticsFrontend.Client.Pages.Application.GetFilteredOptions
  () [0x00183] in
  C:\Users\codef\source\repos\OptionalyticsFrontend\OptionalyticsFrontend\Client\Pages\Application.razor:310</p>
  </blockquote>
  <p>I've looked at the specified lines over and over, and looked at Google and Stack Overflow for a while, and can't figure it out. The following are the compression/decompression methods used on both back and front end:</p>
  <pre><code>public static string DecompressString(this byte[] input)
  {
      var result = new byte[input.Length];
      using (var source = new MemoryStream(input))
      {
          using (var decompressionStream = new GZipStream(source,
              CompressionMode.Decompress))
          {
              decompressionStream.Read(result, 0, input.Length); // &lt;---------- THIS IS THE ERROR LINE
          }
      }
      return Encoding.UTF8.GetString(result);
  }
  
  public static byte[] CompressString(this string inputString)
  {
      byte[] input = Encoding.UTF8.GetBytes(inputString);
      using (var result = new MemoryStream())
      {
          using (var compressionStream = new GZipStream(result,
              CompressionMode.Compress))
          {
              compressionStream.Write(input, 0, input.Length);
              compressionStream.Flush();
  
          }
          return result.ToArray();
      }
  }
  </code></pre>
  <p>The following line is where the decompression is called, in my Razor component:</p>
  <pre><code>var responseString = OptionalyticsFrontend.Shared.Services.Compression.DecompressString(responseBytes);
  </code></pre>
  <p>I have looked at, and tried implementing, the following Stack Overflow answers, and none of them worked, I still get the same exception every time.</p>
  <p><a href="https://stackoverflow.com/questions/271260/gzipstream-and-deflatestream-will-not-decompress-all-bytes">GZipStream And DeflateStream will not decompress all bytes</a></p>
  <p><a href="https://stackoverflow.com/questions/38902654/gzipstream-does-gzipping-but-ungzipping-file-end-up-with-unexpected-end-of-data">GZipStream does gzipping but ungzipping file end up with &quot;Unexpected end of data&quot;</a></p>
  <p><a href="https://stackoverflow.com/questions/1327997/gzipstream-zip-file-invalid-or-corrupted">GZipStream zip file invalid or corrupted</a></p>
  <p><a href="https://stackoverflow.com/questions/25134897/gzip-compression-and-decompression-in-c-sharp">Gzip compression and decompression in C#</a></p>
  <p>What is going on here? I am running the front-end Blazor server as .NET Standard and .NET Core (the actual client and shared codes are .NET Standard, server-side is .NET Core)</p>
  <p>EDIT: As Andy's comment asked, I added a new stream and tried copying to the new stream, but this results in the same error.</p>
  <pre><code>public static string DecompressString(byte[] input)
  {
      var result = new byte[input.Length];
      using (var newStream = new MemoryStream())
      {
          using (var source = new MemoryStream(input))
          {
              using (var decompressionStream = new GZipStream(source,
                  CompressionMode.Decompress))
              {
                  decompressionStream.CopyTo(newStream);
              }
          }
          newStream.Position = 0;
          return Encoding.UTF8.GetString(newStream.ToArray());
      }
  }
  </code></pre>