Nick Nick - 2 months ago 9
C Question

can I reuse zlib's z_stream for several operations?

I am currently playing with

zlib
.

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);

deflateEnd(&zs);

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
z_stream
and
deflateInit
?

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

Answer

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

Comments