Search code examples
c++ziparchiveunziplibzip

libzip function to find index of filename doesn't work c++


I started learning libzip today everything is going great, it's just that I can't get the zip_name_locate to work and I can't find any tutorials/examples/documentation that explains it(well the documentation does but I can't get it to work). Documentation 1, Documentation 2 ( I can't get any of these to work, the Documentation 2 page also has some enums that apparently haven't been declared in my scope)

So first, my zip archive opens correctly as I can replace and add files to it. I've got terminal output that shows the paths of the indexes (from the zip_get_name function).

Number of files in archive: 3
dummy/
dummy/dummy1/
dummy/dummy1/dummytxt

No matter which one of these I try to find the index of, I always get the index of "dummy/" which is 0, I've also tried with all different enums (ZIP_FL_COMPRESSED, ZIP_FL_ENCRYPTED, ZIP_FL_NOCASE, ZIP_FL_NODIR, ZIP_FL_RECOMPRESS, ZIP_FL_UNCHANGED and 0).

(This "dummy/dummy1/" returns 0, which according to zip_get_name is "dummy/")

I would be very grateful for any help.


Solution

  • Alright so I found a "workaround" using the zip_stat structure.

    struct zip_stat stat;
    zip_stat_init(&stat);
    zip_stat(zipPointer, "dummy/dummy1/dummytxt", 0, &stat);
    std::cout << "name= " <<stat.name << ", index= " << stat.index << std::endl;
    

    This would print "name= dummy/dummy1/dummytxt, index= 2". You can also use "zip_stat_index" to build the zip_stat structure by the index instead. Here you have some documentation for the zip_stat struct and what you can use it for, it seems very useful. Documentation1, Documentation2, Documentation3.

    Oh and also if you have problems that the debugger says that stat has not been declared in this scope make sure you write it like this: struct zip_stat stat;. You've probably missed the struct in front of the declaration.