如何在视觉工作室中使用C++解压缩压缩文件 (.zip)

How to unzip a zipped file(.zip) using C++ in visual studio

本文关键字:压缩 解压缩 文件 zip C++ 视觉 工作室      更新时间:2023-10-16

我正在做一个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;
}