1

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);
xadec4
  • 11
  • 1
  • Why do you think something is wrong with your extraction code? It could simply be that you extract the file correctly but that file cannot be executed on your current machine. – Jesper Juhl Mar 12 '20 at 20:20
  • Because when I manually extract file.exe in the zip archive with WinRAR it has no problem executing on my machine. – xadec4 Mar 12 '20 at 20:26
  • @TedLyngmo I think you're right because when I type 0 for flags it works as expected. Thank you. – xadec4 Mar 12 '20 at 20:31
  • @xadec4 Great. I made that into an answer. Feel free to accept it :-) – Ted Lyngmo Mar 12 '20 at 20:36

1 Answers1

1

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.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108