title: Decompressing a base 64 encoded GZip with Powershell
body: <p>I have the following python code which works:</p>
  
  <pre><code>import sys
  import base64
  import gzip
  from io import BytesIO
  
  def deserialize(encoded_string): # decode and decompress the base64 string
    compressed_json = base64.b64decode(encoded_string)
    compressedFile = BytesIO()
    compressedFile.write(compressed_json)
    compressedFile.seek(4)
  
    decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='r')
    decompressed_json = decompressedFile.read()
    return decompressed_json
  </code></pre>
  
  <p>My task is simple and yet, for some reason, daunting...
  I need to rewrite the code above so that it would work in Powershell.</p>
  
  <p>So far, I've gotten as far as the following:</p>
  
  <pre><code>$input = '&lt;some_base64_encoded_string_here&gt;'    
  $b64 = [System.Convert]::FromBase64String($input)
  $ms = New-Object IO.MemoryStream(,$b64)
  $gzs = New-Object IO.Compression.GzipStream($ms, [IO.Compression.CompressionMode]::Decompress)
  $sr = New-Object IO.StreamReader ($gzs, [Text.Encoding]::UTF8)
  $decoded = $sr.ReadToEnd();
  Write-Host $decoded
  </code></pre>
  
  <p>But I keep getting an error in the line that creates the IO.StreamReader object:</p>
  
  <pre><code>Exception calling "ReadToEnd" with "0" argument(s): "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."
  At line:6 char:1
  + $decoded = $sr.ReadToEnd();
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : InvalidDataException
  </code></pre>
  
  <p>I have no idea why this is happening...</p>
  
  <p>The string I'm trying to decode is valid because the same string passes with flying colors in the python script.</p>
  
  <p>Does anyone have any idea what I'm doing wrong? Any help would be greatly appreciated.</p>
  
  <p>Thanks!</p>