[iostreams] use of gzip_compressor with strings
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_streambufbio::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
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_streambufbio::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 http://stackoverflow.com/questions/2845769/can-a-stdstring-contain-embedded-... 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 http://stackoverflow.com/questions/27529570/simple-zlib-c-string-compression... Ed
Thanks, just what I needed! John -----Original Message----- From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of eg Sent: Tuesday, June 21, 2016 11:16 AM To: boost-users@lists.boost.org 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_streambufbio::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 http://stackoverflow.com/questions/2845769/can-a-stdstring-contain-embedded-... 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 http://stackoverflow.com/questions/27529570/simple-zlib-c-string-compression... Ed
participants (2)
-
Boncek, John
-
eg