How to read a .gz file line-by-line in C++?

后端 未结 7 510
一整个雨季
一整个雨季 2020-12-29 23:08

I have 3 terabyte .gz file and want to read its uncompressed content line-by-line in a C++ program. As the file is quite huge, I want to avoid loading it completely in memor

7条回答
  •  庸人自扰
    2020-12-29 23:30

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

    Alternatively you may have a look at BOOST C++ wrapper

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

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() 
    {
        using namespace std;
    
        ifstream file("hello.z", ios_base::in | ios_base::binary);
        filtering_streambuf in;
        in.push(zlib_decompressor());
        in.push(file);
        boost::iostreams::copy(in, cout);
    }
    

提交回复
热议问题