title: Having trouble inflating a base64 string: zlib error: -3
body: <p>My app receives a PDF as a base64, zLib deflated string in an xml file. At least that's the format I'm told it is in. It gets stored in a database, then I need to recreate the PDF from that string.  I created a test app to figure it out.  The function below takes the string and is supposed to return it in a decoded, inflated format which I believe I'll be able to use to rebuild the original PDF (I'm not there yet).   </p>
  
  <p>I've done lots of research and found a few different libraries and ways to do this as well as received a java program from the developer who is sending me the PDF to use as an example.  However I can not get the string to a usable format.  Using the ManagedZLib.dll and the function below seems to get me the closest.  As far as I can tell from debugging, everything works until I try to decompress:</p>
  
  <pre><code>zStream.Read(decompressedBytes, 0, decodedBytes.Length - 1)
  </code></pre>
  
  <p>This produces a "zLib error: -3".  The only info I can find on that error is it is a 'data error'.  There is very little other information on the web about it.</p>
  
  <p>Any help getting past this error, or thoughts on a different/better approach to accomplish my end goal is greatly appreciated.</p>
  
  <pre><code>Public Function DecompressString4(ByVal origString As String) As String
  
      Dim returnString = Nothing
      ' get the base64 content into String
      ManagedZLib.ManagedZLib.Initialize()
  
      '// parse the string into a byte array
      Dim b64bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(origString)
      Dim decodedBytes() As Byte = Nothing
  
      'decode the byte array into another byte array, but this time of Base 64.
      Using ms As New MemoryStream(b64bytes)
          Using zStream As New ManagedZLib.Base64Stream(ms, Base64Options.Decode)
              ReDim decodedBytes(b64bytes.Length)
              zStream.Read(decodedBytes, 0, b64bytes.Length)
          End Using
      End Using
  
      decmpStrTxtBox.Text = Convert.ToString(decodedBytes)
  
      Dim decompressedBytes() As Byte = Nothing
  
      ' inflate the base64 array
      Using ms2 As New MemoryStream(decodedBytes)
          Using zStream As New ManagedZLib.CompressionStream(ms2, CompressionOptions.Decompress)
              'ReDim decompressedBytes(origString.Length)
              ReDim decompressedBytes(decodedBytes.Length)
              zStream.Read(decompressedBytes, 0, decodedBytes.Length - 1)
          End Using
      End Using
  
      'write output to a stream
      returnString = Convert.ToString(decompressedBytes)
      ManagedZLib.ManagedZLib.Terminate()
  
      Return returnString
  
  End Function
  </code></pre>