Created
January 21, 2012 16:43
-
-
Save satoruhiga/1653244 to your computer and use it in GitHub Desktop.
compress with zlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your code. That would be solved my issues.