Skip to contents

archive_extract() writes members of a ZIP or tar archive under a directory, and never anywhere else: every member's path is checked before anything is written, and a member whose path could land outside exdir refuses the whole extraction.

Usage

archive_extract(x, exdir, members = NULL, links = FALSE)

Arguments

x

A handle from archive_open(), or what it takes: a path to an archive or a raw vector holding one (opened with the default archive_limits()).

exdir

The directory to write into: a single path. It is created if it does not exist.

members

NULL to extract every member, or the names of the members to extract, as archive_list() shows them.

FALSE to refuse symbolic and hard link members, or TRUE to write those that stay inside exdir, as described above.

Value

The paths written, invisibly: exdir joined with each extracted member's cleaned name, in the archive's order. Directory and link members are included. A member is written under the UTF-8 bytes of its name in every locale; off Windows, in a locale that is not UTF-8, a path whose name is not ASCII is returned as a native string holding those bytes, so that file.exists() and the other file functions of R find the file.

Details

Paths. A member name is split on /, and empty and . components are dropped (a//./b is written as a/b). The extraction is refused, before anything is written, when a member to extract has a name with a .. component, a leading / (an absolute path), a backslash (a separator on Windows), a colon (a drive letter such as C:, or a Windows stream), a NUL byte, or nothing left once cleaned; and on Windows, also a name that Windows would alias: a device name such as CON or aux.c, or a component ending in a dot or a space. So are two members that clean to the same path, and a member whose path another member needs as a directory.

Links. By default (links = FALSE) a symbolic link or hard link member refuses the extraction. With links = TRUE:

  • a symbolic link (tar, or ZIP, where the link's target is the member's bytes) is created when its target, read from the link's own directory, stays inside exdir: not absolute, .. only at its start and no more of them than the link has directories above it, and not exdir itself. The target is checked as written, never by following links on disk, and is written in its plain form (./a//b as a/b). A member to be written through a link created earlier is refused, as any link in the way is.

  • a tar hard link is written as a copy of the member it names, which must be a file extracted earlier in the same call.

An absolute target, or one leading out of exdir, refuses the extraction whatever links says. On Windows, creating a symbolic link takes a privilege (or Developer Mode); without it, a symbolic link member raises an error naming the member and the privilege.

Nothing is overwritten. Each file and link is created anew: a file, directory or link already at a member's path refuses that member, and so does a symbolic link among the directories above it. On a file system that ignores case, two names differing only in case are caught this way, when the second is written. Directories that exist already are used.

Checked first. Before anything is written, every member to extract is checked as archive_read() checks it: limits, encryption, compression method. A member found corrupt while it is written (a CRC-32 mismatch, a broken DEFLATE stream) stops the extraction: its file is removed, and the members written before it stay, listed in the condition's written field. A tar archive is walked whole when it is opened, so a tar member cut short by the end of the archive refuses the extraction before anything is written.

Member times and permissions are not restored: each file is written as the process creates files.

Conditions

Every refusal is an error of class zuarchive_error and a subclass. Those of archive_open() and archive_read(), and:

  • zuarchive_error_path: a member's name is not a safe relative path, two members would be written to one path, a member is a link and links is FALSE, a link's target leads outside exdir or a hard link names no file extracted before it, a symbolic link cannot be created for lack of privilege, something is already at a member's path; exdir is not a single path or names a file; members is not a character vector without NA or repeated names; links is not TRUE or FALSE.

  • zuarchive_error_not_found: a name in members is no member of the archive.

  • zuarchive_error_format: the archive is a Compound File, whose stream names may hold control characters that some file systems cannot store, so this version reads them with archive_read() only; or a tar member is a device, a FIFO or a sparse file.

A failure to create a directory or write a file raises zuarchive_error with no subclass. Each condition carries member, the member's name, where one member is at fault, and written, the paths written before the failure.

Examples

zip <- system.file("extdata", "hello.zip", package = "zuarchive")
out <- tempfile()
archive_extract(zip, out)
readLines(file.path(out, "hello.txt"), warn = FALSE)
#> [1] "hello"