Search code examples
c++windows-10libzip

Unable to read archive files from an archive created from code


I am pushing files to an archive using libzip / c++ and so far so good. I can open the created archive with the WinRar Windows client, unpack the archive and read every file without a problem.

But when attempting to read some files (the ones in subfolders for some reason) from the archive with c++ / libzip, it seems libzip just can't locate them and my returned zip_file struct is NULL. I am 100% sure the paths to those files are correct.

Fun fact : if I unpack the created archive manually and manually repack it with the WinRar client, I have then no problem reading the archive with libzip.

I tried getting info on the error here with this code :

if (isNull(zf)) { // zf is of type - struct zip_file* -
    zip_error_t ze;
    zip_error_init(&ze);
    zip_error_code_system(&ze);
    zip_error_code_zip(&ze);
    const char* err = zip_error_strerror(&ze);
    ...
}

No error codes are registered and the err message is No error.

What is going on here ?


Solution

  • I found the problem with this and a solution.

    When pushing files to subfolders with libzip, one has to also create each directory separately within the archive with zip_dir_add(...).

    Even tho not doing so won't generate errors when adding files (and e.g. the WinRar client is able to see files through their own directories), libzip won't be able to read those files unless each directory has been pushed separately.

    TLDR:

    Parse each pushed file for their directory structure and add each subfolder individually with zip_dir_add().