解压缩可以用一些库,我用了minizip库,我知道大家不喜欢废话,所以
```cpp
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <zip.h>
#pragma comment(lib,"libzip.lib")
void extractArchive(const char* archivePath, const char* outputPath) {
// 打开压缩包
zip* archive = zip_open(archivePath, 0, NULL);
if (archive) {
// 获取压缩包中的条目数
int numEntries = zip_get_num_entries(archive, 0);
// 遍历压缩包中的每个条目并解压缩
for (int i = 0; i < numEntries; i++) {
const char* entryName = zip_get_name(archive, i, 0);
//extractEntry(archive, entryName, outputPath);
// 获取指定条目在压缩包中的索引
int entryIndex = zip_name_locate(archive, entryName, 0);
if (entryIndex >= 0) {
// 获取指定条目的信息
struct zip_stat entryStat;
zip_stat_init(&entryStat);
zip_stat_index(archive, entryIndex, 0, &entryStat);
// 判断条目类型
if (entryStat.name[strlen(entryStat.name) - 1] == '/') {
// 如果是文件夹,则创建对应的目录
std::string folderPath = outputPath;
folderPath += "/";
folderPath += entryName;
WCHAR wszClassName[256];
memset(wszClassName, 0, sizeof(wszClassName));
MultiByteToWideChar(CP_ACP, 0, folderPath.c_str(), strlen(folderPath.c_str()) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));
CreateDirectory(wszClassName,NULL);
}
else {
// 如果是文件,则解压缩该文件
zip_file* file = zip_fopen_index(archive, entryIndex, 0);
if (file) {
// 创建输出文件流
std::string filePath = outputPath;
filePath += "/";
filePath += entryName;
std::ofstream outputFile(filePath, std::ios::binary);
if (outputFile) {
// 创建缓冲区来存储文件内容
unsigned char* buffer = new unsigned char[entryStat.size];
// 读取文件内容到缓冲区
zip_fread(file, buffer, entryStat.size);
// 将缓冲区内容写入输出文件流
outputFile.write(reinterpret_cast<const char*>(buffer), entryStat.size);
outputFile.close();
// 删除缓冲区内存
delete[] buffer;
}
// 关闭文件流
zip_fclose(file);
}
}
}
}
// 关闭压缩包
zip_close(archive);
}
}
```
点赞过5速更下一期
下一期:修改注册表