简体   繁体   English

我无法使用 libzip 正确提取可执行文件

[英]I am unable to extract an executable correctly with libzip

I'm trying to extract an executable file from a zip archive using the code below but for some reason it is not outputting the file to disk correctly.我正在尝试使用下面的代码从 zip 存档中提取可执行文件,但由于某种原因,它没有将文件正确输出到磁盘。 When I try to run the the extracted file I get an error "This app can't run on your PC" The filesize is 309 KB when extracted so it looks like all the data is there.当我尝试运行提取的文件时,出现错误“此应用程序无法在您的 PC 上运行”提取时文件大小为 309 KB,因此看起来所有数据都在那里。 What is wrong with my code?我的代码有什么问题? It runs just fine when I manually extract it.当我手动提取它时,它运行得很好。 Also, when I try to extract a .txt file it writes 2 newlines for each newline instead of 1.此外,当我尝试提取 .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 - Read the compressed data. ZIP_FL_COMPRESSED -读取压缩数据。 Otherwise the data is uncompressed ...否则数据未压缩...

This means that you reading the data from the archive in its compressed form.这意味着您以压缩形式从存档中读取数据。

Just remove that flag to get the uncompressed content.只需删除该标志即可获得未压缩的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM