C++ 如何使用 zlib 轻松压缩和解压缩文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5649030/
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 can I easily compress and decompress files using zlib?
提问by Datoxalas
How can I easily compress and decompress files using zlib?
如何使用 zlib 轻松压缩和解压缩文件?
回答by Cristi
For decompression:
解压:
char buf[1024*1024*16];
gzFile *fi = (gzFile *)gzopen("file.gz","rb");
gzrewind(fi);
while(!gzeof(fi))
{
int len = gzread(fi,buf,sizeof(buf));
//buf contains len bytes of decompressed data
}
gzclose(fi);
For compression
用于压缩
gzFile *fi = (gzFile *)gzopen("file.gz","wb");
gzwrite(fi,"my decompressed data",strlen("my decompressed data"));
gzclose(fi);
回答by Alok Save
回答by Dan
If you can use boost, I would recommend the boost iostreams API. See the boost iostreams tutorial. It has support for GZIP and BZIP2 compressors and decompressors.
如果您可以使用 boost,我会推荐 boost iostreams API。请参阅 boost iostreams 教程。它支持 GZIP 和 BZIP2 压缩器和解压缩器。