gpt4 book ai didi

c++ - 如何使用 minizip 读取子文件夹中的文件

转载 作者:搜寻专家 更新时间:2023-10-31 00:16:21 46 4
gpt4 key购买 nike

对于一个项目来说,必须读取一些 zip 文件。一切都很好,但是什么时候想从 zip 文件中的文件夹中读取这是行不通的。或者我只是不知道 zip 在 C++ 中是如何工作的。我在互联网上搜索过,找不到答案。

最佳答案

据我记忆过去使用 minizip 时,文件夹层次结构中的所有文件都会同时返回。您只需要与每个文件的路径名进行比较,找出哪些与您要阅读的文件夹相匹配。

zipFile zip = unzOpen(zipfilename); 
if (zip) {
if (unzGoToFirstFile(zip) == UNZ_OK) {
do {
if (unzOpenCurrentFile(zip) == UNZ_OK) {
unz_file_info fileInfo;
memset(&fileInfo, 0, sizeof(unz_file_info));

if (unzGetCurrentFileInfo(zip, &fileInfo, NULL, 0, NULL, 0, NULL, 0) == UNZ_OK) {
char *filename = (char *)malloc(fileInfo.size_filename + 1);
unzGetCurrentFileInfo(zip, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
filename[fileInfo.size_filename] = '\0';

// At this point filename contains the full path of the file.
// If you only want files from a particular folder then you should compare
// against this filename and discards the files you don't want.
if (matchFolder(filename)) {
unsigned char buffer[4096];
int readBytes = unzReadCurrentFile(zip, buffer, 4096);
// Do the rest of your file reading and saving here.
}

free(filename);
}

unzCloseCurrentFile(zip);
}
} while (unzGoToNextFile(zip) == UNZ_OK);
}
unzClose(zip);
}

我目前无法测试此代码,因此可能存在一些错误,但希望您能大致了解它应该如何工作。

关于c++ - 如何使用 minizip 读取子文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16386335/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com