首页 > 解决方案 > 在 C++ 中从 zip 文件写入文件

问题描述

我需要用 C++ 解压缩从 Internet 下载的文件。我已经搜索了几个小时,但找不到任何东西。

目前我正在使用 libzip,并且可以成功读取该文件,但我只是不知道如何将其作为文件夹中的文件写入:

int unzipFile(std::string zipPath) {
    //Open the ZIP archive
    int err = 0;
    zip* z = zip_open(zipPath.c_str(), 0, &err);

    //Search for the file of given name
    const char* name = "AvaloniaLauncher.exe";
    struct zip_stat st;
    zip_stat_init(&st);
    zip_stat(z, name, 0, &st);

    //Alloc memory for its uncompressed contents
    char* contents = new char[st.size];

    //Read the compressed file
    zip_file* f = zip_fopen(z, name, 0);
    zip_fread(f, contents, st.size);
    zip_fclose(f);

    //And close the archive
    zip_close(z);

    //Do something with the contents    
    printf(contents);


    //delete allocated memory
    delete[] contents;

    return err;
}

我只在 12 年的旧线程上找到了这个片段,所以我不会再唤醒它。

我现在有了文件的未压缩内容,但是我将如何编写它呢?谢谢。

标签: c++

解决方案


推荐阅读