简体   繁体   中英

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. 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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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