在C++中使用minizip库解压文件的方法如下:
#include "unzip.h" int main() { unzFile zipFile = unzOpen("example.zip"); if (zipFile == NULL) { printf("Error opening zip file\n"); return 1; } unz_file_info fileInfo; char filename[256]; unzGetCurrentFileInfo(zipFile, &fileInfo, filename, sizeof(filename), NULL, 0, NULL, 0); if (unzLocateFile(zipFile, filename, 1) != UNZ_OK) { printf("File not found in zip\n"); unzClose(zipFile); return 1; } if (unzOpenCurrentFile(zipFile) != UNZ_OK) { printf("Error opening current file\n"); unzClose(zipFile); return 1; } FILE* outFile = fopen(filename, "wb"); if (outFile == NULL) { printf("Error creating output file\n"); unzCloseCurrentFile(zipFile); unzClose(zipFile); return 1; } char buffer[4096]; int bytesRead; while ((bytesRead = unzReadCurrentFile(zipFile, buffer, sizeof(buffer))) > 0) { fwrite(buffer, 1, bytesRead, outFile); } fclose(outFile); unzCloseCurrentFile(zipFile); unzClose(zipFile); return 0; }
在上面的示例中,我们使用minizip库打开了一个zip文件,然后解压了其中的文件到当前目录。您可以根据自己的需求修改文件路径等参数。