1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
bool ( const std::string& compressedBytes, std::string& uncompressedBytes ) { if ( compressedBytes.size() == 0 ) { uncompressedBytes = compressedBytes ; return true ; }
uncompressedBytes.clear() ;
unsigned full_length = compressedBytes.size() ; unsigned half_length = compressedBytes.size() / 2;
unsigned uncompLength = full_length ; char* uncomp = (char*) calloc( sizeof(char), uncompLength );
z_stream strm; strm.next_in = (Bytef *) compressedBytes.c_str(); strm.avail_in = compressedBytes.size() ; strm.total_out = 0; strm.zalloc = Z_NULL; strm.zfree = Z_NULL;
bool done = false ;
if (inflateInit2(&strm, -MAX_WBITS) != Z_OK) { free( uncomp ); return false; }
while (!done) { if (strm.total_out >= uncompLength ) { char* uncomp2 = (char*) calloc( sizeof(char), uncompLength + half_length ); memcpy( uncomp2, uncomp, uncompLength ); uncompLength += half_length ; free( uncomp ); uncomp = uncomp2 ; }
strm.next_out = (Bytef *) (uncomp + strm.total_out); strm.avail_out = uncompLength - strm.total_out;
int err = inflate (&strm, Z_SYNC_FLUSH); if (err == Z_STREAM_END) done = true; else if (err != Z_OK) { break; } }
if (inflateEnd (&strm) != Z_OK) { free( uncomp ); return false; }
for ( size_t i=0; i<strm.total_out; ++i ) { uncompressedBytes += uncomp[ i ]; } free( uncomp ); return true ; }
|
近期评论