16static bool compressFolders(
const std::vector<std::string> &folders, std::string_view zipSaveFolder,
17 std::string_view zipPrefix, std::string_view zipExtension)
20 std::string compressFile = fmt::format(
"{}/{}_{}.{}", zipSaveFolder, zipPrefix, dateString, zipExtension);
32 zip_t *zip = zip_open(compressFile.c_str(), ZIP_CREATE | ZIP_TRUNCATE,
nullptr);
42 std::cout <<
"🐶" << std::endl;
45 std::cout <<
"🐱" << std::endl;
48 std::cout <<
"🐭" << std::endl;
51 for (
const auto &folderSave: folders)
53 std::string pathToFolder {folderSave.substr(0, folderSave.find_last_of(
'/'))};
55 for (
const auto &fileFolderSave: std::filesystem::recursive_directory_iterator(folderSave))
57 if (fileFolderSave.is_regular_file())
59 std::string relativePath {std::filesystem::relative(fileFolderSave.path(), pathToFolder).string()};
60 zip_source_t *source {zip_source_file(zip, fileFolderSave.path().c_str(), 0, 0)};
62 if (source ==
nullptr)
67 if (zip_file_add(zip, relativePath.c_str(), source, ZIP_FL_OVERWRITE) < 0)
69 zip_source_free(source);
71 fmt::format(
"Error adding the file to the zip file: {}", fileFolderSave.path().string()));
74 else if (fileFolderSave.is_directory() and fs::is_empty(fileFolderSave.path()))
76 zip_dir_add(zip, fileFolderSave.path().c_str(), ZIP_FL_ENC_UTF_8);
81 if (zip_close(zip) < 0)
91 const std::unordered_map<std::string, std::string> &compressedFoldersExtractionPaths)
93 zip_t *zip {zip_open(compressedFile.c_str(), ZIP_RDONLY,
nullptr)};
101 int64_t numEntries {zip_get_num_entries(zip, 0)};
103 for (int64_t entryIteration = 0; entryIteration < numEntries; ++entryIteration)
106 zip_stat_index(zip,
static_cast<zip_uint64_t
>(entryIteration), 0, &stat);
108 std::string entryName {stat.name};
109 std::string extractingFolderName {entryName.substr(0, entryName.find_first_of(
'/'))};
110 std::string pathSaveName;
113 std::find_if(compressedFoldersExtractionPaths.begin(), compressedFoldersExtractionPaths.end(),
114 [&extractingFolderName](
const auto &pair) { return pair.first == extractingFolderName; });
116 if (it != compressedFoldersExtractionPaths.end())
122 pathSaveName = entryName;
125 if (pathSaveName.back() ==
'/')
133 std::string folderSaveExtractedFile {pathSaveName.substr(0, pathSaveName.find_last_of(
'/'))};
137 zip_file_t *fileInZip {zip_fopen_index(zip,
static_cast<zip_uint64_t
>(entryIteration), 0)};
139 if (fileInZip ==
nullptr)
145 FILE *extractedFile {fopen(pathSaveName.c_str(),
"wb")};
147 if (extractedFile ==
nullptr)
149 zip_fclose(fileInZip);
155 zip_int64_t bytesRead;
157 while ((bytesRead = zip_fread(fileInZip, buffer,
sizeof(buffer))) > 0)
159 fwrite(buffer, 1,
static_cast<size_t>(bytesRead), extractedFile);
162 fclose(extractedFile);
165 zip_fclose(fileInZip);