Simple Zlib C ++ - Komprimierung und Dekomprimierung von Strings

Ich brauche eine einfache Komprimierung und Dekomprimierung eines std :: string in C ++. Ich habe mir dieses @ angeschaSeite? ˅ und der Code ist für Zeichenarray. Was ich implementieren möchte, sind die beiden Funktionen:

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;

ch hatte versucht, die Boost-Komprimierung als:

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();
}

, aber der Code gibt manchmal Null-Zeichen in Zeichenfolge, dh \ u0000. Wie gehe ich vor, wenn die komprimierten Daten diese Nullzeichen enthalten? Ist der Rückgabetyp string richtig? Wie kann ich function @ implementiere string_compress und string_decompress using zlib?

Antworten auf die Frage(4)

Ihre Antwort auf die Frage