2012-02-08 107 views
0

我的应用程序接收PDF格式为x64文件中的base64,zLib压缩字符串。至少这是我告诉它的格式。它存储在数据库中,然后我需要从该字符串重新创建PDF。我创建了一个测试应用程序来弄清楚。下面的函数需要字符串,并且应该以解码的,充气的格式返回它,我相信我可以使用它来重建原始PDF(我还没有)。麻烦膨胀base64字符串:zlib错误:-3

我已经做了大量的研究,并找到了一些不同的库和方法来做到这一点,以及从发送给我的PDF作为例子的开发人员收到一个Java程序。但是我无法将字符串转换为可用的格式。使用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 

回答

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