Skip to content

Instantly share code, notes, and snippets.

@habedi
Created August 25, 2019 18:02
Show Gist options
  • Save habedi/6c55d0f7c6a6e9dbf85379d5f6e84b15 to your computer and use it in GitHub Desktop.
Save habedi/6c55d0f7c6a6e9dbf85379d5f6e84b15 to your computer and use it in GitHub Desktop.
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__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment