C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > c++解析zip文件

c++实现解析zip文件的示例代码

作者:spirits_of_snail

这篇文章主要为大家详细介绍了如何利用c++实现解析zip文件,并对流式文件pptx内容的修改,文中的示例代码讲解详细,有需要的小伙伴可以参考一下

libzip

官网地址:

示例代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <zip.h>

//解析原始zip内容,保存为新的zip文件
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文件
	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;
		}
		//printf("index: [%llu]\t", entryStat.index);
		//printf("Name: [%s]\t", entryStat.name);
		//printf("valid: [%llu]\t", entryStat.valid);
		//printf("Size: [%llu]\t", entryStat.size);
		//printf("comp_size: [%llu]\t", entryStat.comp_size);
		//printf("comp_method: [%zu]", entryStat.comp_method);
		//printf("\t flags: [%lu]\n", entryStat.flags);
		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;
				//goto END;
				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 存档中
			//zip_source_buffer内部会自动释放itemData内存
			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;
			}
			//  use zip_file_replace() to modify source zip file
			/****
			if (zip_file_replace(zip_file, i, source, 0) < 0) {
				std::cout << "replace failed." << std::endl;
				zip_source_free(source);
				zip_fclose(entryFile);
				break;
			}
			****/

			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文件
	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> /* _access */
#include<direct.h> /* _mkdir */
#include<windows.h>
#else
#include<unistd.h> /* access */
#include<sys/stat.h> /*mkdir*/
#include <sys/types.h>
#endif 

//创建多级文件夹
void createFolders(std::string rootPath);
//获取多级文件夹下所以文件列表
void getDirAllFilePath(const char* folderPath, std::vector<std::string>& filePaths);

//解压zip,保存到磁盘指定目录
int unzipFunc(const char* destzip, const std::string output) {
	// 打开ZIP文件
	zip* archive = zip_open(destzip , 0, NULL);
	if (!archive) {
		std::cerr << "Failed to open archive" << std::endl;
		return -1;
	}
	// 获取ZIP文件中的文件数量
	int numFiles = zip_get_num_files(archive);
	//std::cout << "Archive contains " << numFiles << " files" << std::endl;

	// 遍历ZIP文件中的所有文件
	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);
	}
	
	// 关闭ZIP文件
	if (zip_close(archive) != 0) {
		std::cerr << "Failed to close archive" << std::endl;
		return -1;
	}
	return 0;
}


//将文件夹压缩为指定的zip
int zipFunc(const char* inputDirPath, const char* destzip) {
	// 打开ZIP文件
	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();
		//获取指定目录下的相对路径,作为zip包的条目名称
		std::string itemName = files[i].substr(rootSize + 1);
		std::cout << "entry: " << files[i].c_str() << std::endl;
		//通过文件创建zip_source源对象
		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;
		}
		//add file
		if (zip_file_add(archive, itemName .c_str(), source, ZIP_FL_OVERWRITE) < 0) {
			zip_source_free(source);
			zip_close(archive);
			return 2;
		}
	}
	// 关闭ZIP文件
	if (zip_close(archive) != 0) {
		std::cerr << "Failed to close archive" << std::endl;
		return -1;
	}
	return 0;
}

//创建文件夹
int create_dir(const char *dir)
{
#ifdef WIN32
	if ((_access(dir, 0)) != 0)	//如果文件夹不存在
	{
		int flag = _mkdir(dir);
		if (flag != 0)
		{
			printf("Fail to create directory %s.", dir); //"Fail to create directory." << std::endl;
			return OOXML_PARAMETER_ERR;
		}
	}
#else
	if ((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;
		}
	}
#endif
	return 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);
		//std::cout << subdirname << std::endl;
		mkdirRecursively("", subdirname);
	}
	else{
		//std::cout << rootPath << std::endl;
		mkdirRecursively("", rootPath);
	}
}



void getDirAllFilePath(const char* folderPath, std::vector<std::string>& filePaths)
{
#ifdef  WIN32
	HANDLE 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
	{
		// 是否是文件夹,并且名称不为"."或".." 
		//std::cout << findData.dwFileAttributes << "\n";
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
			&& strcmp(findData.cFileName, ".") != 0
			&& strcmp(findData.cFileName, "..") != 0
			)
		{
			// 将dirNew设置为搜索到的目录,并进行下一轮搜索 
			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);
#else
	DIR* 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
}

到此这篇关于c++实现解析zip文件的示例代码的文章就介绍到这了,更多相关c++解析zip文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文