csizeofstrlenlibzip

How can I get zip_stat_t for large files


For small files I can get the the zip_stat_t information, but if its a file 40Mb big I can't.

zip_stat_t info;
zip_stat_index(zipfile, 544, ZIP_FL_ENC_GUESS, &info);
printf("%s\n", info.name);

eg printing info.name is segfaulting for large files eg 40mb file. A 2Mb file will open with no problems. How can I get the size of info.name, for example since it it seems the info struct isn't being stored properly in RAM?

If I do printf(strlen(info.name)) it results in a segmentation fault.


Solution

  • What you experience is probably a failure of the info retrieval, so that the name field was invalid causing the segfault. The documentation doesn't mention size limits like those you experience.

    To avoid this behavior is recommended to check the return value of zip_stat_index():

    Upon successful completion 0 is returned. Otherwise, -1 is returned and the error information in archive is set to indicate the error.

    if (zip_stat_index(zipfile, 544, ZIP_FL_ENC_GUESS, &info) == 0 )
    {
        printf("%s\n", info.name);
    }
    

    Why did the index retrieval fail? Possibly the index provided to the function (in your case 544) is not present in the archive. In order to prevent this kind of "not found index" issues you can obtain the index of a given file using zip_name_locate () function:

    int zip_name_locate(struct zip *archive, const char *fname, int flags);
    

    which returns either the index of fname within archive or -1 if the file is not found.

    Alternatively you can just use zip_stat() instead of zip_stat_index() which saves one step as it allows to ask for a file name as a parameter:

    int zip_stat(struct zip *archive, const char *fname, int flags, struct zip_stat *sb);