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

Frank picture Frank · Mar 8, 2009 · Viewed 43.2k times · Source

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

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

gz::igzstream in(filename);
std::string line;
while(std::getline(in, line)){
  std::cout << line << std::endl;
}

But I wasn't able to actually link it (although I have a /usr/lib/libz.a). A simple

g++ test-gzstream.cpp -lz

didn't do it (undefined reference to gz::gzstreambase::~gzstreambase()).

Answer

Johannes Schaub - litb picture Johannes Schaub - litb · Mar 8, 2009

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