Boost logo

Boost Users :

Subject: Re: [Boost-users] [iostreams] use of gzip_compressor with strings
From: eg (egoots_at_[hidden])
Date: 2016-06-21 12:15:38

On 6/17/2016 10:26 AM, Boncek, John wrote:
> 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();
> }

std::string() can handle strings with embedded NULL's (0's). However,
certain other operators and functions of the class do not (e.g.
c_str()). See

If you need to pass this as a string to someone else or use these other
functions/operators, you may wish to encode the compressed string before
returning it. See


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at