Skip to contents

archive_write() writes files into a new ZIP or tar archive, one member per file, in the order given. The same files, names, times and compression give the same bytes on every platform: the archive holds nothing about the machine that wrote it.

Usage

archive_write(
  path,
  files,
  format = "zip",
  names = basename(files),
  mtime = "fixed",
  compression = NULL
)

Arguments

path

Where to write the archive: a single path. An existing file there is replaced.

files

The files to archive: a character vector of paths. Each must be a regular file (or a link to one); a directory, device, FIFO or socket is refused before any file is opened. May be empty, which writes an archive with no members.

format

"zip" or "tar".

names

The member names, one per file: a character vector as long as files. By default, the files' base names.

mtime

"fixed"; "file", each file's own modification time (file.mtime()); or the members' modification times: a POSIXct of length one (for every member) or as long as files. Either way each time must lie from 1980-01-01 00:00:00 to 2107-12-31 23:59:58 UTC for ZIP, or from 1970-01-01 00:00:00 to 2242-03-16 12:56:31 UTC for tar.

compression

NULL for the format's default, or how members are stored. ZIP takes "deflate" (the default) or "stored"; members of three bytes or fewer are always stored, as DEFLATE cannot shrink them. tar takes "stored" (the default, as tar does not compress members).

Value

path, invisibly.

Details

Reproducible. Members are written in the order of files, with the names given and nothing about the host (no owner, permissions, host times or ZIP extra fields), compressed at a fixed DEFLATE level, and dated by mtime alone. With mtime = "fixed" (the default) every member is dated 1980-01-01 00:00:00 UTC, the first time a ZIP member can carry, in tar as in ZIP, so writing the same files twice, anywhere, gives identical archives.

tar. With format = "tar" each member is a POSIX ustar header (mode 0644, owner and group 0 with no names) and the file's bytes. A name longer than 100 bytes or not ASCII, and a file of 8 GiB or more, is written with a pax extended header before it, which every current tar reads; the ustar prefix field is never used. The archive ends with the two zero blocks tar requires and is not padded further. Times are whole seconds (a fraction is dropped) from 1970-01-01 00:00:00 to 2242-03-16 12:56:31 UTC, the range of the ustar time field. Members are not compressed: compress the whole archive with another tool if needed.

Times. A ZIP member's time is the MS-DOS date and time, which has no time zone and a resolution of two seconds. zuarchive stores the UTC date and time of mtime, rounded down to an even second, and reads it back as UTC, so archive_list() returns it unchanged in any time zone. To date members as the files are, pass mtime = "file".

ZIP64. The archive uses the ZIP64 records when it needs them, decided from the inputs before anything is written: when it has 65535 members or more, a member of 4 GiB (2^32 - 1 bytes) or more, or members that could add up to that much. Otherwise it is a plain ZIP that any reader opens.

Names must be relative paths that archive_extract() would accept, written in their plain form: no .. component, no leading /, no backslash, colon or NUL, no empty or . component, no trailing /. They are stored in UTF-8, with the archive's UTF-8 flag set. A name can be used once, and cannot also be the directory of another ("a" and "a/b").

The archive is written to a temporary file beside path and renamed into place when it is complete, so a failure leaves no partial archive and does not touch an existing file at path.

Conditions

Each refusal is an error of class zuarchive_error and a subclass:

  • zuarchive_error_path: path is not a single path or names a directory; files is not a character vector without NA or empty strings, or names a directory, a device, a FIFO or a socket (anything but a regular file, or a link to one); names is not a character vector as long as files, or holds a name that is NA, empty, longer than 65535 bytes (in either format), not a safe relative path in its plain form, repeated, or the directory of another name ("a" with "a/b"), which archive_extract() would refuse.

  • zuarchive_error_not_found: a file in files does not exist or cannot be read, or the directory path would go in does not exist.

  • zuarchive_error_format: format, mtime or compression is not one of the values above (with mtime = "file", a file dated outside the format's range, named in the condition), or compression is one the format cannot use ("deflate" for tar); or a member cannot be represented in a ZIP archive as miniz writes one (a DEFLATE member of 4 GiB or more whose compressed bytes would start past the first 4 GiB of the archive, or incompressible data within a few hundred kilobytes under 4 GiB).

A failure to write the archive itself (a full disk) raises zuarchive_error with no subclass. Conditions about one member carry member, its name, and path, its file.

Examples

src <- tempfile()
dir.create(src)
writeLines("Hello, archive.", file.path(src, "hello.txt"))
writeLines(c("x,y", "1,2"), file.path(src, "data.csv"))

out <- tempfile(fileext = ".zip")
archive_write(out, file.path(src, c("hello.txt", "data.csv")))
archive_list(archive_open(out))
#>        name size compressed  method      mtime type encrypted
#> 1 hello.txt   16         21 deflate 1980-01-01 file     FALSE
#> 2  data.csv    8         13 deflate 1980-01-01 file     FALSE

# Dated as the files are, in a directory of the archive:
archive_write(out, file.path(src, c("hello.txt", "data.csv")),
  names = c("docs/hello.txt", "docs/data.csv"), mtime = "file")
archive_list(archive_open(out))
#>             name size compressed  method               mtime type encrypted
#> 1 docs/hello.txt   16         21 deflate 2026-12-05 08:02:24 file     FALSE
#> 2  docs/data.csv    8         13 deflate 2026-12-05 08:02:24 file     FALSE

# The same files as a tar archive:
tar <- tempfile(fileext = ".tar")
archive_write(tar, file.path(src, c("hello.txt", "data.csv")),
  format = "tar")
archive_list(archive_open(tar))
#>        name size compressed method      mtime type encrypted
#> 1 hello.txt   16         16 stored 1980-01-01 file     FALSE
#> 2  data.csv    8          8 stored 1980-01-01 file     FALSE