Many netizens read the zlib library compress and uncompress function of the use of the article, still can not independently complete the simple file compression and decompression, the author here to append such a demo code. The root of the problem is that these netizens are very vague about the concept of string and byte throttling, the difference between text file and binary file is often ambiguous, in fact, the byte stream can represent all the data, the binary file is the essence of any file. A byte stream is an octet, and there is no end symbol, so it needs to be given a length of information. The binary file is a byte-by-byte, with no line-feed characters.
File compression, you can automatically calculate the length of the buffer through the length of the source file, compressed after writing to the target file, you need to retain the source file and the length of the target data as the basis for decompression, reference to the following code:
#include <stdlib.h> #include <stdio.h> #include <zlib.h> int main (int argc, char* argv[]) {   ;
file* FILE;
ulong Flen;
unsigned char* fbuf = NULL;
ulong Clen;
unsigned char* cbuf = NULL; /* The data of srcfile file into dstfile file via command line parameters * * if (argc < 3) {&
nbsp; printf ("Usage:zcdemo srcfile dstfile\n");
return-1;  } if ((file = fopen (argv[1), "rb") = = NULL) {  
; printf ("can\ ' t open%s!\n", argv[1]);
return-1;  } /* load source file data to buffer/ fseek (file, 0L, Seek_end); &
nbsp;/* to end of File/ flen = ftell (file); /* Get file length * * fseek (file, 0L, Seek_set); if ((fbuf = (unsigned char*) malloc (sizeof (unsigned char) * flen)) = = NULL) {&NBSP;&N Bsp
printf ("No enough memory!\n");
fclose (file);
return-1;
  fread (fbuf, sizeof (unsigned char), flen, file);
/* Compression Data */ clen = Compressbound (Flen); if ((cbuf = (unsigned char*) malloc (sizeof (unsigned char) * clen)) = = NULL) {&NBSP;&N Bsp
printf ("No enough memory!\n");
fclose (file);
return-1;  } if (Compress (Cbuf, &clen, Fbuf, Flen)!= z_ok) { &nbs P
printf ("Compress%s failed!\n", argv[1]);
return-1;
} fclose (file); if ((file = fopen (argv[2], "wb") = = NULL) { p
rintf ("can\ ' t create%s!\n", argv[2]);
return-1;   /* Save compressed data to target file/ fwrite (&flen, sizeof (ULong), 1, file) ; /* Write to source file length * * fwrite (&clen, sizeof (ULong), 1, file); /*
Write Target data length * * fwrite (cbuf, sizeof (unsigned char), clen, file);
fclose (file);
free (FBUF);
free (CBUF);
return 0; }
File decompression, you can retain information to get the size of the buffer and data flow, so that after the decompression can be directly saved, refer to the following code:
#include <stdlib.h> #include <stdio.h> #include <zlib.h> int main (int argc, char* argv[]) {file* FILE
;
ULong Flen;
unsigned char* fbuf = NULL;
ULong Ulen;
unsigned char* ubuf = NULL;
/* The data of the Srcfile file is uncompressed and stored in the Dstfile file via command-line parameters * * IF (ARGC < 3) {printf ("Usage:zudemo srcfile dstfile\n");
return-1;
} if ((File = fopen (argv[1], rb)) = = NULL) {printf ("can\ ' t open%s!\n", argv[1]);
return-1; }/* Load source file data to buffer * * * fread (&ulen, sizeof (ULong), 1, file); /* Get buffer Size * * fread (&flen, sizeof (ULong), 1, file); /* Get the Data flow size */if ((Fbuf = (unsigned char*) malloc (sizeof (unsigned char) * flen)) = = NULL) {printf ("No enough memory!\n")
);
fclose (file);
return-1;
} fread (Fbuf, sizeof (unsigned char), flen, file);
/* Uncompressed Data */if ((Ubuf = (unsigned char*) malloc (sizeof (unsigned char) * ulen)) = = NULL) {printf ("No enough memory!\n");
fclose (file);
return-1; } if (Uncompress (Ubuf, &ulen, Fbuf, Flen)!= Z_ok) {printf ("uncompress%s FAiled!\n ", argv[1]);
return-1;
} fclose (file);
if ((File = fopen (argv[2], "wb") = = NULL) {printf ("can\ ' t create%s!\n", argv[2));
return-1;
/* Save uncompressed data to target file/* fwrite (ubuf, sizeof (unsigned char), ulen, file);
fclose (file);
Free (FBUF);
Free (UBUF);
return 0; }