Here is my code using DEFLATE compression in boost.iostreams library; I am sure that you can connect the corresponding BZip2 compressor:
#include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/filter/zlib.hpp> #include <boost/iostreams/filter/bzip2.hpp> // <--- this one for you #include <boost/iostreams/write.hpp> // Output std::ofstream datfile(filename, std::ios::binary); boost::iostreams::filtering_ostreambuf zdat; zdat.push(boost::iostreams::zlib_compressor()); // your compressor here zdat.push(datfile); boost::iostreams::write(zdat, BUFFER, BUFFER_SIZE); // Input std::ifstream datfile(filename, std::ios::binary); boost::iostreams::filtering_istreambuf zdat; zdat.push(boost::iostreams::zlib_decompressor()); zdat.push(datfile); boost::iostreams::read(zdat, BUFFER, BUFFER_SIZE);
The bzip2 compressor is called bzip2_(de)compressor() .
If you need a byte buffer, not a file, use a line stream:
char mydata[N]; std::string mydatastr(mydata, N); std::istringstream iss(mydatastr, std::ios::binary); std::ostringstream oss(mydatastr, std::ios::binary);
source share