[c/c++]_[primary]_[using zlib Library compressed file]

Source: Internet
Author: User
Tags filetime


Scene:

1. Windows did not find the system provided by the Win32 API to generate a Zip compressed file, there is known that Daniel Trouble to leave a message.

2. Zlib is more commonly used, compiling is also convenient, use it to do compression. The MACOSX platform supports the Zlib library by default.

Http://zlib.net

3. The src\contrib\minizip\minizip.c in the Zlib library has compression examples, I am using zlib 1.2.5, compiled with VS2010. :

http://download.csdn.net/detail/infoworld/8177625

4. I encapsulated a bit ziphelper easy to use, first of all compile zlib for the DLL version, this class is to cut some things minizip.c, support the Chinese path, the project needs to support only one level of directory, if there are sub-directory is not difficult, you change it.

5. How to use, note that the path string must be UTF8 format encoding, compile with the precompiled macro Zlib_winapi

Ziphelper Z;z.addfile (utf8_path1); Z.addfile (utf8_path2); Z.tozip (Utf8_output_zip_path);

6. MacOSX has not been measured, in theory should be able to compile.

7. Helpful help to top down or leave a message.

Zip_helper.h

#ifndef __zip_helper#define __zip_helper#include <vector> #include <string>//1. Subdirectories not supported//NOTE: Because the zlib library is used, When used, add precompiled Macro Zlib_winapiclass Ziphelper{public:ziphelper () {}~ziphelper () {}//path:utf8 pathziphelper& AddFile ( Const char* Input_path)//output_path:utf8 pathbool tozip (const char* output_path);p rivate:std::vector<std:: String> 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 <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <errno.h> #include <fcntl.h> #include <direct.h># Include <io.h> #include "zlib.h" #include "zip.h" #ifdef _win32#define usewin32ioapi#include "Iowin32.h" #endif # Define WRITEBUFFERSIZE (16384) #define MAXFILENAME (#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,&AMP;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 you add an error when linking to the project, it is because you have not precompiled the macro Zlib_winapi

1>zip_helper.obj:error LNK2001: unresolved external symbol _zipclose



[c/c++]_[primary]_[using zlib Library compressed file]

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.