简体   繁体   中英

Trouble opening zip files with libzip

I'm trying to open a zip archive in C++ using LibZip. Because I didn't find any working tutorials on that, I looked up the documentation and came to this code:

int err = 0;
zip_t *z = zip_open("file.zip", 0, &err);

zip_stat_t st;
zip_stat_init(&st);
cout << zip_stat(z, "file.zip", 0, &st) << endl;

cout << st.size << endl;

zip_close(z);

The problem is, that the zip_stat function returns -1 and the size of the file is zero. err always is 0, so I have no clue were the problem is. The code to get to the content would have looked like this:

char *contents = new char[st.size];

zip_file *f = zip_fopen(z, "file.zip", 0);
zip_fread(f, contents, st.size);
zip_fclose(f);

delete[] contents;

But obviously I couldn't test this part of the code yet. I'm using C++ 14 and LibZip 1.5.2

zip_stat does not return information about a zip file, but about one of the entries inside a zip file. So unless your file.zip file contains another file also called file.zip your code is mistaken.

Maybe you really meant

cout << zip_stat(z, "data.zip", 0, &st) << endl;

since you seem to be trying to decompress a zip file entry called data.zip.

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