簡體   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