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

@tprk77 tprk77 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

@holland01 holland01 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

@bkggkt3d bkggkt3d 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

@heape heape commented Mar 5, 2019

thank you!

@correaa

This comment has been minimized.

Copy link

@correaa correaa commented Nov 21, 2019

And add lib to linker with "-lboost-iostreams"

-lboost_iostreams (with underscore) in Linux.

@ckt1010

This comment has been minimized.

Copy link

@ckt1010 ckt1010 commented Nov 28, 2019

And add lib to linker with "-lboost-iostreams"

-lboost_iostreams (with underscore) in Linux.

Thanks!

@kuhlmannmarkus

This comment has been minimized.

Copy link

@kuhlmannmarkus kuhlmannmarkus commented Feb 2, 2020

Awesome! Thanks!

@mal-w

This comment has been minimized.

Copy link

@mal-w mal-w commented Oct 17, 2020

Amazing gist, really helped with my understanding of this component of the library.

Thanks also to @bkggkt3d for the install reminder...

Build help for CMake:

IMPORTANT NOTE: As highlighted by @ILENIMARIUS in the following comment, there are alternative ways to setup your build with cmake for this specific example... The following is maybe functional but definitely not the correct way to setup your cmake build lol
I don't have the link of the other resource I using to work this out unfortunately...

CMakeList.txt:

# ... declare your executable <your_target> ...
find_package(Boost COMPONENTS iostreams) # and any other components obviously

# and the typical: (already in your CMakeLists.txt if you're already using another Boost component)
target_include_directories(<your_target> PRIVATE ${Boost_INCLUDE_DIR})
target_link_libraries(<your_target> ${Boost_LIBRARIES})
@ILENIMARIUS

This comment has been minimized.

Copy link

@ILENIMARIUS ILENIMARIUS commented Apr 6, 2021

Amazing gist, really helped with my understanding of this component of the library.

Thanks also to @bkggkt3d for the install reminder...

Build help for CMake:

I don't have the link of the other resource I using to work this out unfortunately...

CMakeList.txt:

# ... declare your executable <your_target> ...
find_package(Boost COMPONENTS iostreams) # and any other components obviously

# and the typical: (already in your CMakeLists.txt if you're already using another Boost component)
target_include_directories(<your_target> PRIVATE ${Boost_INCLUDE_DIR})
target_link_libraries(<your_target> ${Boost_LIBRARIES})

Try this after compiling the iostreams + zlib:

find_package (Boost ${Boost_Version} COMPONENTS iostreams zlib )
if(Boost_FOUND)
target_link_libraries(${TARGET_NAME} PUBLIC Boost::iostreams
Boost::zlib)
endif()

@Rami-Slicer

This comment has been minimized.

Copy link

@Rami-Slicer Rami-Slicer commented Apr 7, 2021

Super useful! Thanks for sharing!

@mal-w

This comment has been minimized.

Copy link

@mal-w mal-w commented Apr 14, 2021

@ILENIMARIUS so that made make fail but the cmake step worked (after a couple fails and the realization that I never had explicitly called find_package with zlib). I think what you're suggesting is working it's just there is some other part of my code that depends on my janky build setup... (i.e. using target_inlcude_directories with the PRIVATE qualifier.

I'll update my old comment with a disclaimer... Which is that I shouldn't be giving cmake advice lol 😓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment