解压缩bzip2字节数组

时间:2011-08-17 20:43:56

标签: c++ boost bzip2

如何使用boost解压缩bzip2压缩的字节数组?我找到了一个示例here,但输入是一个文件,因此使用了ifstream。文档对我来说不是很清楚:(。

编辑:我会接受提升的替代方案。

1 个答案:

答案 0 :(得分:6)

这是我在boost.iostreams库中使用DEFLATE压缩的代码;我相信你可以在相应的BZip2压缩器中挂钩:

#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);

bzip2压缩器名为bzip2_(de)compressor()

如果您需要字节缓冲区而不是文件,请使用字符串流:

char mydata[N];
std::string mydatastr(mydata, N);
std::istringstream iss(mydatastr, std::ios::binary);
std::ostringstream oss(mydatastr, std::ios::binary);
相关问题