#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]); }
时间: 2024-10-18 22:44:48