功能实现需要依赖相关头文件和库文件,我这里的是64位的。需要的可以在这下载:https://download.csdn.net/download/bangtanhui/88403596
参考代码如下:
#include <zip.h>
#pragma comment(lib,"libzip.lib")
//解压压缩包
//传参:压缩包路径、目标文件夹路径
void DriverList::unZip(const char* zipName, const char* dirName) {
int iErr = 0;
struct zip* zipfile = NULL;
struct zip_file* entries = NULL;
struct zip_stat stat;
zip_int64_t i64Num = 0;
zip_int64_t i64Count = 0;
int iRead = 0;
int iLen = 0;
char buf[1024];
memset(&stat, 0, sizeof(stat));
memset(buf, 0, sizeof(buf));
zipfile = zip_open(zipName, ZIP_CHECKCONS, &iErr);
if (!zipfile)
{
printf("zip open failed:%d\n", iErr);
exit(EXIT_FAILURE);
}
//get how many entrrites in archive
i64Num = zip_get_num_entries(zipfile, 0);
for (i64Count = 0; i64Count < i64Num; i64Count++)
{
iLen = 0;
if (zip_stat_index(zipfile, i64Count, 0, &stat) == 0)
{
printf("the file name is:%s\n", stat.name);
}
entries = zip_fopen_index(zipfile, i64Count, 0);
if (!entries)
{
printf("fopen index failed\n");
goto End;
}
//create the original file
char filePath[MAX_PATH]{ 0 };
strcpy(filePath, dirName);
strcat(filePath, "/");
strcat(filePath, stat.name);
FILE* fp = fopen(filePath, "wb+");
if (!fp)
{
printf("create local file failed\n");
goto End;
}
while (iLen < stat.size)
{
iRead = zip_fread(entries, buf, 1024);
if (iRead < 0)
{
printf("read file failed\n");
fclose(fp);
goto End;
}
fwrite(buf, 1, iRead, fp);
iLen += iRead;
}
fclose(fp);
}
End:
zip_close(zipfile);
}
工程的“包含目录”和“库目录”需要包含相关路径
程序运行可能还需要依赖当前exe路径下相关dll文件