C + + Zip compressed file, using the Zlib library and CodeProject on the zip_utils__c++

Source: Internet
Author: User
Tags zip folder

PS; The Zlib library is older, like version 1.01e. Basic functions are similar.


Part1:

Using the complete library on the CodeProject, Daniel is really convenient and easy to find.

Address: Http://www.codeproject.com/Articles/7530/Zip-Utils-clean-elegant-simple-C-Win

Download down on four files, zip.h zip.cpp unzip.h unzip.cpp

Use the direct include on the line.

Just put the readme on it.

For unzipping, add ' unzip.cpp ' to your project. Then, for example,
  #include "unzip.h"
  //
  hzip Hz = Openzip ("C:\\stuff.zip", 0);
  ZipEntry ze; Getzipitem (Hz,-1,&ze); int numitems=ze.index;
  for (int i=0; i<numitems; i++)
  {getzipitem (hz,i,&ze);
    Unzipitem (Hz,i,ze.name);
  }
  Closezip (Hz);




For zipping, add ' zip.cpp ' to your project. (You can add just one
of Zip/unzip, or both they function independently and also co-exist.)
  #include "zip.h"
  //
  hzip Hz = Createzip ("C:\\simple1.zip", 0);
  Zipadd (Hz, "Znsimple.bmp", "c:\\simple.bmp");
  Zipadd (Hz, "Znsimple.txt", "C:\\simple.txt");
  Closezip (Hz);

Part2: Use Zlib's old library.

Code on:

Directly to add compression of the package well;

void Addfiletozip (ZipFile ZF, const char* filenameinzip, const char* srcfile) {file* SRCFP = NULL;
	Initializes the file information written to the zip Zip_fileinfo zi; Zi.tmz_date.tm_sec = Zi.tmz_date.tm_min = Zi.tmz_date.tm_hour = Zi.tmz_date.tm_mday = Zi.tmz_date.tm_mon = zi.tmz_date.t
	m_year = 0;
	zi.dosdate = 0;
	ZI.INTERNAL_FA = 0;

	ZI.EXTERNAL_FA = 0;
	If Srcfile is empty, add empty directory char new_file_name[writebuffersize];
	memset (new_file_name, 0, sizeof (new_file_name));
	strcat (New_file_name, filenameinzip);
	if (srcfile = = NULL) {strcat (New_file_name, "/"); ///Create new file in zip file Zipopennewfileinzip (ZF, new_file_name, &zi, NULL, 0, NULL, 0, NULL, z_deflated, z_default_compress
	ION);
		if (srcfile!= NULL) {//Open source file SRCFP = fopen (Srcfile, "RB");
		if (SRCFP = = NULL) {zipclosefileinzip (ZF);//close zip file return;
		//Read into the source file and write to the zip file int size_buf = 0;
		void* Buf=null;
		Size_buf = writebuffersize;

		BUF = (void*) malloc (SIZE_BUF);
		int numbytes = 0; while (!feof (SRCFP)) {numbytes = Fread (buf,1, Size_buf, SRCFP);
			Zipwriteinfileinzip (ZF, buf, numbytes);
		if (Ferror (SRCFP)) break;
	//Close source file fclose (SRCFP);
//Close ZIP file Zipclosefileinzip (ZF); }

Use time:

ZipFile ZF;
Addfiletozip (ZF, "name in Zip", "source file to be compressed");
Zipclose (zf,0);

Not yet, compress to zip folder and decompress part. Add again later

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.