[C/C ++] _ [primary] _ [Use zlib library to compress files]
Scenario:
1. WIndows does not find the win32 api provided by the system to generate a zip file. If you know it, leave a comment.
2. zlib is commonly used and compilation is convenient. Use it for compression. The MacOSX platform supports the zlib library by default.
Http://zlib.net
3. There is a compression example in src \ contrib \ minizip. c In the zlib library. I am using zlib 1.2.5 and compiled it using vs2010. :
Http://download.csdn.net/detail/infoworld/8177625
4. I encapsulated ZipHelper for ease of use. First, we need to compile zlib as the dll version. This class deletes minizip. some items in c support Chinese paths. Currently, the project only supports level-1 directories. If there are subdirectories, it is not difficult to modify them.
5. usage. Note that the path string must be UTF-8 encoded and the precompiled macro ZLIB_WINAPI must be added during compilation.
ZipHelper z;z.AddFile(utf8_path1);z.AddFile(utf8_path2);z.ToZip(utf8_output_zip_path);
6. MacOSX has not been tested. It should be compiled theoretically.
Zip_helper.h
# Ifndef _ ZIP_HELPER # define _ ZIP_HELPER # include
# Include
// 1. Sub-directories are not supported at the moment. // Note: zlib library is used, and the pre-compiled macro ZLIB_WINAPIclass ZipHelper {public: ZipHelper () {}~ is added during use (){}~ ZipHelper () {}// path: utf8 pathZipHelper & AddFile (const char * input_path); // output_path: utf8 pathbool ToZip (const char * output_path); private: std: vector
Files _;}; # endif
Zip_helper.cpp
#include "zip_helper.h"#ifndef _WIN32#ifndef __USE_FILE_OFFSET64#define __USE_FILE_OFFSET64#endif#ifndef __USE_LARGEFILE64#define __USE_LARGEFILE64#endif#ifndef _LARGEFILE64_SOURCE#define _LARGEFILE64_SOURCE#endif#ifndef _FILE_OFFSET_BIT#define _FILE_OFFSET_BIT 64#endif#endif#include
#include
#include
#include
#include
#include
#include
#include
#include "zlib.h"#include "zip.h"#ifdef _WIN32#define USEWIN32IOAPI#include "iowin32.h"#endif#define WRITEBUFFERSIZE (16384)#define MAXFILENAME (256)#ifdef _WIN32static wchar_t* QXUtf82Unicode(const char* utf) { if(!utf || !strlen(utf)) { return NULL; } int dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,utf,-1,NULL,0); size_t num = dwUnicodeLen*sizeof(wchar_t); wchar_t *pwText = (wchar_t*)malloc(num); memset(pwText,0,num); MultiByteToWideChar(CP_UTF8,0,utf,-1,pwText,dwUnicodeLen); return pwText; } static FILE* ZipFopen(const char* path,const char* mode){wchar_t* path_u = QXUtf82Unicode(path);wchar_t* mode_u = QXUtf82Unicode(mode);FILE* file = _wfopen(path_u,mode_u);free(path_u);free(mode_u);return file;}/* name of file to get info on *//* return value: access, modific. and creation times *//* dostime */uLong filetime(const char* f, tm_zip *tmzip, uLong *dt) {int ret = 0;{FILETIME ftLocal;HANDLE hFind;WIN32_FIND_DATA ff32;wchar_t *unicode = QXUtf82Unicode(f);hFind = FindFirstFile(unicode,&ff32);if (hFind != INVALID_HANDLE_VALUE){FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);FindClose(hFind);ret = 1;}free(unicode);}return ret;}#else#define ZipFopen fopen;#endifZipHelper& ZipHelper::AddFile(const char* input_path){files_.push_back(input_path);return *this;}bool ZipHelper::ToZip(const char* output_path){int err=0;zipFile zf;int errclose;int opt_compress_level = Z_DEFAULT_COMPRESSION;#ifdef USEWIN32IOAPIzlib_filefunc64_def ffunc;fill_win32_filefunc64W(&ffunc);wchar_t* temp_path = QXUtf82Unicode(output_path);zf = zipOpen2_64(temp_path,APPEND_STATUS_CREATE,NULL,&ffunc);free(temp_path);#elsezf = zipOpen64(output_path,APPEND_STATUS_CREATE);#endifif (zf == NULL){printf("error opening %s\n",output_path);err= ZIP_ERRNO;return false;}int size = files_.size();void* buf = NULL;int size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf);for (int i = 0; i < size; ++i){FILE * fin;int size_read;const char* filenameinzip = files_[i].c_str();const char *savefilenameinzip;zip_fileinfo zi;unsigned long crcFile=0;int zip64 = 0;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.tm_year = 0;zi.dosDate = 0;zi.internal_fa = 0;zi.external_fa = 0;filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);savefilenameinzip = filenameinzip;const char* pos = NULL;if( (pos = strrchr(savefilenameinzip,'\\')) || (pos = strrchr(savefilenameinzip,'/')) ){pos++;}else{pos = savefilenameinzip;}err = zipOpenNewFileInZip3_64(zf,pos,&zi,NULL,0,NULL,0,NULL,(opt_compress_level != 0) ? Z_DEFLATED : 0,opt_compress_level,0,-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,NULL,crcFile, zip64);if (err != ZIP_OK){printf("error in opening %s in zipfile\n",pos);}else{fin = ZipFopen(filenameinzip,"rb");if (fin==NULL){err=ZIP_ERRNO;printf("error in opening %s for reading\n",filenameinzip);}}if (err == ZIP_OK)do{err = ZIP_OK;size_read = (int)fread(buf,1,size_buf,fin);if (size_read < size_buf){if (feof(fin)==0){printf("error in reading %s\n",filenameinzip);err = ZIP_ERRNO;}}if (size_read>0){err = zipWriteInFileInZip (zf,buf,size_read);if (err<0){printf("error in writing %s in the zipfile\n",filenameinzip);}}} while ((err == ZIP_OK) && (size_read>0));if(fin){fclose(fin);}if (err<0){err=ZIP_ERRNO;}else{err = zipCloseFileInZip(zf);if (err!=ZIP_OK){printf("error in closing %s in the zipfile\n",filenameinzip);}}}errclose = zipClose(zf,NULL);if (errclose != ZIP_OK){printf("error in closing %s\n",output_path);return false;}return true;}
Note: if an error is reported when the project is linked again, it is because you did not pre-compile the macro ZLIB_WINAPI.
1> zip_helper.obj: error LNK2001: external symbol that cannot be parsed _ zipClose