R: Compress Files into 'zip' Archives

zip {zip} | R Documentation

## Compress Files into 'zip' Archives

### Description

`zipr` and `zip` both create a new zip archive file.

### Usage

    zip(zipfile, files, recurse = TRUE, compression_level = 9,
      include_directories = TRUE)

    zipr(zipfile, files, recurse = TRUE, compression_level = 9,
      include_directories = TRUE)

    zip_append(zipfile, files, recurse = TRUE, compression_level = 9,
      include_directories = TRUE)

    zipr_append(zipfile, files, recurse = TRUE, compression_level = 9,
      include_directories = TRUE)

### Arguments

`zipfile` | The zip file to create. If the file exists, `zip` overwrites it, but `zip_append` appends to it.
`files` | List of file to add to the archive. See details below about absolute and relative path names.
`recurse` | Whether to add the contents of directories recursively.
`compression_level` | A number between 1 and 9. 9 compresses best, but it also takes the longest.
`include_directories` | Whether to explicitly include directories in the archive. Including directories might confuse MS Office when reading docx files, so set this to `FALSE` for creating them.

### Details

`zipr_append` and `zip_append` append compressed files to an existing 'zip' file.

### Value

The name of the created zip file, invisibly.

### Permissions

`zipr()` (and `zip()`, `zipr_append()`, etc.) add the permissions of the archived files and directories to the ZIP archive, on Unix systems. Most zip and unzip implementations support these, so they will be recovered after extracting the archive.

Note, however that the owner and group (uid and gid) are currently omitted, even on Unix.

### Relative paths

The different between `zipr` and `zip` is how they handle the relative paths of the input files.

For `zip` (and `zip_append`), the root of the archive is supposed to be the current working directory. The paths of the files are fully kept in the archive. Absolute paths are also kept. Note that this might result non-portable archives: some zip tools do not handle zip archives that contain absolute file names, or file names that start with `..//` or `./`. This behavior is kept for compatibility, and we suggest that you use `zipr` and `zipr_append` for new code.

E.g. for the following directory structure:

    foo
      bar
        file1
      bar2
        file2
    foo2
      file3

Assuming the current working directory is `foo`, the following zip entries are created by `zip`:

    zip("x.zip", c("bar/file1", "bar2", "../foo2"))
    zip_list("x.zip")$filename
    #> bar/file1
    #> bar2
    #> bar2/file2
    #> ../foo2
    #> ../foo2/file3

For `zipr` (and `zipr_append`), each specified file or directory in `files` is created as a top-level entry in the zip archive. We suggest that you use `zipr` and `zipr_append` for new code, as they don't create non-portable archives. For the same directory structure, these zip entries are created:

    zipr("x.zip", c("bar/file1", "bar2", "../foo2"))
    zip_list("x.zip")$filename
    #> file1
    #> bar2
    #> bar2/file2
    #> foo2
    #> foo2/file3

Because of the potential issues with `zip()` and `zip_append()`, they are now soft-deprecated, and their first use in the R session will trigger a reminder message. To suppress this message, you can use something like this:

    withCallingHandlers(
      zip::zip(...),
      deprecated = function(e) NULL)

### Examples

    ## Some files to zip up
    dir.create(tmp <- tempfile())
    cat("first file", file = file.path(tmp, "file1"))
    cat("second file", file = file.path(tmp, "file2"))

    zipfile <- tempfile(fileext = ".zip")
    zipr(zipfile, tmp)

    ## List contents
    zip_list(zipfile)

    ## Add another file
    cat("third file", file = file.path(tmp, "file3"))
    zipr_append(zipfile, file.path(tmp, "file3"))
    zip_list(zipfile)

[Package zip version 2.0.4 Index]