[Lazarus] GZip issues

Rik van Kekem rik at graficalc.nl
Mon Nov 16 12:11:48 CET 2015


Werner Pamler wrote:
> Another question: I would prefer to avoid Abbrevia altogether and use 
> the standard zip support in fpc. There are examples in the wiki on how 
> to use the TDecompressionStream for inflating gzip files. But I always 
> get a "data error"at least with the mailinglist archive files from 
> http://lists.lazarus.freepascal.org/pipermail/lazarus/. If I use a 
> TGZFileStream instead of the TDecompressionStream by a TGZFileStream, 
> then the file decompresses fine. But this is not a good solution as I 
> would have to use temporary files which I want to avoid. How can the 
> TGZFileStream be modified such that it accepts a stream as input 
> parameter instead of a filename?
It should be possible to use inflateInit2 from paszlib.
I've found a snippet here: http://www.gocher.me/GZIP

If you take GZIPUtils.pas, at first you think it should work, but it 
didn't. The only thing I found was that that code only takes a header of 
9 bytes into account while the 2015-November.txt.gz has a header of 71 
bytes. So if I change the following in ZUncompressStream I could 
successfully extract 2015-November.txt:

On line 178:
     // inStream.Position := 10; // jump over header
     inStream.Position := 72; // jump over header, this needs to be 
adjusted to read a string until #0

========
procedure TForm1.Button1Click(Sender: TObject);
var
   inStream, outStream: TMemoryStream;
begin
   inStream := TMemoryStream.Create;
   outStream := TMemoryStream.Create;
   try
     inStream.LoadFromFile('c:\temp\2015-November.txt.gz');
     if ZUncompressStream(inStream, outStream) then
     begin
       outStream.SaveToFile('c:\temp\2015-November.txt');
     end;
   finally
     inStream.Free;
     outStream.Free;
   end;
end;
========
Of course in your final code you should read a string until #0 from the 
file and position it correctly to begin at the real data.

You could also strip the deflate-part of that unit (or completely create 
your own implementation from this source). It's not much code after you 
strip all the things you don't need.

Grtz,
/Rik/ (rvk on forum)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20151116/e1cb5264/attachment-0003.html>


More information about the Lazarus mailing list