2

I have started working with the libzip library today. But I do not understand the principle how libzip works.

My focus is on zipping a directory with all the files and dirs within into a zip-file.

Therefore, I started with zip_open(), then I read the directory contents and add all the dirs with zip_dir_add() to the archive. After that, I closed the zip-file with zip_close(). Everything was fine. The next step should be to add all the files to the archive with zip_file_add(). But it doesn't work. The last step closing the file fails.

OK, I forgot to create a zip_source to get this done. I added a statement a line before to get this source (zip_source_file()). But still it doesn't work.

What is wrong in my thinking? Do I have to fopen() and fclose() the file on the filesystem also?

And what is the difference between zip_source_file() and zip_source_filep()?

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
tria1312
  • 153
  • 1
  • 1
  • 8
  • zip_close would be last, after adding all files, structure etc. The zip itself is a file with a pointer, on closing said pointer, you will no longer be able to write to the pointer, and so you cannot add files after closing. – MiltoxBeyond Nov 18 '15 at 20:04
  • OK, I know. Perhaps I didn't write it. But I did it first step: zip_open(), zip_dir_add(), zip_close(). Next step was zip_open(), zip_dir_add(), zip_source_file(), zip_file_add(), zip_close(). But one closing I get a error = -1. – tria1312 Nov 18 '15 at 20:55
  • Hey @tria1312 did you find any solution? i have a similar problem. – Rahul Kumar Apr 02 '18 at 12:46
  • 1
    Posting the source code would be a good starting point. – Andrejs Cainikovs Jul 30 '18 at 08:27

1 Answers1

1

Do I have to fopen() and fclose() the file on the filesystem also?

No, you can just use zip_source_file().

From your comments I think you have the right general idea, but there is probably some detail that is making it fail. Make sure you perform all the error checking the documentation suggests after each libzip call so you can get more information about what is causing it to fail.

You could also compare your code with https://gist.github.com/clalancette/bb5069a09c609e2d33c9858fcc6e170e

RyanCu
  • 516
  • 4
  • 12