Skip to contents

unzip() always restores modification times of the extracted files and directories.

Usage

unzip(zipfile, files = NULL, overwrite = TRUE, junkpaths = FALSE, exdir = ".")

Arguments

zipfile

Path to the zip file to uncompress.

files

Character vector of files to extract from the archive. Files within directories can be specified, but they must use a forward slash as path separator, as this is what zip files use internally. If NULL, all files will be extracted.

overwrite

Whether to overwrite existing files. If FALSE and a file already exists, then an error is thrown.

junkpaths

Whether to ignore all directory paths when creating files. If TRUE, all files will be created in exdir.

exdir

Directory to uncompress the archive to. If it does not exist, it will be created.

Permissions

If the zip archive stores permissions and was created on Unix, the permissions will be restored.

Examples

## temporary directory, to avoid messing up the user's workspace.
dir.create(tmp <- tempfile())
dir.create(file.path(tmp, "mydir"))
cat("first file", file = file.path(tmp, "mydir", "file1"))
cat("second file", file = file.path(tmp, "mydir", "file2"))

zipfile <- tempfile(fileext = ".zip")
zip::zip(zipfile, "mydir", root = tmp)

## List contents
zip_list(zipfile)
#>      filename compressed_size uncompressed_size           timestamp permissions
#> 1      mydir/               0                 0 2024-01-27 11:33:18         755
#> 2 mydir/file1              15                10 2024-01-27 11:33:18         644
#> 3 mydir/file2              16                11 2024-01-27 11:33:18         644
#>      crc32 offset
#> 1 00000000      0
#> 2 00effe3a     36
#> 3 735af9a0    108

## Extract
tmp2 <- tempfile()
unzip(zipfile, exdir = tmp2)
dir(tmp2, recursive = TRUE)
#> [1] "mydir/file1" "mydir/file2"