繁体   English   中英

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

[英]Having trouble inflating a base64 string: zlib error: -3

我的应用程序在xml文件中以base64,zLib压缩字符串接收PDF。 至少这是我被告知的格式。它存储在数据库中,然后我需要从该字符串重新创建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

显然我使它变得太复杂了。 这是获取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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM