1.包含頭文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#define GZFile gzFile
2.主函數(shù)部分
int main(int argc, char **argv)
{
GZFile zfile;
FILE *fp;
int c;
if(argc < 3)
{
printf("use: %s [input file] [output file]\n", argv[0]);
return 0;
}
fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("open file error!\n");
return -1;
}
zfile = gzopen(argv[2], "wb");
if(zfile == NULL)
{
printf("gzopen file error!\n");
fclose(fp);
return -1;
}
while((c=fgetc(fp)) != EOF)
{
gzputc(zfile, c);
}
fclose(fp);
gzclose(zfile);
return 0;
}
3.編譯
gcc -o gztest gztest.c -I$HOME/local/include -lz -lminizip
4.運行
$ ./gztest test.gz test