Using the C language to demonstrate how to use the Zlib library to realize the compression and decompression of file __c language

Source: Internet
Author: User
Tags fread uncompress

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[]) { &nbsp ;
 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;    &nbsp}     if ((file = fopen (argv[1), "rb") = = NULL)     {   &nbsp
;    printf ("can\ ' t open%s!\n", argv[1]);
        return-1;    &nbsp}     /* 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;
   &nbsp     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;    &nbsp}     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;    &nbsp     /* 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; }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.