How do I read / write gzipped files in C++?

后端 未结 5 890
渐次进展
渐次进展 2020-12-14 03:51

How do I read / write gzipped files in C++?

The iostream wrapper classes here look good, and here is a simple usage example:

gz::igzstre         


        
相关标签:
5条回答
  • 2020-12-14 04:19

    To give more details than what was briefly mentioned by the other users, here is how I managed to work with gzstream on my computer.

    First, I downloaded gzstream and installed it in my home (the two last lines can be added to your ~/.bash_profile):

    cd ~/src
    mkdir GZSTREAM
    cd GZSTREAM/
    wget http://www.cs.unc.edu/Research/compgeom/gzstream/gzstream.tgz
    tar xzvf gzstream.tgz
    cd gzstream
    make
    export CPLUS_INCLUDE_PATH=$HOME/src/GZSTREAM/gzstream
    export LIBRARY_PATH=$HOME/src/GZSTREAM/gzstream
    

    Then, I tested the installation:

    make test
    ...
    # *** O.K. Test finished successfully. ***
    

    Finally, I wrote a dummy program to check that I could effectively use the library:

    cd ~/temp
    vim test.cpp
    

    Here is the code (very minimalist, should be much improved for real applications!):

    #include <iostream>
    #include <string>
    #include <gzstream.h>
    using namespace std;
    
    int main (int argc, char ** argv)
    {
      cout << "START" << endl;
    
      igzstream in(argv[1]);
      string line;
      while (getline(in, line))
      {
        cout << line << endl;
      }
    
      cout << "END" << endl;
    }
    

    Here is how I compiled it:

    gcc -Wall test.cpp -lstdc++ -lgzstream -lz
    

    And last but not least, here is how I used it:

    ls ~/ | gzip > input.gz
    ./a.out input.gz
    START
    bin/
    src/
    temp/
    work/
    END
    
    0 讨论(0)
  • 2020-12-14 04:28

    Consider using the Boost zip filters. According to them, it supports bzip, gzip and zlib format.

    • boost zlib
    • boost gzip
    • boost bzip2
    0 讨论(0)
  • 2020-12-14 04:39

    Obviously you need the cpp-file where the gzstreambase destructor is defined as well, i.e. gzstream.cpp (that's the link fault). libz is just a c-api for gzip, it knows nothing of c++ stdlib streams.

    Boost's iostream lib has gzip and bzip2 streams too.

    EDIT: Updated the link to point to the latest version of the code that includes a major bug fix.

    0 讨论(0)
  • 2020-12-14 04:41

    This is from the "Gzstream Library Home Page"

    Either compile gzstream.C by hand, place it in some library, and move gzstream.h into the include search path of your compiler. Or use the provided Makefile, adapt its variables, and follow the remarks in the Makefile.

    0 讨论(0)
  • 2020-12-14 04:43

    I had this trouble as well with old GCC compiler. I just fixed this by making a header only version of gzstream which should be easier to use.

    https://gist.github.com/1508048

    0 讨论(0)
提交回复
热议问题