概述
zlib
库是一个广泛使用的压缩/解压缩库- 下面实现是用了
zlib 1.3
版本的代码和相关库 - 具体是使用了
zlib
源码中带的minizip
- 下面实现是用了
- 其中的
minizip
扩展提供了处理ZIP
文件的功能
解压
- 在解压之前可能需要处理其他的很多工作,比如验证目录,验证目标文件是否存在等。这里只记述了具体的解压实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
void unzip_helper::unzip(const char* zip_filename, const char* dest_dir, const char* appid) { do { unzFile zipfile = unzOpen(zip_filename); if (zipfile == NULL) { break; } unz_global_info gi; if (unzGetGlobalInfo(zipfile, &gi) != UNZ_OK) { unzClose(zipfile); break; } bool flag = false; char filename[256]; char outputFilePath[1024]; for (int i = 0; i < static_cast<int>(gi.number_entry); i++) { if (unzGetCurrentFileInfo(zipfile, NULL, filename, sizeof(filename), NULL, 0, NULL, 0) != UNZ_OK) { flag = true; break; } snprintf(outputFilePath, sizeof(outputFilePath), "%s\\%s", dest_dir, filename); size_t len = strlen(outputFilePath); if ((outputFilePath[len - 1] == '\\') || (outputFilePath[len - 1] == '/')) { auto tmp = UTF8ToWStr(outputFilePath); #if defined(WIN32) || defined(__WIN32) || defined(__WIN32__) || defined(__NT__) if (CreateDirectory(tmp.c_str(), NULL) == 0) { flag = true; break; } #elif defined(__APPLE__) || defined(__linux__) std::string temp = to_str(tmp.c_str()); if (temp.empty()) { flag = true; break; } auto mask = umask(0); if (mkdir(temp.c_str(), 0777) != 0) { umask(mask); flag = true; break; } umask(mask); #endif } else { if (unzOpenCurrentFile(zipfile) != UNZ_OK) { flag = true; break; } FILE* outputFile = fopen(outputFilePath, "wb"); if (outputFile == NULL) { unzCloseCurrentFile(zipfile); flag = true; break; } char buffer[4096]; int bytesRead; while ((bytesRead = unzReadCurrentFile(zipfile, buffer, sizeof(buffer))) > 0) { if (fwrite(buffer, 1, bytesRead, outputFile) != bytesRead) { flag = true; break; } } fclose(outputFile); unzCloseCurrentFile(zipfile); } if (flag) { break; } if ((i + 1) < (int)gi.number_entry) { if (unzGoToNextFile(zipfile) != UNZ_OK) { flag = true; break; } } } if (flag) { if (zipfile) { unzClose(zipfile); } break; } if (m_unz) { m_unz->unzip_status(install_type::em_it_unzip_success, appid); } return ; } while (false); if (m_unz) { m_unz->unzip_status(install_type::em_it_unzip_other_err, appid); } } |
unzOpen
函数是minizip
中用于打开ZIP
文件的函数,先打开压缩文件unzGetGlobalInfo
是minizip
中的一个函数,用于获取一个ZIP
文件的全局信息,例如该ZIP文件中的文件数量、注释等unzGetCurrentFileInfo
是minizip
库中的一个函数,用于获取当前打开的ZIP
文件中当前条目的详细信息- 这可以帮助您了解每个条目(文件或目录)的属性,如文件名、压缩和未压缩大小、修改时间等
- 根据当前条目的类型,进程操作,如果判定是文件夹,则创建文件夹。如果判定是文件,则按文件来处理
unzOpenCurrentFile
是zlib
库中的一个函数,用于打开ZIP
文件中的当前条目(如一个文件)以进行解压缩- 在
ZIP
文件中遍历条目时,如果想解压缩某个特定的条目 - 首先需要使用
unzOpenCurrentFile
打开该条目 - 然后,使用
unzReadCurrentFile
从该条目中读取数据,直到读取完整个条目 - 最后,使用
unzCloseCurrentFile
关闭该条目
- 在
unzReadCurrentFile
是zlib
库中的一个函数,具体地说,是其minizip
扩展中的一个函数- 这个函数用于从
ZIP
文件的当前条目中读取数据,也就是解压当前选定的文件或数据
- 这个函数用于从
unzCloseCurrentFile
是zlib
的一个函数,特定于处理zip
文件格式。使用zlib
来解压一个zip
文件时,你会经历以下基本步骤:- 打开
zip
文件(unzOpen
) - 可选地检索
zip
文件中的全局信息(例如,通过unzGetGlobalInfo
) - 遍历
zip
文件中的每个条目(通常是文件和/或目录) - 对于每个条目:
打开该条目(unzOpenCurrentFile
)
读取该条目的数据(例如,通过unzReadCurrentFile
)
关闭该条目(unzCloseCurrentFile
) - 关闭
zip
文件(unzClose
)
- 打开
unzGoToNextFile
是zlib
中用于处理zip
文件格式的函数。它是在解压zip
文件时使用的,具体用于迭代zip
文件中的条目
压缩
- 待续
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ STL_queue06/07
- ♥ COM组件_303/07
- ♥ CommandLine库gflags使用01/12
- ♥ C++14_第一篇12/14
- ♥ 编译器扩展语法:一07/06
- ♥ Effective C++_第五篇07/02