Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
c++ gzip compress/decompress string with boost
#ifndef __GZIP_H__
#define __GZIP_H__
#include <sstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
class Gzip {
public:
static 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();
}
static std::string decompress(const std::string& data)
{
namespace bio = boost::iostreams;
std::stringstream compressed(data);
std::stringstream decompressed;
bio::filtering_streambuf<bio::input> out;
out.push(bio::gzip_decompressor());
out.push(compressed);
bio::copy(out, decompressed);
return decompressed.str();
}
};
#endif // __GZIP_H__
@tprk77

This comment has been minimized.

Copy link

commented Aug 30, 2017

Thanks! I was having trouble figuring this out. (Was trying to use boost::iostreams::write, oops.)

@holland01

This comment has been minimized.

Copy link

commented Dec 26, 2017

You definitely saved me a few hours (well, maybe 30-60 minutes), man. Thanks!

@bkggkt3d

This comment has been minimized.

Copy link

commented Jul 26, 2018

Don't forget to install libboost-iostreams-dev e.g. on Debian & Co with "apt-get install libboost-iostreams-dev"
And add lib to linker with "-lboost-iostreams"
Thanks for this code...

@heape

This comment has been minimized.

Copy link

commented Mar 5, 2019

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.