如何在 C++ 中读取/写入 gzipped 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/624250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I read / write gzipped files in C++?
提问by Frank
How do I read / write gzipped files in C++?
如何在 C++ 中读取/写入 gzipped 文件?
The iostream
wrapper classes herelook 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
). A simple
但是我实际上无法链接它(尽管我有一个/usr/lib/libz.a
)。一个简单的
g++ test-gzstream.cpp -lz
didn't do it (undefined reference to gz::gzstreambase::~gzstreambase()
).
没有做(undefined reference to gz::gzstreambase::~gzstreambase()
)。
采纳答案by Macke
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.
显然,您还需要定义 gzstreambase 析构函数的 cpp 文件,即gzstream.cpp(这是链接错误)。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.
编辑:更新了链接以指向包含主要错误修复的代码的最新版本。
回答by Johannes Schaub - litb
Consider using the Boost zip filters. According to them, it supports bzip
, gzip
and zlib
format.
考虑使用 Boost zip 过滤器。据他们说,它支持bzip
,gzip
和zlib
格式。
回答by tflutre
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
回答by Piti Ongmongkolkul
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.
我在使用旧的 GCC 编译器时也遇到了这个问题。我只是通过制作 gzstream 的仅标头版本来解决此问题,该版本应该更易于使用。
回答by Mykola Golubyev
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. Or use the provided Makefile, adapt its variables, and follow the remarks in the Makefile.
要么手动编译 gzstream.C,将它放在某个库中,然后将 gzstream.h 移动到编译器的包含搜索路径中。或者使用提供的 Makefile,调整其变量,并按照 Makefile 中的说明进行操作。