2009年5月14日星期四

使用zlib读写gz文件

zlib is A Massively Spiffy Yet Delicately Unobtrusive Compression Library. The utility functions in zlib are very much like the stdandard I/O funtions in <stdio.h>.

Demo in short:

#include "zlib.h"

/* write buffer to gz file */

gzFile fp = gzopen("file.gz", "wb");
/* Writes the given number of uncompressed bytes into the compressed file.*/
gzwrite(fp, buffer, sizeof(buffer));
gzclose(fp);


/* read gz file into buffer */

gzFile fp = gzopen("file.gz", "rb");
/* read the given number of uncompressed bytes from the compressed file */
gzread(fp, buffer, sizeof(buffer));
/* or read a line */
gzgets(fp, buffer, sizeof(buffer));
gzclose(fp);

Use option "-lz" to link with zlib library.

Read more:
http://www.zlib.net/manual.html

没有评论: