Simple compression and decompression of String C ++ C ++

I need simple compression and decompression of std :: string in C ++. I looked at this site , and the code looked at an array of characters. I want to implement two functions:

std::string original = "This is to be compressed!!!!"; std::string compressed = string_compress(original); std::cout << compressed << std::endl; std::string decompressed = string_decompress(compressed); std::cout << decompressed << std::endl; 

I tried boost compression like:

 std::string CompressData(const std::string &data) { std::stringstream compressed; std::stringstream decompressed; decompressed << data; boost::iostreams::filtering_streambuf<boost::iostreams::input> out; out.push(boost::iostreams::zlib_compressor()); out.push(decompressed); boost::iostreams::copy(out, compressed); return compressed.str(); } std::string DecompressData(const std::string &data) { std::stringstream compressed; std::stringstream decompressed; compressed << data; boost::iostreams::filtering_streambuf<boost::iostreams::input> in; in.push(boost::iostreams::zlib_decompressor()); in.push(compressed); boost::iostreams::copy(in, decompressed); return decompressed.str(); } 

but the code sometimes gives null characters in a string, i.e. \ u0000 . How to handle if compressed data contains these null characters. Is the return type string valid ? How to implement string_compress and string_decompress functions using zlib?

+5
source share
2 answers

You can do it like @LawfulEvil. Here is a snippet of code that works :)

 std::string original = "This is to be compressed!!!!"; std::string compressed_encoded = string_compress_encode(original); std::cout << compressed_encoded << std::endl; std::string decompressed_decoded = string_decompress_decode(compressed_encoded); std::cout << decompressed_decoded << std::endl; 

Using this as a base64 encoding / decoding library

 std::string string_compress_encode(const std::string &data) { std::stringstream compressed; std::stringstream original; original << data; boost::iostreams::filtering_streambuf<boost::iostreams::input> out; out.push(boost::iostreams::zlib_compressor()); out.push(original); boost::iostreams::copy(out, compressed); /**need to encode here **/ std::string compressed_encoded = base64_encode(reinterpret_cast<const unsigned char*>(compressed.c_str()), compressed.length()); return compressed_encoded.str(); } std::string string_decompress_decode(const std::string &data) { std::stringstream compressed_encoded; std::stringstream decompressed; compressed_encoded << data; /** first decode then decompress **/ std::string compressed = base64_decode(compressed_encoded); boost::iostreams::filtering_streambuf<boost::iostreams::input> in; in.push(boost::iostreams::zlib_decompressor()); in.push(compressed); boost::iostreams::copy(in, decompressed); return decompressed; } 
+5
source

Compression uses all the values โ€‹โ€‹available for each byte, so it will be displayed as a "garbage" or "strange" character when trying to view as ascii. Expected. You will need to encode the data for the / json transfer package to avoid zeros. I propose a base of 64. The code for this is available at the link below (which I did not write, so I will not copy here).

+1
source

Source: https://habr.com/ru/post/1209370/


All Articles