Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 455
Frank Avatar asked Mar 08 '09 20:03

Frank


People also ask

Can you concatenate Gzipped files?

Files compressed by gzip can be directly concatenated into larger gzipped files.

What is a Gzipped file?

A GZ file is a compressed archive that is created using the standard gzip (GNU zip) compression algorithm. It may contain multiple compressed files, directories and file stubs. This format was initially developed to replace compression formats on UNIX systems.


2 Answers

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

  • boost zlib
  • boost gzip
  • boost bzip2
like image 142
Johannes Schaub - litb Avatar answered Oct 18 '22 20:10

Johannes Schaub - litb


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
like image 40
tflutre Avatar answered Oct 18 '22 20:10

tflutre