Search code examples
c++boostzlib

Simple Zlib C++ String Compression and Decompression


I need a simple compression and decompression of a std::string in C++. I looked at this site and the code is for Character array. What I want to implement are the 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 had tried the boost compression as:

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 string ie \u0000. How do I handle if the compressed data contains these null characters. Is the return type string correct? How can I implement function string_compress and string_decompress using zlib?


Solution

  • You can do as @LawfulEvil suggested. Here is the code snippet 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 the base64 encode/decode library.

    #include <sstream>
    #include <boost/iostreams/filtering_streambuf.hpp>
    #include <boost/iostreams/copy.hpp>
    #include <boost/iostreams/filter/zlib.hpp>
    #include <cpp-base64/base64.h>
    
    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;
    }
    
    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.str();
    }