ZIP_FILE_ADD(3)
π 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. forzip_file_addonly, bestie.ZIP_FL_ENC_GUESSβ guess the encoding ofname(this is the default). (only CP-437 and UTF-8 are recognized.)ZIP_FL_ENC_UTF_8β interpretnameas UTF-8.ZIP_FL_ENC_CP437β interpretnameas 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 azip_file_addorzip_file_replacecall. like, do not do that. hands off π
also please note that when usingzip_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 withzip_file_extra_field_get(3)and restore them afterzip_replacewithzip_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 callednamein the archive. (only applies tozip_file_add(), and only ifZIP_FL_OVERWRITEis not provided). yeah it's taken, sorry.[ZIP_ER_INVAL]βsourceornameareNULL, orindexis 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 π
β 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
- arch rolling: libzip 1.11.4-1 Β· BSD
- fedora 43: libzip-devel 1.11.4-2.fc43 Β· BSD-3-Clause
- fedora 43: libzip-devel 1.11.4-2.fc43 Β· BSD-3-Clause
- fedora 44: libzip-devel 1.11.4-3.fc44 Β· BSD-3-Clause
- fedora 44: libzip-devel 1.11.4-3.fc44 Β· BSD-3-Clause
- nixpkgs unstable: libzip 1.11.4
- ubuntu resolute: libzip-dev 1.11.4-2 Β· BSD-3-Clause
- ubuntu stonking: libzip-dev 1.11.4-2build1 Β· BSD-3-Clause
- Copyright (C) 2004-2021 Dieter Baron and Thomas Klausner
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms