这是我在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);
Run Code Online (Sandbox Code Playgroud)
调用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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2206 次 |
| 最近记录: |