Skip to content

Instantly share code, notes, and snippets.

@satoruhiga
Created January 21, 2012 16:43
Show Gist options
  • Select an option

  • Save satoruhiga/1653244 to your computer and use it in GitHub Desktop.

Select an option

Save satoruhiga/1653244 to your computer and use it in GitHub Desktop.
compress with zlib
#include <zlib.h>
bool zcompress(const string& input, string& output)
{
z_stream z;
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;
output.resize(input.size() * 1.01 + 12);
int status = Z_OK;
status = deflateInit(&z, Z_DEFAULT_COMPRESSION);
if(status != Z_OK)
{
return -1;
}
z.total_out = 0;
z.next_in = (Bytef*)input.data();
z.avail_in = input.size();
do
{
z.next_out = (Bytef*)output.data() + z.total_out;
z.avail_out = output.size() - z.total_out;
status = deflate(&z, Z_FINISH);
}
while (status == Z_OK);
output.resize(z.total_out);
deflateEnd(&z);
}
@VictorZhang2014
Copy link

Thank you for your code. That would be solved my issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment