---
name: "zip_file_replace"
section: "3"
platform: "linux"
package: "libzip"
version: "1.11.4-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/zip_file_replace"
---

# zip\_file\_replace(3)

add file to zip archive or replace file in zip archive

## 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

The function **zip\_file\_add**() adds a file to a zip archive, while **zip\_file\_replace**() replaces an existing file in a zip archive. The argument *archive* specifies the zip archive to which the file should be added. *name* is the file's name in the zip archive (for **zip\_file\_add**()), while *index* specifies which file should be replaced (for **zip\_file\_replace**()). The *flags* argument can be any combination of ZIP\_FL\_OVERWRITE with one of ZIP\_FL\_ENC\_\*:

- ZIP\_FL\_OVERWRITE

  Overwrite any existing file of the same name. For **zip\_file\_add** only.

- ZIP\_FL\_ENC\_GUESS

  Guess encoding of *name* (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 data is obtained from the *source* argument, see [zip\_source(5)](/zip_source/5.md).

*NOTE*: [zip\_source\_free(3)](/zip_source_free/3.md) should not be called on a *source* after it was used successfully in a **zip\_file\_add** or **zip\_file\_replace** call.

Please also 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)](/zip_file_extra_field_get/3.md) and restore them after **zip\_replace** with [zip\_file\_extra\_field\_set(3)](/zip_file_extra_field_set/3.md).

## RETURN VALUES

Upon successful completion, **zip\_file\_add**() returns the index of the new file in the archive, and **zip\_file\_replace**() returns 0. Otherwise, -1 is returned and the error code in *archive* is set to indicate the error.

## 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).

- \[ZIP\_ER\_INVAL\]

  *source* or *name* are NULL, or *index* is invalid.

- \[ZIP\_ER\_MEMORY\]

  Required memory could not be allocated.

- \[ZIP\_ER\_RDONLY\]

  Archive was opened in read-only mode.

## SEE ALSO

[libzip(3)](/libzip/3.md), [zip\_source(5)](/zip_source/5.md)

## 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*⟩
