izaak_pyzaak izaak_pyzaak - 3 months ago 5
C++ Question

zlib gzopen() returns a compressed file stream. Does it decompress the file?

zlib

gzopen()
returns a compressed file stream.

gzFile data_file;
data_file = gzopen(filename.c_str(), "r");


where
data_file
is the compressed file stream.
I may have missed this from zlib docs. But, does this decompress the opened file?
Or is
gzopen()
able to directly parse the gzipped file without the need
to decompress it?

Thanks in advance!

Answer

gzopen doesn't decompress the data, gzread decompresses as you read it.

I haven't found a simple statement of this fact in the zlib docs, but if you want to "prove" it to yourself create a large (several GB) compressed file, and then measure how quickly gzopen returns. It "obviously" doesn't take the time required to decompress the whole file. If you look into how gzip compression is defined, you'll see that it's designed to be written and read as a stream, that is to say you don't need to decompress the whole file at once.

Comments