0

I've searched many forums and blogs before posting the question. I've found samples in python and VB which are using ZLib. but I can't get it to work in Delphi.

I have the stream from a pdf that is encoded with FlateDecode.

Here is the stream saved as a simple file named "compressed_stream.pdf" (in fact it's not pdf - it's only the stream, but I just left the .pdf file extension) https://files.fm/u/epka2hxz

Here is my code: Execution goes to System.Zlib.ZDecompressStream(streamIn, streamOut); and just sleeps... no errors, no crashes, nothing - just sleeps until I break the execution.

Any idea?

var
  fs: TFileStream;
  streamIn, streamOut: TMemoryStream;
begin
  fs := TFileStream.Create(sDocumentFolder + 'compressed_stream.pdf', fmOpenRead);

  streamIn := TMemoryStream.Create();
  streamOut := TMemoryStream.Create();

  streamIn.CopyFrom(fs, 0);

  streamIn.Position := 0;
  System.Zlib.ZDecompressStream(streamIn, streamOut);

end;
Josef Švejk
  • 1,047
  • 2
  • 12
  • 23
Ivan Peshev
  • 435
  • 7
  • 16
  • Have you checked that what you have really is the right thing, that you've extracted the stream correctly? Have you tried using other tools to do the work? I think there are good Python libraries for this. – David Heffernan Nov 28 '19 at 14:24
  • 1
    Your code really doesn't work, but using instance of `TZDecompressionStream` to decompress supplied file works very well. At least, some kind of data was extracted. Something that is similar to PDF, but not PDF at all. You would better encode picture or text to get *real* results of decoding. – Josef Švejk Nov 28 '19 at 14:26
  • David Hefferman, Dima, I've uploaded the whole pdf file in the same folder https://files.fm/u/epka2hxz - my_test.pdf. It's a very simple file with some text and a table. so the extracted file contains the data between "stream" and "endstream" of the "4 0 obj". Dima, would you show me your code with TZDecompressionStream ? I'll try it myself in the mean time. Thanks. – Ivan Peshev Nov 29 '19 at 08:09
  • 1
    Just want to say: Dima, thank you very much!! :) It works with TZDecompressionStream. I cannot upvote you comment because of a double-click by mistake. – Ivan Peshev Nov 29 '19 at 08:23
  • For backgrounds: The content of PDF streams with FlateDecode filter actually is presented in the ZLIB Compressed Data Format (as per RFC 1950) wrapping FLATE compressed data. Probably `System.Zlib.ZDecompressStream` actually expects naked FLATE compressed streams while `TZDecompressionStream` understands data in ZLIB Compressed Data Format. Also see [this answer](https://stackoverflow.com/a/58199465/1729265). – mkl Nov 29 '19 at 10:41

1 Answers1

1

Thanks to Dima I quickly found a sample for TZDecomoressionStream: https://forum.lazarus.freepascal.org/index.php?topic=33009.0

function ZDecompressString(aText: string): string;
var
  strInput,
  strOutput: TStringStream;
  Unzipper: TZDecompressionStream;
begin
  Result:= '';
  strInput:= TStringStream.Create(aText);
  strOutput:= TStringStream.Create;
  try
    Unzipper:= TZDecompressionStream.Create(strInput);
    try
      strOutput.CopyFrom(Unzipper, Unzipper.Size);
    finally
      Unzipper.Free;
    end;
    Result:= strOutput.DataString;
  finally
    strInput.Free;
    strOutput.Free;
  end;
end;
Ivan Peshev
  • 435
  • 7
  • 16