首页 > 解决方案 > 我无法使用 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);

标签: c++libzip

解决方案


ZIP_FL_COMPRESSED-读取压缩数据。否则数据未压缩...

这意味着您以压缩形式从存档中读取数据。

只需删除该标志即可获得未压缩的内容。


推荐阅读