c++ - 如何在 Visual Studio 中使用 C++ 解压缩压缩文件 (.zip)

标签 c++ visual-studio unzip compression

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

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

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

换句话说,如果我有一个 .zip 文件,我无法使用 MSDN API 来解压缩它。

希望大家有什么想法可以帮助我,非常感谢!

最佳答案

你尝试过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;
}

关于c++ - 如何在 Visual Studio 中使用 C++ 解压缩压缩文件 (.zip),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182274/

相关文章:

maven - 使用 Maven 解压 zip 中的内部 zip

c++ - 如何通过复制和引用将 vector 传递给函数?

c# - 将应用程序部署到 Windows Phone 时出错

c# - 如何构建自己的自定义代码片段?

c# - aspx页面的代码隐藏不在页面层次结构下

windows-mobile - 如何在 Windows Mobile 中解压缩 .zip 文件?

c++ - 在第一个可用索引处将对象设置为 C 数组

c++ - _M_ 尝试实现多线程队列时出现 construct null not valid 错误

c++ - 继承构造函数、默认构造函数和可见性

javascript - 使用 Javascript 如何从使用 PhoneGap 构建的应用程序在移动设备上解压缩文件?