无法膨胀base64字符串:zlib错误:-3

时间:2012-02-08 17:18:38

标签: vb.net zlib

我的应用程序在xml文件中收到PDF作为base64,zLib缩小字符串。至少那是我告诉它的格式。它存储在数据库中,然后我需要从该字符串重新创建PDF。我创建了一个测试应用程序来解决它。下面的函数接受字符串,并且应该以解码的,膨胀的格式返回它,我相信我将能够用它来重建原始PDF(我还没有)。

我做了很多研究,发现了一些不同的库和方法,以及从开发人员那里收到一个java程序,该程序正在向我发送PDF用作示例。但是我无法将字符串转换为可用的格式。使用ManagedZLib.dll和下面的函数似乎让我最接近。据调试我可以看出,一切正常,直到我尝试解压缩:

zStream.Read(decompressedBytes, 0, decodedBytes.Length - 1)

这会产生“zLib错误:-3”。我可以在该错误上找到的唯一信息是“数据错误”。网上关于它的信息非常少。

非常感谢任何帮助我们克服这个错误,或者想出一个不同/更好的方法来实现我的最终目标。

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

1 个答案:

答案 0 :(得分:0)

显然我太复杂了。这是获取base64缩小字符串,解码它,然后对其进行充气并从中输出PDF的最终解决方案。如果需要,可以很容易地调整它以返回结果字符串。我最终得到了更好的结果,不包括任何库(本来应该更容易:-))。我实际上并没有找到我收到的错误的答案,只是假设该库不是为我的目的而设计的。使用内置的.net类做得更好(Convert.FromBase64String和System.IO.Compression.DeflateStream)。

我希望这有助于未来的某些人,因为我无法在任何地方找到这样的例子。

Imports System.IO
Imports System.IO.Compression
Imports System.Text

    Public Sub DecompressString(ByVal origString As String)

    Dim decodedDeflatedBytes() As Byte = Nothing
    Dim decodedInflatedBytes() As Byte = Nothing

    'parse the string into a decoded byte array
    decodedDeflatedBytes = Convert.FromBase64String(origString)

    'once around the block to get the length of the buffer we'll need
    Dim decompressedBufferLength As Integer = 0
    Using ms1 As New MemoryStream(decodedDeflatedBytes)
        Using dStream1 As System.IO.Compression.DeflateStream = New System.IO.Compression.DeflateStream(ms1, Compression.CompressionMode.Decompress)
            While dStream1.ReadByte <> -1  ' -1 indicates nothing left to read
                decompressedBufferLength += 1
            End While
        End Using
    End Using

    'a second time around the block to do the actual inflation now that we have the length
    Using ms2 As New MemoryStream(decodedDeflatedBytes)
        Using dStream2 As System.IO.Compression.DeflateStream = New System.IO.Compression.DeflateStream(ms2, Compression.CompressionMode.Decompress)
            ReDim decodedInflatedBytes(decompressedBufferLength - 1) '11711
            dStream2.Read(decodedInflatedBytes, 0, decompressedBufferLength) '11712
        End Using
    End Using

    'output the PDF with a 'save as' prompt
    Response.ClearContent()
    Response.ClearHeaders()
    Response.Clear()
    Response.ContentType = "Application/pdf"
    Response.AddHeader("Content-Length", decodedInflatedBytes.Length.ToString)
    Response.AddHeader("content-disposition", "attachment;filename=YourReport.pdf")
    Response.BinaryWrite(decodedInflatedBytes)
    Response.End()

End Sub