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()
).
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
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
):Then, I tested the installation:
Finally, I wrote a dummy program to check that I could effectively use the library:
Here is the code (very minimalist, should be much improved for real applications!):
Here is how I compiled it:
And last but not least, here is how I used it:
This is from the "Gzstream Library Home Page"
Consider using the Boost zip filters. According to them, it supports
bzip
,gzip
andzlib
format.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.