ZIP_FILE_ADD(3)

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

πŸ’– ZIP_FILE_ADD(3) πŸ’–

okay bestie, gather round, because this is the zip_file_add / zip_file_replace page from the Library Functions Manual and it's literally the glow-up your zip archive has been waiting for ✨


🌸 NAME 🌸

zip_file_add, zip_file_replace β€” add file to zip archive or replace file in zip archive


πŸŽ€ LIBRARY πŸŽ€

libzip (-lzip)


✨ SYNOPSIS ✨

#include <zip.h>

zip_int64_t
zip_file_add(zip_t *archive, const char *name, zip_source_t *source,
    zip_flags_t flags);

int
zip_file_replace(zip_t *archive, zip_uint64_t index,
    zip_source_t *source, zip_flags_t flags);

πŸ’… DESCRIPTION πŸ’…

so here's the tea: zip_file_add() adds a file to a zip archive, and zip_file_replace() replaces a file that's already living in a zip archive. two icons, two jobs, no overlap 🫢

the archive argument is the zip archive you're adding the file to. name is the file's name inside the zip archive (that's the zip_file_add() one), and index says which file gets replaced (that's the zip_file_replace() one). and flags? it can be any combination of ZIP_FL_OVERWRITE with one of the ZIP_FL_ENC_* values:

  • ZIP_FL_OVERWRITE β€” overwrite any existing file of the same name. for zip_file_add only, bestie.
  • ZIP_FL_ENC_GUESS β€” guess the encoding of name (this is the default). (only CP-437 and UTF-8 are recognized.)
  • ZIP_FL_ENC_UTF_8 β€” interpret name as UTF-8.
  • ZIP_FL_ENC_CP437 β€” interpret name as code page 437 (CP-437).

the actual data comes from the source argument, see zip_source(5).

⚠️ NOTE: zip_source_free(3) should not be called on a source after it was used successfully in a zip_file_add or zip_file_replace call. like, do not do that. hands off πŸ™ˆ
also please note that when using zip_replace, the target file's extra field information will be deleted, since this usually is dependent on the file contents. if you want to keep them, query them beforehand with zip_file_extra_field_get(3) and restore them after zip_replace with zip_file_extra_field_set(3). save your faves before you wreck the vibe πŸ’€

🌈 RETURN VALUES 🌈

when it all works out: zip_file_add() returns the index of the new file in the archive, and zip_file_replace() returns 0. icon behaviour, no notes πŸ‘‘

otherwise -1 is returned and the error code in archive is set to indicate the error. not the slay we wanted, but at least it tells you why 😭


πŸ¦‹ EXAMPLES πŸ¦‹

      zip_source_t *s;
      const char buf[]="teststring";

      if ((s=zip_source_buffer(archive, buf, sizeof(buf), 0)) == NULL ||
          zip_file_add(archive, name, s, ZIP_FL_ENC_UTF_8) < 0) {
          zip_source_free(s);
          printf("error adding file: %s\n", zip_strerror(archive));
      }

πŸ’€ ERRORS πŸ’€

zip_file_add() and zip_file_replace() fail if:

  • [ZIP_ER_EXISTS] β€” there is already a file called name in the archive. (only applies to zip_file_add(), and only if ZIP_FL_OVERWRITE is not provided). yeah it's taken, sorry.
  • [ZIP_ER_INVAL] β€” source or name are NULL, or index is invalid. nope, not valid, not today.
  • [ZIP_ER_MEMORY] β€” required memory could not be allocated. she's hungry, she needs RAM πŸ“
  • [ZIP_ER_RDONLY] β€” archive was opened in read-only mode. you literally cannot write here bestie, read the room πŸ“–

πŸ‘‘ SEE ALSO πŸ‘‘

libzip(3), zip_source(5)


⭐ HISTORY ⭐

zip_file_add() and zip_file_replace() were added in libzip 0.11.


🧚 AUTHORS 🧚

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


and that's the whole page, fr fr πŸ’œ this was shipped as part of Debian, dated March 18, 2024 β€” so when you're out here adding files like a main character, just know the icons behind ZIP_FILE_ADD(3) have been holding it down since then. periodt πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from