2012-02-08 37 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