c++ - 我无法使用 libzip 正确提取可执行文件
问题描述
我正在尝试使用下面的代码从 zip 存档中提取可执行文件,但由于某种原因,它没有正确地将文件输出到磁盘。当我尝试运行提取的文件时,出现错误“此应用程序无法在您的 PC 上运行” 提取时文件大小为 309 KB,因此看起来所有数据都在那里。我的代码有什么问题?当我手动提取它时它运行得很好。此外,当我尝试提取 .txt 文件时,它会为每个换行符写入 2 个换行符,而不是 1 个。
int error = 0;
zip *z = zip_open("pathtozip.zip", 0, &error);
struct zip_stat st;
zip_stat_init(&st);
zip_stat(z, "file.exe", 0, &st);
char *contents = new char[st.size];
zip_file *f = zip_fopen(z, "file.exe", ZIP_FL_COMPRESSED);
zip_fread(f, contents, st.size);
zip_fclose(f);
if (std::ofstream("C:\\users\\admin\\desktop\\test.exe", std::ofstream::binary).write(contents, st.size))
std::cout << "File Extracted" << std::endl;
zip_close(z);
解决方案
ZIP_FL_COMPRESSED
-读取压缩数据。否则数据未压缩...
这意味着您以压缩形式从存档中读取数据。
只需删除该标志即可获得未压缩的内容。
推荐阅读
- c# - Unity3D安卓USB通讯
- android - 通过 Android 项目的源代码控制共享代码样式时遇到问题 (Android Studio)
- sql - 限制 SQL Server 中动态 SQL 的执行时间
- react-native - 我不能创建一个 react-native 项目?
- delphi - 升级 Indy 库以使用最新的 OpenSSL 库
- c# - 匹配第二个实例
- c++ - Cmake:包含目录-父目录
- sharepoint - CSOM 为 SharePoint 列表上的组授予编辑权限
- arrays - 如何从 Postgres 函数返回新创建的 ids 数组?
- angular - PrimeNG ConfirmDialog 中的 acceptLabel、acceptVisible 不起作用