How can I easily compress and decompress files usi

2019-01-12 03:26发布

How can I easily compress and decompress files using zlib?

3条回答
唯我独甜
2楼-- · 2019-01-12 03:57

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);
查看更多
走好不送
3楼-- · 2019-01-12 04:02

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.

查看更多
Rolldiameter
4楼-- · 2019-01-12 04:05

Please read through this. The information is already available here:
That is the first link that shows up even on google.

查看更多
登录 后发表回答