我正在开发一个 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/