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

use*_*006 2 c++ zip

对于一个项目,必须阅读一些zip文件.一切都很好但是什么时候想要从zip文件中的文件夹中读取它不起作用.或者我只是不知道zip如何在c ++中工作.香港专业教育学院在互联网上搜索,找不到答案.

Jam*_*ess 6

据我所知,过去使用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);
}
Run Code Online (Sandbox Code Playgroud)

我目前无法测试此代码,因此可能存在一些错误,但希望您能够看到它应该如何工作的一般概念.