www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Trouble with std.zlib

reply Andrew Wiley <wiley.andrew.j gmail.com> writes:
I'm using std.zlib in an application, and I'm having trouble decompressing a
file. The file is gzipped, and I can easily see the contents with `cat file
| gzip -d` but this code doesn't work:
auto arr = read("file.gz");
ubyte[] realarr = cast(ubyte[])uncompress(arr);

output:
std.zlib.ZlibException std/zlib.d(59): data error

From the header file, it seems like zlib should be detecting a gzip header
and *just working*, but, well, it isn't. I have to store data in a compressed format, and eventually the code I'm writing will be used in-memory instead of on files, so I'm going to have to uncompress it somehow. Has anyone encountered this before? Thanks, Andrew
Jul 19 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrew Wiley Wrote:

 I'm using std.zlib in an application, and I'm having trouble decompressing a
 file. The file is gzipped, and I can easily see the contents with `cat file
 | gzip -d` but this code doesn't work:
 auto arr = read("file.gz");
 ubyte[] realarr = cast(ubyte[])uncompress(arr);
The zlib format is different from the gzip format. http://www.zlib.net/zlib_faq.html#faq18 One is made for file storage and the other internal data transfer. D does not provide a wrapper to the gzip functions at this time but they are available in std.c.zlib (with the C interface).
Jul 19 2011
parent reply Andrew Wiley <wiley.andrew.j gmail.com> writes:
On Tue, Jul 19, 2011 at 7:11 AM, Jesse Phillips
<jessekphillips+D gmail.com>wrote:

 Andrew Wiley Wrote:

 I'm using std.zlib in an application, and I'm having trouble
decompressing a
 file. The file is gzipped, and I can easily see the contents with `cat
file
 | gzip -d` but this code doesn't work:
 auto arr = read("file.gz");
 ubyte[] realarr = cast(ubyte[])uncompress(arr);
The zlib format is different from the gzip format. http://www.zlib.net/zlib_faq.html#faq18 One is made for file storage and the other internal data transfer. D does not provide a wrapper to the gzip functions at this time but they are available in std.c.zlib (with the C interface).
I know, but if you look at the docs for the inflate functions, zlib is supposed to be autodetecting the gzip header and uncompressing the payload correctly, if I understand the documentation (which I probably don't at this point).
Jul 19 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrew Wiley Wrote:

 I know, but if you look at the docs for the inflate functions, zlib is
 supposed to be autodetecting the gzip header and uncompressing the payload
 correctly, if I understand the documentation (which I probably don't at this
 point).
Not sure what documentation you are talking of. But it doesn't sound like it does auto detection, an answer to one of the question: "You can request that deflate write the gzip format instead of the zlib format using deflateInit2(). You can also request that inflate decode the gzip format using inflateInit2(). Read zlib.h for more details."
Jul 19 2011
next sibling parent Andrew Wiley <wiley.andrew.j gmail.com> writes:
On Tue, Jul 19, 2011 at 9:57 AM, Jesse Phillips
<jessekphillips+D gmail.com>wrote:

 Andrew Wiley Wrote:

 I know, but if you look at the docs for the inflate functions, zlib is
 supposed to be autodetecting the gzip header and uncompressing the
payload
 correctly, if I understand the documentation (which I probably don't at
this
 point).
Not sure what documentation you are talking of. But it doesn't sound like it does auto detection, an answer to one of the question: "You can request that deflate write the gzip format instead of the zlib format using deflateInit2(). You can also request that inflate decode the gzip format using inflateInit2(). Read zlib.h for more details."
Ah, I stand corrected: "inflate() can decompress and check either zlib-wrapped or gzip-wrapped deflate data. The header type is detected automatically, if requested when initializing with inflateInit2()."
Jul 19 2011
prev sibling parent Andrew Wiley <wiley.andrew.j gmail.com> writes:
On Tue, Jul 19, 2011 at 10:14 AM, Andrew Wiley <wiley.andrew.j gmail.com>wrote:

 On Tue, Jul 19, 2011 at 9:57 AM, Jesse Phillips <
 jessekphillips+D gmail.com> wrote:

 Andrew Wiley Wrote:

 I know, but if you look at the docs for the inflate functions, zlib is
 supposed to be autodetecting the gzip header and uncompressing the
payload
 correctly, if I understand the documentation (which I probably don't at
this
 point).
Not sure what documentation you are talking of. But it doesn't sound like it does auto detection, an answer to one of the question: "You can request that deflate write the gzip format instead of the zlib format using deflateInit2(). You can also request that inflate decode the gzip format using inflateInit2(). Read zlib.h for more details."
Ah, I stand corrected: "inflate() can decompress and check either zlib-wrapped or gzip-wrapped deflate data. The header type is detected automatically, if requested when initializing with inflateInit2()."
Looks like this is the solution: ubyte[] realarr = cast(ubyte[])uncompress(arr, 0, 47); The 47 makes zlib autodetect the header (because the 32 bit is set; if you wanted to force gzip, you'd set the 16 bit).
Jul 19 2011