如何在visual studio中使用C ++解压缩压缩文件(.zip)

时间:2018-01-10 07:26:09

标签: c++ visual-studio unzip compression

我正在开发一个需要解压缩功能的C ++项目。

我在互联网上搜索并发现 zlib 可能有用,但事实证明zlib只提供C语言版本,而C ++版本仅适用于Linux。

我还发现MSDN有自己的API:解压缩& 压缩功能,但在我尝试使用解压缩功能解压缩压缩文件后,我发现MSDN“解压缩”功能仅对由自己的MSDN压缩功能压缩的文件有用。

换句话说,如果我有一个.zip文件,我就无法使用MSDN API对其进行解压缩。

希望有人有任何想法帮助我,非常感谢!!!

1 个答案:

答案 0 :(得分:1)

你尝试过libzip吗?这是一个例子。您还应该从Github找到一些包装器,例如libzippp

bool unzip(const std::wstring &zipPath, const std::wstring &desPath)
{
    int err;
    struct zip *hZip = zip_open_w(zipPath.c_str(), 0, &err);
    if (hZip)
    {
        size_t totalIndex = zip_get_num_entries(hZip, 0);
        for (size_t i = 0; i < totalIndex; i++)
        {
            struct zip_stat st;
            zip_stat_init(&st);
            zip_stat_index(hZip, i, 0, &st);

            struct zip_file *zf = zip_fopen_index(hZip, i, 0);
            if (!zf)
            {
                zip_close(hZip);
                return false;
            }

            std::vector<char> buffer;
            buffer.resize(st.size);
            zip_fread(zf, buffer.data(), st.size);
            zip_fclose(zf);

            // your code here: write buffer to file
            // desPath
            // st.name: the file name

        }
        zip_close(hZip);
    }
    return true;
}