开发者

uzing libgz to inflate a gz input

开发者 https://www.devze.com 2023-01-03 03:15 出处:网络
I\'m currently trying to 开发者_开发技巧use the zlib to inflate a source of gzipped data. It seems that the inflate API in zlib cannot inflate a gzipped data ( The example http://www.zlib.net/zpipe.c

I'm currently trying to 开发者_开发技巧use the zlib to inflate a source of gzipped data.

It seems that the inflate API in zlib cannot inflate a gzipped data ( The example http://www.zlib.net/zpipe.c fails to read a gzipped file: "zpipe: invalid or incomplete deflate data" ). I noticed that there is a gzopen function in this API, but , as far as I understand, it only works with a filename or a file descriptor.

Can I use this API if my source of gzipped data is stored in memory, in a sql blob, etc... ?

Many Thanks

Pierre


You can open memory locations as files using the fmemopen function and then pass that file descriptor to the gzopen function.


The gzip format just adds a simple header (and trailer) to a zlib compressed stream. Skipping over the header isn't difficult; the format is documented in RFC 1952.


As another solution, there's gzdopen - which takes a file descriptor. You can obtain one to read memory with pipe(). You can then use some form of non-blocking file descriptors, or an auxiliary thread to read in data.

You may find this more trouble than it's worth: Matthew Slattery's solution may very well be more viable.


zlib.h has an API for decompressing/compressing in-memory buffers of data, inflateInit2() allows you to add 16 to the recommended windowBits value of 15 to decompress gzipped data buffers. Unfortunately there is no API for reading out the expected number of decompressed bytes from the 8 byte gzip trailer (4 byte Fletcher-32 CRC, 4 byte size of decompressed data in bytes). As far as I can tell you have to do that yourself, or make a guess and then resize your output buffer if inflate() returns with Z_BUF_ERROR (not enough room in the passed in output buffer). If you have the entire input buffer in memory use the Z_FINISH flush flag to inflate(). If you have enough output memory it will return Z_STREAM_END, if not it returns Z_BUF_ERROR and you can reallocate your output buffer and continue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号