Search code examples
c++algorithmcompressionzlibchunks

Decompress appended compressed strings


I am creating an application, where I have to store lots of string data - received in chunks. I am trying to save it in compressed format. however, If i use zlib to compress strings and similarly append more compressed chunks to it, the decompression fails.

std::string allinput = "AAAA";
std::string cstr1 = compress_string( allinput );

std::string allinput2 = "BBBB";
std::string cstr2 = compress_string( allinput2 );

std::string cstr = cstr1 + cstr2;

std::string out = decompress_string( cstr );
std::cout << "DECOMP:" << out << "\n" ;

With the above code I get the following output :

AAAA

Is there some method to decompress appended chunks of compressed strings

PS: implementation of compress_string and decompress_string can be found from C++ Code Snippet - Compressing STL Strings with zlib


Solution

  • Your idea of adding compressed chunks will not work well, and you have to replace that interface, https://panthema.net/2007/0328-ZLibString.html to zlib.

    If you look inside zlib you see that the compression-routine deflate will stop when it runs out of data to compress, based on avail_in. Set that to the amount of data you have, and increase when you get the new string.

    (If you want to have useable results at every step it becomes more complicated.)

    Compressing multiple strings separately and adding them together would be possible, but is a bad idea. It will add overhead, and give worse result since compression is based on patterns within the input data and by compressing multiple times you have to re-discover the patterns in each input.