Created
August 25, 2019 18:02
-
-
Save habedi/6c55d0f7c6a6e9dbf85379d5f6e84b15 to your computer and use it in GitHub Desktop.
c++ gzip compress/decompress string with boost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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