简体   繁体   English

如何在 C++ 中读取/写入 gzip 文件?

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

How do I read / write gzipped files in C++?如何在 C++ 中读取/写入 gzip 文件?

The iostream wrapper classes here look good, and here is a simple usage example: 这里iostream包装类看起来不错,下面是一个简单的使用示例:

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 ).但我无法真正链接它(尽管我有一个/usr/lib/libz.a )。 A simple一个简单的

g++ test-gzstream.cpp -lz

didn't do it ( undefined reference to gz::gzstreambase::~gzstreambase() ).没有这样做( undefined reference to gz::gzstreambase::~gzstreambase() )。

Consider using the Boost zip filters.考虑使用升压 zip 滤波器。 According to them, it supports bzip , gzip and zlib format.据他们介绍,它支持bzipgzipzlib格式。

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.为了提供比其他用户简要提到的更多的细节,这里是我如何设法在我的计算机上使用gzstream

First, I downloaded gzstream and installed it in my home (the two last lines can be added to your ~/.bash_profile ):首先,我下载gzstream并将其安装在我的家中(最后两行可以添加到您的~/.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

Obviously you need the cpp-file where the gzstreambase destructor is defined as well, ie gzstream.cpp (that's the link fault).显然,您还需要定义 gzstreambase 析构函数的 cpp 文件,即gzstream.cpp (这是链接错误)。 libz is just a c-api for gzip, it knows nothing of c++ stdlib streams. libz 只是 gzip 的 c-api,它对 c++ stdlib 流一无所知。

Boost's iostream lib has gzip and bzip2 streams too. Boost 的 iostream 库也有 gzip 和 bzip2 流。

EDIT: Updated the link to point to the latest version of the code that includes a major bug fix.编辑:更新了链接以指向包含主要错误修复的代码的最新版本。

I had this trouble as well with old GCC compiler.旧的 GCC 编译器也遇到了这个问题。 I just fixed this by making a header only version of gzstream which should be easier to use.我只是通过制作一个 header 的唯一版本的 gzstream 来解决这个问题,它应该更易于使用。

https://gist.github.com/1508048 https://gist.github.com/1508048

This is from the "Gzstream Library Home Page"这是来自“Gzstream图书馆主页”

Either compile gzstream.C by hand, place it in some library, and move gzstream.h into the include search path of your compiler.手动编译 gzstream.C ,将其放在某个库中,然后将 gzstream.h 移动到编译器的包含搜索路径中。 Or use the provided Makefile, adapt its variables, and follow the remarks in the Makefile.或者使用提供的Makefile,修改其变量,按照Makefile中的说明进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM