views:

1478

answers:

3

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()).

+4  A: 

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.

Marcus Lindblom
he uses lib instead of .cpp file.
Mykola Golubyev
Then he needs to link to the lib where gzstream.cpp has been compiled. liblm_access?
Marcus Lindblom
Thanks, just compiling together with gzstream.cpp works.
+2  A: 

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

Johannes Schaub - litb
+1  A: 

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.

Mykola Golubyev