看看Boost IOStreams
例如,我从命令行创建了以下文件:
$ echo "this is the first line" > file
$ echo "this is the second line" >> file
$ echo "this is the third line" >> file
$ bzip2 file
$ file file.bz2
file.bz2: bzip2 compressed data, block size = 900k
然后我使用 boost::iostreams::filtering_istream 来读取名为 file.bz2 的解压缩 bzip2 文件的结果。
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <iostream>
namespace io = boost::iostreams;
/* To Compile:
g++ -Wall -o ./bzipIOStream ./bzipIOStream.cpp -lboost_iostreams
*/
int main(){
io::filtering_istream in;
in.push(io::bzip2_decompressor());
in.push(io::file_source("./file.bz2"));
while(in.good()){
char c = in.get();
if(in.good()){
std::cout << c;
}
}
return 0;
}
运行命令的结果就是解压出来的数据。
$ ./bzipIOStream
this is the first line
this is the second line
this is the third line
您当然没有逐个字符地读取数据,但我试图使示例保持简单。