zip | R Documentation |
This is currently a mechanism to create a zip archive file and insert one or more files or in-memory content into it. This works with C code rather than relying on an external executable/program.
One specifies a list or vector of elements to add to the file along
with the name of the file to create. The elements to be added
can be file names (regular character strings) or AsIs elements
(created with I()
) or raw vectors containing binary content.
Each element is added to the archive using a name coming from
the name of the vector/list of elements or altNames
.
This allows us to give the elements names in the archive that differ
from the names of files or objects.
In the future, I will add facilities to create this in richer ways and to add, replace and remove entries in the archive directly.
zip(zipfile, files = NULL, append = file.exists(zipfile),
compression_level = 9, altNames = names(files), time = Sys.time(),
comments = character(), globalComment = character())
zipfile |
the name of the new file to create |
files |
a character vector giving the names of the files to be added.
These are the full paths to files on the file system.
One can provide alternative names for the entries in the zip archive via
the names of this vector or explicitly via In the future, we will allow this to be a list and the elements can be file names,
character strings containing the content,
|
append |
a logical value indicating whether to append to |
compression_level |
an integer between 1 and 9 indicating the compression level, with higher values meaning higher compression |
altNames |
a character vector with as many elements as there are elements in
|
time |
a vector of POSIXct times specifying the "creation" or "insertion" times for the specified files. This is recycled, and it defaults to the current time and date. |
comments |
a character vector of comments, either empty or one string for each entry being added to the Zip file. |
globalComment |
a character string (character vector of length 1) used as a comment for the entire zip archive. |
A logical value TRUE
if successful, or an error is raised.
Duncan Temple Lang by adapting code from Gilles Vollant.
miniunzip code by Gilles Vollant http://www.winimage.com/zLibDll/minizip.html
zipArchive
tar
# zip("foo.zip", c("zip.xml", "FAQ.html"))
ff = sapply(c("DESCRIPTION", "NAMESPACE"), system.file, package = "Rcompression")
zip("Rcompression.zip", ff)
ar = zipArchive("Rcompression.zip")
names(ar)
ar[["DESCRIPTION"]]
zip("Rcompression.zip", ff, altNames = c("A", "B")) # appending
ar = zipArchive("Rcompression.zip")
names(ar)
ar[["DESCRIPTION"]] == ar[["A"]] # should be same but FALSE.
xx = strsplit(c(ar[["DESCRIPTION"]], ar[["A"]]), "")
all(xx[[1]] == xx[[2]])
zip("Rcompression.zip", ff, altNames = c("A", "B"), append = FALSE)
ar = zipArchive("Rcompression.zip")
names(ar)
files = list.files(system.file("Meta", package = "Rcompression"), full.names = TRUE)
zip("meta.zip", files)
names(zipArchive("meta.zip"))
names(files) = gsub("^.*Rcompression[/\\]", "", files)
zip("meta.zip", files, append = FALSE)
names(zipArchive("meta.zip"))
# a character vector enclosed in I()
content = I(c( myFile = "This is raw text", otherFile = paste(letters, collapse = "\n")))
zip("asis.zip", content, append = FALSE)
z = zipArchive("asis.zip")
names(z)
z[[1]]
# A list with elements which are each I(character) and a file name
content = list( myFile = I("This is raw text"),
otherFile = I(paste(letters, collapse = "\n")),
system.file("Meta", "nsInfo.rds", package = "Rcompression"))
zip("asis.zip", content, append = FALSE)
# Here we add a binary file. It is another zip file.
f = system.file("sampleData", "MyZip.zip", package = "Rcompression")
rw = readBin(f, "raw", file.info(f)[1, "size"])
content$raw = rw
zip("asis.zip", content, append = FALSE)
# Now we read this newly created zip file and extract each of the elements
z = zipArchive("asis.zip")
names(z)
z[["myFile"]]
z[["otherFile"]]
# Here we get the zip file we put into the zip file.
rr = z[["raw", mode = "raw"]]
# It is a raw vector now, but we can treat it as a Zip
# archive by using it as an in-memory archive.
other = zipArchive(rr)
names(other)
other[["NAMESPACE"]]
# The labels we use for the names can have / in them. They are just
# labels to us. But when the external unzip facilities see these,
# they will create the corresponding directories.
content = list( "A/myFile" = I("This is raw text"), "B/otherFile" = I(paste(letters, collapse = "\n")), top = I("A string"))
zip("dir.zip", content)
#
content = list( myFile = I("This is raw text"), otherFile = I(paste(letters, collapse = "\n")))
zip("asis.zip", content, comments = c("Hi there", "And another comment"), append = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.