1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/zip.R
\name{zip}
\alias{zip}
\alias{zipr}
\alias{zip_append}
\alias{zipr_append}
\title{Compress Files into 'zip' Archives}
\usage{
zip(
zipfile,
files,
recurse = TRUE,
compression_level = 9,
include_directories = TRUE,
root = ".",
mode = c("mirror", "cherry-pick")
)
zipr(
zipfile,
files,
recurse = TRUE,
compression_level = 9,
include_directories = TRUE,
root = ".",
mode = c("cherry-pick", "mirror")
)
zip_append(
zipfile,
files,
recurse = TRUE,
compression_level = 9,
include_directories = TRUE,
root = ".",
mode = c("mirror", "cherry-pick")
)
zipr_append(
zipfile,
files,
recurse = TRUE,
compression_level = 9,
include_directories = TRUE,
root = ".",
mode = c("cherry-pick", "mirror")
)
}
\arguments{
\item{zipfile}{The zip file to create. If the file exists, \code{zip}
overwrites it, but \code{zip_append} appends to it. If it is a directory
an error is thrown.}
\item{files}{List of file to add to the archive. See details below
about absolute and relative path names.}
\item{recurse}{Whether to add the contents of directories recursively.}
\item{compression_level}{A number between 1 and 9. 9 compresses best,
but it also takes the longest.}
\item{include_directories}{Whether to explicitly include directories
in the archive. Including directories might confuse MS Office when
reading docx files, so set this to \code{FALSE} for creating them.}
\item{root}{Change to this working directory before creating the
archive.}
\item{mode}{Selects how files and directories are stored in
the archive. It can be \code{"mirror"} or \code{"cherry-pick"}.
See "Relative Paths" below for details.}
}
\value{
The name of the created zip file, invisibly.
}
\description{
\code{zip()} creates a new zip archive file.
}
\details{
\code{zip_append()} appends compressed files to an existing 'zip' file.
\subsection{Relative paths}{
\code{zip()} and \code{zip_append()} can run in two different modes: mirror
mode and cherry picking mode. They handle the specified \code{files}
differently.
\subsection{Mirror mode}{
Mirror mode is for creating the zip archive of a directory structure,
exactly as it is on the disk. The current working directory will
be the root of the archive, and the paths will be fully kept.
zip changes the current directory to \code{root} before creating the
archive.
E.g. consider the following directory structure:
\if{html}{\out{<div class="sourceCode">}}\preformatted{.
|-- foo
| |-- bar
| | |-- file1
| | `-- file2
| `-- bar2
`-- foo2
`-- file3
}\if{html}{\out{</div>}}
Assuming the current working directory is \code{foo}, the following zip
entries are created by \code{zip}:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{setwd("foo")
zip::zip("../test.zip", c("bar/file1", "bar2", "../foo2"))
#> Warning in warn_for_dotdot(data$key): Some paths reference parent directory,
#> creating non-portable zip file
zip_list("../test.zip")[, "filename", drop = FALSE]
#> # A data frame: 4 x 1
#> filename
#> <chr>
#> 1 bar/file1
#> 2 bar2/
#> 3 ../foo2/
#> 4 ../foo2/file3
}\if{html}{\out{</div>}}
Note that zip refuses to store files with absolute paths, and chops
off the leading \code{/} character from these file names. This is because
only relative paths are allowed in zip files.
}
\subsection{Cherry picking mode}{
In cherry picking mode, the selected files and directories
will be at the root of the archive. This mode is handy if you
want to select a subset of files and directories, possibly from
different paths and put all of the in the archive, at the top
level.
Here is an example with the same directory structure as above:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{zip::zip(
"../test2.zip",
c("bar/file1", "bar2", "../foo2"),
mode = "cherry-pick"
)
zip_list("../test2.zip")[, "filename", drop = FALSE]
#> # A data frame: 4 x 1
#> filename
#> <chr>
#> 1 file1
#> 2 bar2/
#> 3 foo2/
#> 4 foo2/file3
}\if{html}{\out{</div>}}
From zip version 2.3.0, \code{"."} has a special meaning in the \code{files}
argument: it will include the files (and possibly directories) within
the current working directory, but \strong{not} the working directory itself.
Note that this only applies to cherry picking mode.
}
}
\subsection{Permissions:}{
\code{zip()} (and \code{zip_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.
}
\subsection{\code{zipr()} and \code{zipr_append()}}{
These function exist for historical reasons. They are identical
to \code{zip()} and \code{zipr_append()} with a different default for the
\code{mode} argument.
}
}
\examples{
## Some files to zip up. We will run all this in the R session's
## 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)
## Add another file
cat("third file", file = file.path(tmp, "mydir", "file3"))
zip_append(zipfile, file.path("mydir", "file3"), root = tmp)
zip_list(zipfile)
}
|