Boost logo

Boost Users :

Subject: Re: [Boost-users] [iostreams] use of gzip_compressor with strings
From: Boncek, John (jboncek_at_[hidden])
Date: 2016-06-23 16:49:41

Thanks, just what I needed!


-----Original Message-----
From: Boost-users [mailto:boost-users-bounces_at_[hidden]] On Behalf Of eg
Sent: Tuesday, June 21, 2016 11:16 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [iostreams] use of gzip_compressor with strings

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