#include <fcntl.h> #include <sys/stat.h> #include <stdio.h> #include < dirent.h> #include <string.h> #include <stdlib.h> #include <vector> #include <string> #include <iostream> #include <fstream> #include "Minizip/zip.h" # include "Minizip/unzip.h" Using namespace std;void enumdirfiles (const string& dirprefix,const string& dirname,vector<string>& vfiles) { if (Dirprefix.empty () | | dirname.empty ()) return; string dirNameTmp = dirName; string dirPre = dirPrefix; if (Dirnametmp.find_last_of ("/") != dirnametmp.length () - 1) dirNameTmp += "/"; if (dirnametmp[0] == '/') DIRNAMETMP&NBSP;=&NBSP;DIRNAMETMP.SUBSTR (1); if (dirpre.find_last_of ("/") != dirpre.length () - 1) dirPre += "/"; string path; path = dirpre + dirnametmp; struct stat filestat; Dir* pdir = opendir (Path.c_str ()); if (!pDir) return; struct dirent* pDirEnt = NULL; while ( (Pdirent = readdir (Pdir)) != NULL ) { if (strcmp (Pdirent->d_name, ".") == 0 | | &NBSP;STRCMP (Pdirent->d_name, "..") == 0) continue; string tmpDir = dirPre + dirNameTmp + pdirent->d_name; if (Stat (TMPDIR.C_STR (), &fileStat) != 0) continue; string innerdir = dirnametmp + pdirent->d_name ; if (filestat.st_mode & s_ifdir == s_ifdir) { enumdirfiles (dirprefix,innerdir,vfiles); continue; } vfilEs.push_back (Innerdir); } if (PDir) closedir (Pdir);} Int writeinzipfile (zipfile zfile,const string& file) { fstream f (File.c_str (), std::ios::binary | std::ios::in); &NBSP;&NBSP;&NBSP;&NBSP;F.SEEKG (0,&NBSP;STD:: Ios::end); long size = f.tellg (); f.seekg (0, std::ios::beg); if ( size <= 0 ) { return zipwriteinfileinzip (zFile,NULL,0); } char* buf = new char[size]; f.read (buf,size); int ret = zipwriteinfileinzip (zFile,buf,size); delete[] buf; return ret;} Int main (iNt argc, char *argv[]) { if (argc < 3) { cout<< "Usage: mini from to" <<endl; return -1; } string dest = string (argv[1]); string src = string (argv[2]); if (src.find_last_of ("/") == src.length () - 1) src = src.substr (0,src.length ()-1); struct stat fileinfo; stat (Src.c_str (), &fileInfo) ; if (S_isreg (fileinfo.st_mode)) { zipfile zfile = zipopen (Dest.c_str (), append_status_create); if (zfile == null) { cout<< "Openfile failed" <<endl; return -1; } zip_fileinfo zfileinfo = { 0 }; int ret = Zipopennewfileinzip (Zfile,src.c_str (), &zfileinfo,null,0,null,0,null,0,z_default_compression); if (RET&NBSP;!=&NBSP;ZIP_OK) { cout<< "OpenFile in zip failed "<<endl; zipclose (zfile,null); return -1; } ret = writeinzipfile (ZFILE,SRC); if (RET&NBSP;!=&NBSP;ZIP_OK) { cout<< "write In zip failed "<<endl; Zipclose (zfile,null); return -1; } zipclose ( Zfile,null); cout<< "Zip ok" <<endl; } else if (S_isdir (fileinfo.st_mode)) { &nbSp; size_t pos = src.find_last_of ("/"); string dirname = src.substr (pos + 1); string dirprefix = src.substr (0,pos); zipfile zfile = zipopen (Dest.c_str (), append_status_create); if (zfile == null) { cout<< "Openfile failed" << endl; return -1; } vector<string> Vfiles; enumdirfiles (dirprefix,dirname,vfiles); &nBsp;vector<string>::iterator itf = vfiles.begin (); for (; Itf != vfiles.end (); ++itf) { zip_fileinfo zfileinfo = { 0 }; int ret = zipopennewfileinzip (Zfile,itf->c_str (), &zfileinfo,null,0,null,0,null,0,z_default_ COMPRESSION); if (ret != &NBSP;ZIP_OK) { cout<< "Openfile in zip failed "<<endl; zipclose (zfile,null); return -1; } ret = writeinzipfile (ZFILE,*ITF); if (RET&NBSP;!=&NBSP;ZIP_OK) { cout<< "Write in zip failed" <<endl; zipclose (ZFile,NULL); return -1; } } &nBsp; zipclose (zfile,null); cout<< " Zip ok "<<ENDL;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;0;}
Target=minilib=-lminizip-laes-lz Libpath=-l/usr/local/libincpath=-i/usr/local/include${target}: g++ main.cpp-g-O [ Email protected] ${lib} ${libpath} ${incpath}clean:-rm ${target}
Minizip C + + compressed files and folders