Nick Nick - 1 year ago 56
C Question

can I reuse zlib's z_stream for several operations?

I am currently playing with


Usual example is more or less as follows (C/C++ pseudo code)

z_stream zs;
memset(&zs, 0, sizeof(zs));

if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
return false;

zs.next_in = input_data;
zs.avail_in = input_size;

zs.next_out = output_data;
zs.avail_out = output_size;

int result = deflate(&zs, Z_FINISH);


return result == Z_STREAM_END ? true : false;

Using valgrind I see deflateInit and memset are not entirely "free" operations, but takes considerable time.

My question is, can I reuse

e.g. can I deflate more than once by just updating the buffers?

Answer Source

Yes. You can use deflateReset() to have the same effect as deflateEnd() followed by deflateInit(), but entirely avoiding the costly de- and re-allocations.