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

标签 c++ zip

对于一个项目来说,必须读取一些 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/

相关文章:

linux - 如何解决 linux 操作系统中的 "unzip: cannot find or open"错误

c++ - 覆盖左手运算符(operator)的可能性?

javascript - javascript 上等效的 php 的 gzuncompress

C++ 什么时候标准库容器中的 typedef 不是您所期望的?

c++ - 错误 : invalid use of incomplete type (Maybe a definition issue)

python - Tarfile/Zipfile extractall() 更改某些文件的文件名

python - 如何将 csv 文件直接压缩成 zip 存档?

python - Pandas:如何加载包含多个 txt 文件的 zip 文件?

c++ - 将字符串解析为数字或数字和百分号

c++ - 使用 Boost::Python::Object 会导致链接器错误