Skip to content

Instantly share code, notes, and snippets.

@tekladis
Created July 8, 2017 15:40
Show Gist options
  • Select an option

  • Save tekladis/8055dd97ae66b5757c5085497a139bbe to your computer and use it in GitHub Desktop.

Select an option

Save tekladis/8055dd97ae66b5757c5085497a139bbe to your computer and use it in GitHub Desktop.
Simple ZLib function for C++
/*
Uses vectors for both input and output... Just a reminder to myself of how to use zlib properly in the future.
*/
static inline long int decompress(std::vector<uint8_t>& in, std::vector<uint8_t> &out)
{
auto ret = Z_OK;
const size_t BUFSIZE = 1024 * 64;
uint8_t chunk[BUFSIZE];
z_stream zs = {Z_NULL};
zs.next_in = in.data();
zs.avail_in = in.size();
inflateInit2(&zs, -15);
do {
zs.next_out = chunk;
zs.avail_out = BUFSIZE;
ret = inflate(&zs, Z_NO_FLUSH);
if (zs.avail_out < BUFSIZE) {
out.insert(out.end(), chunk, chunk + BUFSIZE - zs.avail_out);
}
} while (ret == Z_OK);
inflateEnd(&zs);
return (ret != Z_STREAM_END ? -1 : zs.total_out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment