15 #include <minizip/unzip.h> 
   16 #include <minizip/zip.h> 
   33     if (zfile == 
nullptr || dirname.empty())
 
   37     temp = 
new char[len + 2];
 
   38     memcpy(temp, dirname.c_str(), len);
 
   39     if (temp[len - 1] != 
'/')
 
   49     ret = zipOpenNewFileInZip64(zfile,
 
   65     zipCloseFileInZip(zfile);
 
   85     int ret = zipOpenNewFileInZip64(zfile,
 
   86                                     (zipPath + filename).c_str(),
 
   94                                     Z_DEFAULT_COMPRESSION,
 
   95                                     (size > 0xffffffff) ? 1 : 0);
 
   99         zipClose(zfile, 
nullptr);
 
  105     while ((n = fs.readsome(buf, 
sizeof(buf))) > 0)
 
  107         ret = zipWriteInFileInZip(zfile, buf, (
unsigned int)n);
 
  110             zipCloseFileInZip(zfile);
 
  114     zipCloseFileInZip(zfile);
 
  129     std::ifstream fs(filepath, std::ios::binary);
 
  151            function<
bool(
string path, 
string filename)>   processFile,
 
  152            function<
bool(
const char* data, 
size_t len)>   writeChunk,
 
  153            function<
bool(
string path)>                    processDir,
 
  154            function<
int(
int currentFile, 
int totalFiles)> progress = 
nullptr)
 
  160     int     nbProcessedFile = 0;
 
  162     uzfile = unzOpen64(zipfile.c_str());
 
  167     unz_global_info global_info;
 
  168     if (unzGetGlobalInfo(uzfile, &global_info) != UNZ_OK)
 
  176         unz_file_info64 finfo;
 
  177         unsigned char   buf[8192];
 
  178         if (unzGetCurrentFileInfo64(uzfile,
 
  195         if (progress != 
nullptr && progress(nbProcessedFile++, (
int)global_info.number_entry))
 
  201         if (finfo.uncompressed_size == 0 && strlen(name) > 0 && name[strlen(name) - 1] == 
'/')
 
  203             if (unzGoToNextFile(uzfile) != UNZ_OK)
 
  208         if (unzOpenCurrentFile(uzfile) != UNZ_OK)
 
  211             unzCloseCurrentFile(uzfile);
 
  215         if (processFile(dirname, filename))
 
  217             while ((n = unzReadCurrentFile(uzfile, buf, 
sizeof(buf))) > 0)
 
  219                 if (!writeChunk((
const char*)buf, n))
 
  221                     unzCloseCurrentFile(uzfile);
 
  227             writeChunk(
nullptr, 0);
 
  230                 unzCloseCurrentFile(uzfile);
 
  236         unzCloseCurrentFile(uzfile);
 
  237         if (unzGoToNextFile(uzfile) != UNZ_OK)
 
  244     if (progress != 
nullptr)
 
  245         progress((
int)global_info.number_entry, (
int)global_info.number_entry);
 
  255 bool zip(
string path, 
string zipname)
 
  260         zipname = path + 
".zip";
 
  262     zipFile zfile = zipOpen64(zipname.c_str(), 0);
 
  264     if (zfile == 
nullptr)
 
  266         zipClose(zfile, 
nullptr);
 
  277        zipRootPath](
string path,
 
  284                                                zipRootPath.size()));
 
  286       [zfile, &ret, zipRootPath](
string path,
 
  291                                    path.erase(0, zipRootPath.size()) + baseName);
 
  302     zipClose(zfile, NULL);
 
  317            function<
int(
int currentFile, 
int totalFiles)> progress)
 
  324       [&fs, &
override, dest](
string path, 
string filename) -> 
bool 
  328               fs.open(dest + path + filename, std::ios::binary);
 
  333       [&fs](
const char* data, 
size_t len) -> 
bool 
  341               catch (std::exception& e)
 
  343                   std::cout << e.what() << std::endl;
 
  351       [dest](
string path) -> 
bool 
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
 
bool fileExists(const string &pathfilename)
Returns true if a file exists.
 
bool dirExists(const string &path)
Returns true if a directory exists.
 
bool makeDir(const string &path)
Creates a directory with given path.
 
unsigned int getFileSize(const string &pathfilename)
Returns the file size in bytes.
 
string getFileName(const string &pathFilename)
Returns the filename of path-filename string.
 
string getDirName(const string &pathFilename)
Strip last component from file name.
 
void removeFile(const string &path)
RemoveFile deletes a file with given path.
 
string trimRightString(const string &s, const string &drop)
trims a string at the right end
 
void loopFileSystemRec(const string &path, function< void(string path, string baseName, int depth)> processFile, function< void(string path, string baseName, int depth)> processDir, const int depth)
process all files and folders recursively naturally sorted
 
ZipUtils provides compressing & decompressing files and folders.
 
static bool zip_add_dir(zipFile zfile, string dirname)
 
static bool zip_add_file(zipFile zfile, std::ifstream &fs, string filename, string zipPath="")
 
bool zip(string path, string zipname)
 
bool unzip(string zipfile, function< bool(string path, string filename)> processFile, function< bool(const char *data, size_t len)> writeChunk, function< bool(string path)> processDir, function< int(int currentFile, int totalFiles)> progress=nullptr)