#include <stdio.h> #include <zlib.h> // Demo int decompress_file(char *infilename, char *outfilename) { int num_read = 0; char buffer[128]; gzFile infile = gzopen(infilename, "rb"); FILE *outfile = fopen(outfilename, "wb"); if (!infile || !outfile) { return -1; } while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) { fwrite(buffer, 1, num_read, outfile); } gzclose(infile); fclose(outfile); } int compress_file(char *infilename, char *outfilename) { int num_read = 0; char inbuffer[128]; FILE *infile = fopen(infilename, "rb"); gzFile outfile = gzopen(outfilename, "wb"); if (!infile || !outfile) { return -1; } while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) { total_read += num_read; gzwrite(outfile, inbuffer, num_read); } fclose(infile); gzclose(outfile); } int main(int argc, char **argv) { compress_file(argv[1],argv[2]); decompress_file(argv[2],argv[3]); }
本文出自 “一路风景” 博客,请务必保留此出处http://which.blog.51cto.com/7179876/1602544
原文地址:http://which.blog.51cto.com/7179876/1602544