ZIP_OPEN(3)

πŸ“¦
version 1.11.4 Β· 4 versions
, in arch rolling, fedora 43, fedora 44, nixpkgs unstable, ubuntu resolute, ubuntu stonking

✨ ZIP_OPEN(3) β€” the "open a zip archive" moment ✨

okay bestie, welcome to section 3 of the manual, ZIP_OPEN(3) πŸ’… this is a Library Functions Manual page and it's all about opening zip archives. pull up a chair, we're doing this together 🫢


🌸 NAME 🌸

zip_open, zip_open_from_source β€” open zip archive

πŸŽ€ LIBRARY πŸŽ€

libzip (-lzip)

like, that's the whole library. she's iconic, she's small, she does everything ✨

πŸ¦‹ SYNOPSIS πŸ¦‹

#include <zip.h>

zip_t *
zip_open(const char *path, int flags, int *errorp);

zip_t *
zip_open_from_source(zip_source_t *zs, int flags, zip_error_t *ze);

πŸ’– DESCRIPTION πŸ’–

so zip_open() opens the zip archive that path points at and hands you back a pointer to a struct zip β€” that's your little handle, your main character object, the thing you use to actually manipulate the archive πŸ‘‘

the flags? you specify them by or'ing the values below together. or just pass 0 if you want none of them. no thoughts, head empty, defaults only πŸ’…

  • ZIP_CHECKCONS β€” do extra, stricter consistency checks on the archive, and error out if they fail. she's thorough. she's checking. πŸ”
  • ZIP_CREATE β€” create the archive if it doesn't exist yet. baby's first zip 🍼
  • ZIP_EXCL β€” error if the archive already exists. exclusive behaviour, literally, that's the name πŸ’…
  • ZIP_TRUNCATE β€” if the archive exists, ignore whatever's currently in it. in other words: handle it the same way as an empty archive. starting fresh, no baggage 🌈
  • ZIP_RDONLY β€” open the archive in read-only mode. look but don't touch, bestie πŸ™ˆ

if an error happens and errorp is non-NULL, it gets set to the corresponding error code. so like, always pass a pointer if you care. and you should care πŸ₯Ί

now zip_open_from_source() β€” same energy, different entrance. it opens a zip archive that's encapsulated by the zip_source zs, using the flags you provide. and if things go sideways, the zip_error ze gets filled in. no secrets here ✨

πŸ‘‘ RETURN VALUES πŸ‘‘

when it works? zip_open() and zip_open_from_source() both return a struct zip pointer. slay.

when it doesn't? NULL comes back. and then:

  • zip_open() sets *errorp to indicate the error
  • zip_open_from(source) sets ze to indicate the error

(yes, the original really writes it as zip_open_from(source) β€” we keep the facts exactly as they are, no editing the lore πŸ’œ)

πŸ“ EXAMPLES πŸ“

here's how you could report errors during zip_open, bestie. take notes πŸ“

zip_t *za;
int err;

if ((za = zip_open(name, 0, &err)) == NULL) {
    zip_error_t error;
    zip_error_init_with_code(&error, err);
    fprintf(stderr, "%s: cannot open zip archive '%s': %s\n",
            progname, name, zip_error_strerror(&error));
    zip_error_fini(&error);
    return -1;
}

see how she initialises the error, prints it all pretty, and then cleans up with zip_error_fini? that's self-care. that's hygiene. that's a girl who finishes her routines 🧚

πŸ’€ ERRORS πŸ’€

the archive specified by path is opened unless… one of these little guys shows up:

  • [ZIP_ER_EXISTS] β€” the file specified by path exists and ZIP_EXCL is set. you said exclusive and she said okay then, no πŸ’…
  • [ZIP_ER_INCONS] β€” inconsistencies were found in the file specified by path. this error is often caused by specifying ZIP_CHECKCONS, but it can also happen without it. so like, don't assume it's your fault? but also it might be 😭
  • [ZIP_ER_INVAL] β€” the path argument is NULL. bestie. you gave her nothing.
  • [ZIP_ER_MEMORY] β€” required memory could not be allocated. the vibes were too big for RAM πŸ’€
  • [ZIP_ER_NOENT] β€” the file specified by path does not exist and ZIP_CREATE is not set. nothing there, and you didn't ask her to make one πŸ₯Ί
  • [ZIP_ER_NOZIP] β€” the file specified by path is not a zip archive. it's giving… something else entirely πŸ™ˆ
  • [ZIP_ER_OPEN] β€” the file specified by path could not be opened.
  • [ZIP_ER_READ] β€” a read error occurred; see errno for details. go check errno, she's holding the receipts πŸ“œ
  • [ZIP_ER_SEEK] β€” the file specified by path does not allow seeks.
⚠️ for newly created archives, zip_open() does not try to create the file. that happens later, when you call zip_close(3). and any errors β€” like missing write permissions β€” get reported then. so don't come for zip_open() about it, she literally wasn't involved yet πŸ’…

πŸ”₯ SEE ALSO πŸ”₯

libzip(3), zip_close(3), zip_error_strerror(3), zip_fdopen(3), zip_source(5)

go read those too, iykyk πŸ“š

⭐ HISTORY ⭐

zip_open() and zip_open_from_source() were added in libzip 1.0. vintage. classic. still eating πŸ’…

🌈 AUTHORS 🌈

Dieter Baron <dillo@nih.at> and Thomas Klausner <wiz@gatalith.at> β€” legends, honestly, thank you for your service 🫢


and that's the page, bestie!! this was Debian's build, dated May 5, 2025, from ZIP_OPEN(3) πŸ’– go forth and open some archives, you absolute icon πŸ‘‘βœ¨

πŸ“¦ where this page comes from