Boost logo

Boost Users :

Subject: [Boost-users] [iostreams] use of gzip_compressor with strings
From: Boncek, John (jboncek_at_[hidden])
Date: 2016-06-17 13:26:00


I need to do some in-memory manipulation of strings, including compression to gzip format. I am new to the use of Boost and to the use of gzip. I found the following snippet on the Internet, compressing a std::string and producing a new std::string as output. This implies that gzip_compressor never produces a null byte in its output but the gzip_compressor documentation doesn't say that and my search of the underlying library zlib documentation doesn't say anything like that either. So is this a correct use?

std::string compress(const std::string& data)
{
    namespace bio = boost::iostreams;

    std::stringstream compressed;
    std::stringstream origin(data);

    bio::filtering_streambuf<bio::input> out;
    out.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_compression)));
    out.push(origin);
    bio::copy(out, compressed);

    return compressed.str();
}

Thanks,
John Boncek



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net