Monday, August 27, 2012

boost and zlib



char *data="test 1\0test 2\0test3";
std::ostringstream compressedData;//hold compressed data
boost::iostreams::filtering_ostreambuf txdat; //compress stream
txdat.push(boost::iostreams::zlib_compressor()); //init compress stream
txdat.push(compressedData);//init compress stream
 
boost::iostreams::copy(boost::iostreams::basic_array_source(data, 20), txdat); //pass data to compress stream

string compressedStr=compressedData.rdbuf()->str();//return as string?

std::istringstream rxCompressedData(compressedStr, compressedStr.length());//compressed string
boost::iostreams::filtering_istreambuf rxdat; //decompress stream
rxdat.push(boost::iostreams::zlib_decompressor());//init decompress stream
rxdat.push(rxCompressedData);//init decompress stream

std::ostringstream decompressedData;//decompress data
boost::iostreams::copy(rxdat, decompressedData);//output to decompressData