[fpc-devel] zstream, fpc and lazarus

Daniël Mantione daniel.mantione at freepascal.org
Sun Dec 2 10:31:48 CET 2007



Op Sun, 2 Dec 2007, schreef Paul Ishenin:

> Daniël Mantione wrote:
> > Please copy/paste that help. I can't believe that one would demand that a
> > zstream starts from the start of a file; I know no file format which
> > works this way, not even gzip.
> > 
> > 
> delphi help:
> "You can call the Seek method with an Offset of zero, and the Origin set to
> soFromBeginning. This has the effect of resetting the input stream,
> effectively rewinding the stream back to the beginning."
> > How does it work in Lazarus? A PNG file does not start with a zstream, it
> > is in the middle of the file. If you seek to the start of a PNG file, you
> > will read PNG chunks rather than a Zstream.
> > 
> paste from fcl-image\src\fpreadpng.pp:
> 
> ZData := TMemoryStream.Create;
> try
> EndOfFile := false;
> while not EndOfFile do
> begin
> ReadChunk;
> HandleChunk;
> end;
> Decompress := TDecompressionStream.Create (ZData);
> try
> Decompress.position := 0;
> DoDecompress;
> finally
>  Decompress.Free;
> end;
> 
> 
> So it uses memory stream. It writes data to ZData memory stream, then creates
> TDecompressionStream. Do you see ZData.Position = 0 here? I dont. Code assume
> that TDecompressionStream.Create will do that. But TDecompressionStream skips
> that => we have crash.

Okay, that explains it. The memory stream is the only solution to seek in 
a sane way. Now we could switch to Delphi behaviour, but to be honest, I 
don't want to enforce the memory stream to everyone who wants to use a 
compressed stream; it should be usable on plain files.

I'm going to interpret the Delphi docs liberally; "the beginning" is the 
beginning of the zstream, not the beginning of the file. If you want to 
reset the source stream, you will have to reset the source stream :) Ok, 
it is a Delphi incompatibility, but really a corner case, and doing so has 
many advantages.

Daniël


More information about the fpc-devel mailing list