Scene:
1. Unzip the file is usually used after downloading a zip file after decompression, or analysis of a file need to unzip the operation.
2. Unzip the file, especially the zip file with folder, often the system does not provide such Win32 API, of course, C # comes with a library can be extracted, of course, this is only the C + +, such as C # and Java This open standard library is not considered in the scope.
3. Examples of use of zlib decompression files in the Contrib\minizip example. Here is basically the direct extraction of MINIUNZ.C code package decompression can be, just changed the next support Chinese path.
Master File Zip_util.cpp
#include "stdafx.h" #include "zip_util.h" #include "unzip.h" #include <iostream> #include <string> #include <algorithm> #include <stdlib.h> #include <zlib.h> #include "ioapi.h" #include "iowin32.h" #include " Wrap_object.h "using namespace std; #define CASESENSITIVITY (0) #define WRITEBUFFERSIZE (8192) #define MAXFILENAME (256) Unzfile Ziputil::unzopen64 (const wchar_t* path) {zlib_filefunc64_def ffunc; FILL_WIN32_FILEFUNC64W (&ffunc); Unzfile ZF = unzopen2_64 (Path,&ffunc); return ZF;} char* ConvertUnicodeToUtf8 (const wchar_t* Unicode) {if (Unicode = = NULL) {return strdup ("n");} int Len; Len = WideCharToMultiByte (Cp_utf8, 0,unicode,-1, NULL, 0, NULL, NULL); Char *szutf8 = (char*) malloc (len + 1); memset (SzUtf8, 0, Len + 1); WideCharToMultiByte (Cp_utf8, 0,unicode, 1, SzUtf8, Len, null,null); return SzUtf8; }wchar_t* convertutf8tounicode (const char* UTF8) {if (!utf8) {wchar_t* buf = (wchar_t*) malloc (2); memset (buf,0,2); return BUF;} InchT Nlen =:: MultiByteToWideChar (Cp_utf8,mb_err_invalid_chars, (LPCSTR) utf8,-1,null,0);//Returns the required Unicode length WCHAR * Wszunicode = new Wchar[nlen+1]; memset (wszunicode, 0, Nlen * 2 + 2); Nlen = MultiByteToWideChar (Cp_utf8, 0, (LPCSTR) UTF8,-1, Wszunicode, Nlen); Turn the UTF8 into Unicodereturn wszunicode;} void Change_file_date (const char *filename,ulong Dosdate,tm_unz tmu_date) {HANDLE hfile; FILETIME Ftm,ftlocal,ftcreate,ftlastacc,ftlastwrite; wchar_t* Filenamew = convertutf8tounicode (filename); hfile = CreateFile (Filenamew,generic_read | Generic_write, 0,null,open_existing,0,null); Getfiletime (Hfile,&ftcreate,&ftlastacc,&ftlastwrite); Dosdatetimetofiletime ((Word) (dosdate>>16), (word) dosdate,&ftlocal); LocalFileTimeToFileTime (&FTLOCAL,&FTM); Setfiletime (HFILE,&FTM,&FTLASTACC,&FTM); CloseHandle (hfile); Free (Filenamew);} int Mymkdir (const char* dirname) {wchar_t* Dirnamew = Convertutf8tounicode (dirname); int res = CreateDirectory(dirnamew,null); free (Dirnamew); return res;} int MakeDir (const char *newdir) {char *buffer; char *p;int len = (int) strlen (newdir); if (len <= 0) return 0;buffer = (c har*) malloc (len+1); memset (buffer,0,len+1); if (buffer==null) {printf ("Error allocating memory\n"); return unz_ Internalerror;} strcpy (Buffer,newdir), if (buffer[len-1] = = '/' | | buffer[len-1] = = ' \ \ ') {buffer[len-1] = ' n ';} if (mymkdir (buffer) = = 0) {free (buffer); return 1;} p = buffer+1;while (1) {char hold;while (*p && *p! = ' \ \ ' && *p! = '/') P++;hold = *p;*p = 0;if ((Mymkdir (buf FER) = =-1) && (errno = = ENOENT)) {printf ("couldn ' t create directory%s\n", buffer); free (buffer); return 0;} if (hold = = 0) break;*p++ = hold;} Free (buffer); return 1;} int Do_extract_currentfile (unzfile uf,const int* popt_extract_without_path,int* popt_overwrite,const char* Password, Const char* dir) {char filename_inzip[256];char* filename_withoutpath;char* p;int ERR=UNZ_OK; FILE *fout=null;void* buf;uint size_buf;unz_file_info64 file_info; ULong Ratio=0;err = UnzGetCurrentFileInfo64 (uf,&file_info,filename_inzip,sizeof (Filename_inzip), Null,0,null, 0); if (ERR!=UNZ_OK) {printf ("Error%d with ZipFile in unzgetcurrentfileinfo\n", err); return err;} Size_buf = Writebuffersize;buf = (void*) malloc (SIZE_BUF), if (buf==null) {printf ("Error allocating memory\n"); return UNZ _internalerror;} std::string Dir1 (dir);d ir1.append (Filename_inzip), strncpy (Filename_inzip,dir1.c_str (), sizeof (Filename_inzip));p = Filename_withoutpath = Filename_inzip;while ((*p)! = ' + ') {if ((*p) = = '/') | | ((*p) = = ' \ \ ')) Filename_withoutpath = p+1;p++;} if ((*filename_withoutpath) = = ' + ') {if ((*popt_extract_without_path) ==0) {printf ("Creating directory:%s\n", filename _inzip); Mymkdir (Filename_inzip);}} Else{const char* write_filename;int skip=0;if ((*popt_extract_without_path) ==0) write_filename = Filename_inzip; Elsewrite_filename = filename_withoutpath;wchar_t* Write_filenamew = Convertutf8tounicode (write_filename); Wrapmalloc Wm_filename (Write_filenamew); err = UnzopencurRentfilepassword (Uf,password); if (ERR!=UNZ_OK) {printf ("Error%d with ZipFile in unzopencurrentfilepassword\n", err);} if ((skip==0) && (ERR==UNZ_OK)) {Fout=_wfopen (write_filenamew,l "WB");/* Some zipfile don ' t contain directory Alone before file */if (fout==null) && ((*popt_extract_without_path) ==0) && (filename_withoutpath!= ( char*) (Filename_inzip)) {char c=* (filename_withoutpath-1); * (filename_withoutpath-1) = ' MakeDir '; * (filename_withoutpath-1) =c;fout = _wfopen (write_filenamew,l "WB");} if (fout==null) {printf ("Error opening%s\n", Write_filename);}} if (fout!=null) {printf ("Extracting:%s\n", write_filename);d O{err = Unzreadcurrentfile (UF,BUF,SIZE_BUF); if (err<0 {printf ("Error%d with ZipFile in unzreadcurrentfile\n", err); if (err>0) if (fwrite (buf,err,1,fout)!=1) {printf ("Error in writing extracted file\n"); err=unz_errno;break;}} while (err>0), if (fout) fclose (Fout), if (err==0) change_file_date (WRITE_FILENAME,FILE_INFO.DOSDATE,FILE_INFO.TMU _date);} if (ERR==UNZ_OK) {err = Unzclosecurrentfile (UF), if (ERR!=UNZ_OK) {printf ("Error%d with ZipFile in unzclosecurrentfile\n" , err);}} Elseunzclosecurrentfile (UF); /* don ' t lose the error */}free (BUF); return err;} int Do_extract (unzfile uf,int opt_extract_without_path,int opt_overwrite,const char* password,const char* dir) {ULong i; Unz_global_info64 Gi;int err; file* Fout=null;err = unzGetGlobalInfo64 (Uf,&gi), if (ERR!=UNZ_OK) printf ("Error%d with ZipFile in Unzgetglobalinfo \ n ", err); for (i=0;i<gi.number_entry;i++) {if (Do_extract_currentfile (uf,&opt_extract_without_path,&opt_ OVERWRITE,PASSWORD,DIR)! = UNZ_OK) return 0;if ((i+1) <gi.number_entry) {err = Unzgotonextfile (UF); if (ERR!=UNZ_OK) { printf ("Error%d with ZipFile in unzgotonextfile\n", err); return 0;}}} return 1;} BOOL Ziputil::unzipfile (std::wstring zip_file,std::wstring unzip_dir) {if (Unzip_dir[unzip_dir.size ()-1]! = L ' \ \ ') { Unzip_dir.append (L "\ \");} int err = UNZ_OK; Error status UInt size_buf = WritebufFersize; Byte size of buffer to store raw CSV data void* buf; The buffer string sout; Output strings char filename_inzip[256]; For Unzgetcurrentfileinfo Unz_file_info File_info; For unzgetcurrentfileinfo unzfile uf = UnzOpen64 (Zip_file.c_str ()); Open ZipFile Stream if (uf==null) {wcerr << "cannot open" << zip_file << Endl; return false; }//File is Opencreatedirectory (Unzip_dir.c_str (), NULL), char* UTF8 = ConvertUnicodeToUtf8 (Unzip_dir.c_str ()); int ret_value = Do_extract (Uf,0,0,null,utf8); Unzclose (UF); free (UTF8); return ret_value;}
Complete project:
http://download.csdn.net/detail/infoworld/8841191
[zlib]_[primary]_[using Zlib to extract the zip content completely]