libzip
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <zip.h>
int ziptest(const char* inputPath, const char* outputPath)
{int error = 0;zip_t *zip_file = zip_open(inputPath, ZIP_CHECKCONS, &error);if (zip_file == NULL) {printf("Failed to open zip file: %s\n", zip_strerror(zip_file));return 1;}int numEntries = zip_get_num_entries(zip_file, 0);if (numEntries < 0) {std::cerr << "Failed to get number of entries." << std::endl;zip_close(zip_file);return 1;}zip_t* archive = zip_open(outputPath, ZIP_CREATE | ZIP_TRUNCATE, nullptr);if (archive == nullptr) {std::cout << "无法创建 ZIP 存档." << std::endl;return 1;}unsigned char* itemData = NULL;for (size_t i = 0; i < numEntries; i++) {zip_stat_t entryStat;if (zip_stat_index(zip_file, i, 0, &entryStat) != 0) {std::cerr << "Failed to get information for entry " << i << std::endl;continue;}if (entryStat.valid & ZIP_STAT_NAME) {zip_file_t *entryFile = zip_fopen_index(zip_file, i, 0);if (entryFile == NULL) {std::cerr << "Failed to open entry file: " << entryStat.name << std::endl;continue;}size_t bufferSize = entryStat.size;itemData = (unsigned char *)malloc(bufferSize);if (zip_fread(entryFile, itemData, bufferSize) < 0) {std::cerr << "Failed to read entry file: " << entryStat.name << std::endl;zip_fclose(entryFile);if (itemData != NULL) {free(itemData);itemData = NULL;}continue;}zip_source* source = zip_source_buffer(archive, itemData, bufferSize, 0);if (source == NULL) {std::cout << "无法写入 ZIP 文件." << std::endl;if (itemData != NULL) {free(itemData);itemData = NULL;}zip_fclose(entryFile);continue;}if (zip_file_add(archive, entryStat.name, source, ZIP_FL_OVERWRITE) < 0) {std::cout << "无法写入 ZIP 文件." << std::endl;zip_source_free(source);zip_fclose(entryFile);if (itemData != NULL) {free(itemData);itemData = NULL;}continue;}zip_fclose(entryFile);}}zip_close(zip_file);zip_close(archive);return 0;
}
#include <zip.h>
#include <memory.h>
#include <stdio.h>
#include <math.h>
#include <fstream>#ifdef _WIN32
#include <io.h>
#include<direct.h>
#include<windows.h>
#else
#include<unistd.h>
#include<sys/stat.h>
#include <sys/types.h>
#endif
void createFolders(std::string rootPath);
void getDirAllFilePath(const char* folderPath, std::vector<std::string>& filePaths);
int unzipFunc(const char* destzip, const std::string output) {zip* archive = zip_open(destzip , 0, NULL);if (!archive) {std::cerr << "Failed to open archive" << std::endl;return -1;}int numFiles = zip_get_num_files(archive);for (int i = 0; i < numFiles; ++i) {FILE *fp = NULL;zip_stat_t fileStat;zip_stat_init(&fileStat);if (zip_stat_index(archive, i, 0, &fileStat) != 0) {std::cerr << "Failed to get file info for index " << i << std::endl;continue;}int len = strlen(fileStat.name);std::cout << "File " << i << ": " << fileStat.name << " (" << fileStat.size << " bytes)" << std::endl;std::string dest_name = output + std::string(fileStat.name);createFolders(dest_name);zip_file_t* zf = zip_fopen_index(archive, i, 0);if (!zf) { continue; }fp = fopen(dest_name.c_str(), "wb");if (fp == NULL) { continue; }long long sum = 0;unsigned char* buffer = (unsigned char*)malloc(fileStat.size);memset(buffer, 0, fileStat.size);if (zip_fread(zf, buffer, fileStat.size) < 0) {continue;}fwrite(buffer, 1, fileStat.size, fp);free(buffer);buffer = NULL;zip_fclose(zf);fclose(fp);}if (zip_close(archive) != 0) {std::cerr << "Failed to close archive" << std::endl;return -1;}return 0;
}
int zipFunc(const char* inputDirPath, const char* destzip) {int iErr = 0;zip* archive = zip_open(destzip, ZIP_CREATE | ZIP_TRUNCATE, &iErr);if (!archive) {std::cerr << "Failed to open archive" << std::endl;return -1;}std::vector<std::string> files;getDirAllFilePath(inputDirPath, files);std::string rootPath(inputDirPath);for (size_t i = 0; i < files.size(); i++) {std::string::size_type rootSize = rootPath.length();std::string itemName = files[i].substr(rootSize + 1);std::cout << "entry: " << files[i].c_str() << std::endl;zip_source_t* source = zip_source_file(archive, files[i].c_str(), 0, -1);if (!source){printf(" open zip_source file failed\n");zip_close(archive);return 1;}if (zip_file_add(archive, itemName .c_str(), source, ZIP_FL_OVERWRITE) < 0) {zip_source_free(source);zip_close(archive);return 2;}}if (zip_close(archive) != 0) {std::cerr << "Failed to close archive" << std::endl;return -1;}return 0;
}
int create_dir(const char *dir)
{
#ifdef WIN32if ((_access(dir, 0)) != 0) {int flag = _mkdir(dir);if (flag != 0){printf("Fail to create directory %s.", dir); return OOXML_PARAMETER_ERR;}}
#elseif ((access(dir, 0)) != 0) {int flag = mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);if (flag != 0){printf("Fail to create directory %s.", dir);return OOXML_PARAMETER_ERR;}}
#endifreturn 0;
}
void mkdirRecursively(const std::string root, const std::string folderPath)
{size_t pos = folderPath.find('/');std::string subPath;if (pos != std::string::npos){subPath = folderPath.substr(0, pos);}else {subPath = folderPath;}if (root == "") {if (!subPath.empty()){if(create_dir(subPath.c_str()) != 0) return ;std::string remainingPath = folderPath.substr(pos + 1);if (pos != std::string::npos) {return mkdirRecursively(subPath, remainingPath);}else {return ;}}}else {std::string subdir = root;if (subdir.back() != '/') {subdir += "/";}if (!subPath.empty()){std::string current = subdir + subPath;if (create_dir(current.c_str()) != 0) return ;std::string remainingPath = folderPath.substr(pos + 1);if (pos != std::string::npos) {return mkdirRecursively(current, remainingPath);}else {return ;}}}return ;
}
void createFolders(std::string rootPath)
{std::string::size_type idx = rootPath.find_last_of('/');std::string filename = rootPath.substr(idx + 1);std::string::size_type pos = filename.find('.');if (!filename.empty() && (pos != std::string::npos)) {std::string subdirname = rootPath.substr(0, idx + 1);mkdirRecursively("", subdirname);}else{mkdirRecursively("", rootPath);}
}void getDirAllFilePath(const char* folderPath, std::vector<std::string>& filePaths)
{
#ifdef WIN32HANDLE hFind;WIN32_FIND_DATA findData;LARGE_INTEGER size;char dirNew[128] = { 0x0 };strcpy(dirNew, folderPath);strcat(dirNew, "\\*.*");hFind = FindFirstFile(dirNew, &findData);if (hFind == INVALID_HANDLE_VALUE){std::cerr << "无法打开目录:" << folderPath << std::endl;return;}do{if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY&& strcmp(findData.cFileName, ".") != 0&& strcmp(findData.cFileName, "..") != 0){std::string file(findData.cFileName);std::string newPath = std::string(folderPath) + "/" + file;getFiles(newPath.c_str(), filePaths);}else if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0) {continue;}else {std::string file(findData.cFileName);std::string fullPath = std::string(folderPath) + "/" + file;filePaths.push_back(fullPath);}} while (FindNextFile(hFind, &findData));FindClose(hFind);
#elseDIR* dir = opendir(folderPath);if (dir == nullptr){std::cerr << "无法打开目录:" << folderPath << std::endl;return;}struct dirent* entry;while ((entry = readdir(dir)) != nullptr){std::string entryName(entry->d_name);if (entryName == "." || entryName == ".."){continue;}std::string entryPath(folderPath);if (entryPath.back() != '/') { entryPath += "/"; }entryPath +=entryName;struct stat entryStat;if (stat(entryPath.c_str(), &entryStat) == -1){std::cerr << "无法获取文件属性:" << entryPath << std::endl;continue;}if (S_ISDIR(entryStat.st_mode)){getFiles(entryPath.c_str(), filePaths);}else if (S_ISREG(entryStat.st_mode)){filePaths.push_back(entryPath);}}closedir(dir);
#endif
}