简体   繁体   English

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

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

My app receives a PDF as a base64, zLib deflated string in an xml file. 我的应用程序在xml文件中以base64,zLib压缩字符串接收PDF。 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. 至少这是我被告知的格式。它存储在数据库中,然后我需要从该字符串重新创建PDF。 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). 下面的函数接受字符串,并应该以解码后的夸张格式返回它,我相信我将能够使用它来重建原始PDF(我还没有在那里)。

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. 我进行了大量研究,发现了一些不同的库和方法来执行此操作,并从开发人员那里收到了一个Java程序,该程序向我发送了PDF以用作示例。 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. 使用ManagedZLib.dll和下面的功能似乎最接近我。 As far as I can tell from debugging, everything works until I try to decompress: 据调试显示,一切工作直到我尝试解压缩为止:

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

This produces a "zLib error: -3". 这将产生“ zLib错误:-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. 网络上几乎没有其他信息。

Any help getting past this error, or thoughts on a different/better approach to accomplish my end goal is greatly appreciated. 非常感谢您提供的克服此错误的帮助,或对使用其他/更好方法实现最终目标的想法。

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

Apparently I was making it too complicated. 显然我使它变得太复杂了。 Here is the final solution for taking a base64 deflated string, decoding it, then inflating it and outputting a PDF from it. 这是获取base64放气字符串,对其进行解码,然后对其进行充气并从中输出PDF的最终解决方案。 It could easily be tweaked to return the resulting string if needed. 如果需要,可以很容易地对其进行调整以返回结果字符串。 I ended up having much better results by not including any libraries (that were supposed to make it easier :-) ). 通过不包含任何库(应该使它更容易:-)),我最终获得了更好的结果。 I didn't actually find an answer to the error I was receiving and only assume that library wasn't designed for my purposes. 我实际上没有找到我收到的错误的答案,只是假设库不是为我的目的而设计的。 Using the built in .net classes did the trick much better (Convert.FromBase64String and System.IO.Compression.DeflateStream). 使用内置的.net类效果更好(Convert.FromBase64String和System.IO.Compression.DeflateStream)。

I hope this helps someone in the future as I couldn't find an example of this anywhere. 我希望这对以后的人有所帮助,因为我在任何地方都找不到这样的例子。

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