溫馨提示×

c++ minizip解壓文件的方法是什么

c++
小樊
146
2024-08-13 00:04:45
欄目: 編程語言

在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文件,然后解壓了其中的文件到當前目錄。您可以根據自己的需求修改文件路徑等參數。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女