2012-02-08 2 views
0

내 응용 프로그램은 PDF 파일을 XML 파일로 base64, zLib 압축 문자열로 수신합니다. 적어도 그것이 내가 말한 형식입니다. 데이터베이스에 저장되면 그 문자열에서 PDF를 다시 만들어야합니다. 내가 알아낼 수있는 테스트 응용 프로그램을 만들었습니다. 아래의 함수는 문자열을 받아서 원래의 PDF를 재구성하는 데 사용할 수있을 것으로 믿어지는 디코딩 된, 부풀려진 형식으로 반환 할 예정입니다 (아직 없습니다).base64 문자열을 확장하는 데 문제가 있습니다. zlib 오류 : -3

나는 많은 연구를했으며 몇 가지 다른 라이브러리와이를 수행하는 방법을 찾았으며 개발자에게 자바 프로그램을 보내 주었다. 그러나 사용할 수있는 형식으로 문자열을 가져올 수 없습니다. 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 
관련 문제