如何在C ++中逐行读取.gz文件?

如何在C ++中逐行读取.gz文件?

问题描述:

我有.gz格式的3T文件。我想在C ++程序中逐行读取,而不是实际解压缩。任何人都可以发布一个简单的例子吗?

I have file of 3T in .gz format. I want to read it line-by-line in a C++ program without actually decompressing. Can any one post a simple example of doing it?

您最有可能需要使用ZLib的deflate,示例可以从网站

You most probably will have to use ZLib's deflate, example is available from their site

或者,您可以查看 BOOST C ++包装器

来自BOOST页面的示例数据从文件并写入标准输出)

The example from BOOST page (decompresses data from a file and writes it to standard output)

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>

int main() 
{
    using namespace std;

    ifstream file("hello.z", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(zlib_decompressor());
    in.push(file);
    boost::iostreams::copy(in, cout);
}