Re: [bds 2006] Decompress .gz files with zlib unit?



On Tue, 24 Jan 2006 23:16:16 -0500, "Richard A. DeVenezia"
<rdevenezia@xxxxxxxxxxxx> wrote:
[snipped]
>>From what I understand (zlib.h), *Init is for zlib wrapped and the *Init2 is
>for gzip wrapped. That being I would presume to want to do something like
>[ note the ,12 -- the second form of the constructor that uses
>InflateInit2 ]
>------------------------
> bufferSize := 1000;
> GetMem (pBuffer, bufferSize);
>
> z := TZDecompressionStream.Create(TFileStream.Create(gzfile,
>fmOpenRead),12);
>
> bytesRead := z.Read(pBuffer^, bufferSize);
>
> z.Free();
>
> WriteLn (bytesRead, ' bytes read.');
> WriteLn (String(pBuffer^));
>
> FreeMem(pBuffer, bufferSize);
>
> WriteLn('Done.');
> ReadLn;
>------------------------
>
>Unfortunately, this raises a Data Error exception.
>
>

I guess the DATA ERROR exception results from not recognizing a gzip
stream, here a snippet for inflateInit2 from zlib.h

<quote>
windowBits can also be greater than 15 for optional gzip decoding. Add
32 to windowBits to enable zlib and gzip decoding with automatic
header detection, or add 16 to decode only the gzip format (the zlib
format will return a Z_DATA_ERROR). If a gzip stream is being
decoded, strm->adler is a crc32 instead of an adler32.
<quote>

So instead of

z := TZDecompressionStream.Create(TFileStream.Create(gzfile,
fmOpenRead),12);

you should try

z := TZDecompressionStream.Create(TFileStream.Create(gzfile,
fmOpenRead),12+32);

or

z := TZDecompressionStream.Create(TFileStream.Create(gzfile,
fmOpenRead),12+16);

BTW: Why do you use windowbit=12 and not the default (15)? The memory
load is somewhat larger(no problem with Delphi), but

<quote>
The memory requirements for decompression depend only on windowBits,
but this is, in a sense, a harsher limitation: whereas data streams
compressed with a smaller window will merely be a bit larger than they
would have otherwise, a reduced window size for decompression means
that streams compressed with larger windows cannot be decompressed at
all.
</quote>

Hope that helps
Wolgang

--
In order to e-mail me a reply to this message, you will have
to remove PLEASE.REMOVE from the address shown in the header
or get it from http://home.netsurf.de/wolfgang.ehrhardt
(Free AES, CRC, Hash, and HMAC source for Pascal/Delphi)
.



Relevant Pages