ZIP_FOPEN_ENCRYPTED(3)

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

✨ ZIP_FOPEN_ENCRYPTED πŸ’…

okay so this is the ZIP_FOPEN_ENCRYPTED(3) page from the Library Functions Manual, and bestie it's giving "how do i read a password-protected file out of my zip archive" energy and honestly? iconic.


πŸŽ€ NAME πŸŽ€

zip_fopen_encrypted, zip_fopen_index_encrypted β€” open an encrypted file in a zip archive for reading. yeah. that's the whole job. πŸ”


πŸ“š LIBRARY πŸ“š

libzip (-lzip) β€” that's the link flag, write it down, you'll need it.


🌸 SYNOPSIS 🌸

#include <zip.h>

zip_file_t *
zip_fopen_encrypted(zip_t *archive, const char *fname,
    zip_flags_t flags, const char *password);

zip_file_t *
zip_fopen_index_encrypted(zip_t *archive, zip_uint64_t index,
    zip_flags_t flags, const char *password);

like. four args. two functions. we got this. ✨


πŸ’– DESCRIPTION πŸ’–

so zip_fopen_encrypted() opens the encrypted file named fname inside archive, using the password you hand it in the password argument. that's it, that's the vibe. 🫢

and listen β€” if password is NULL or an empty string, it just falls back to the default password (peep zip_set_default_password(3) for that one, iykyk). the flags argument? literally the same as for zip_fopen(3). no new rules, we stay consistent. πŸ‘‘

meanwhile zip_fopen_index_encrypted() opens the file sitting at position index β€” go look at zip_fopen_index(3) if you need the tea on that. πŸ¦‹

πŸ’€ and here's the plot twist: these functions get called automatically by zip_fopen(3). so you only ever need to call them yourself if you want to specify a non-default password. see zip_set_default_password(3). that's the whole reason they exist. main character energy only when necessary. πŸ’…

🌈 RETURN VALUES 🌈

if it slays (successful completion), you get a struct zip_file pointer back. πŸ₯³

if it does not slay, you get NULL, and the error code in archive gets set so you know exactly what went wrong. no mystery, no guessing. 😭


πŸ”₯ ERRORS πŸ”₯

  • [ZIP_ER_NOPASSWD] β€” no password was provided. bestie. you forgot the password. πŸ™ˆ

also, zip_fopen_encrypted() may fail and set zip_err for any of the errors specified for the routine zip_fopen(3).

and zip_fopen_index_encrypted() may fail and set zip_err for any of the errors specified for the routine zip_fopen_index(3).


πŸ‘€ SEE ALSO πŸ‘€

libzip(3), zip_fclose(3), zip_fopen(3), zip_fread(3), zip_get_num_entries(3), zip_name_locate(3)

(the whole friend group, fr fr)


⭐ HISTORY ⭐

zip_fopen_encrypted() and zip_fopen_index_encrypted() were added in libzip 1.0. vintage. classic. OG status. 🧚


πŸ’œ AUTHORS πŸ’œ

Dieter Baron <dillo@nih.at> and Thomas Klausner <wiz@gatalith.at>

say thank you to the devs, they ate and left no crumbs. πŸ“


🚨 CAVEATS 🚨

okay this part is important so lock in, no cap. πŸ”’

the zip file format provides very limited possibility for password verification (a short hash of is compared against one byte in the zip archive). one. single. byte. πŸ’€

and because of that: reading a file while using an incorrect password may immediately fail with ZIP_ER_WRONGPASSWD β€” but if the mismatch isn't detected, a zlib error may be returned later instead. 😭

and since zlib errors can also be caused by broken compressed data, there is no way to make sure if the password was incorrect or if it was correct but the compressed data was invalid. that's not a bug, that's just how the format is built. periodt. 🫠


and that's the page!! from the Debian build, dated September 15, 2020 β€” stay encrypted, stay hydrated, and don't forget your password this time πŸ’–βœ¨πŸŽ€