Documents, packages and data exports usually come in containers: a
.docx is a ZIP, a source package is a .tar.gz,
and a legacy .xls is a Compound File. zuarchive reads all
three member by member, with one bounded reader that treats every input
as hostile. This page uses one small ZIP with two members:
src <- tempfile()
dir.create(src)
writeLines("Hello, archive.", file.path(src, "hello.txt"))
writeLines(c("x,y", "1,2"), file.path(src, "data.csv"))
zipfile <- file.path(tempdir(), "demo.zip")
archive_write(zipfile, file.path(src, c("hello.txt", "data.csv")),
format = "zip", names = c("hello.txt", "data.csv"))Open, list and read
archive_open() returns a handle. Members are read one at
a time, as raw bytes or as text, without extracting anything to
disk.
z <- archive_open(zipfile)
archive_list(z)
#> name size compressed method mtime type encrypted
#> 1 hello.txt 16 21 deflate 1980-01-01 file FALSE
#> 2 data.csv 8 13 deflate 1980-01-01 file FALSE
archive_read(z, "hello.txt", as = "text")
#> [1] "Hello, archive.\n"The format comes from the bytes
The input is a path or a raw vector in memory, and the format is detected from its content, never from the file name.
bytes <- readBin(zipfile, "raw", file.size(zipfile))
archive_format(bytes)
#> [1] "zip"
archive_read(archive_open(bytes), "data.csv", as = "text")
#> [1] "x,y\n1,2\n"Hostile input is bounded and classed
Every reader enforces the limits of archive_limits().
Here a handle on the same bytes allows members of at most 4 bytes, and
data.csv holds 8, so reading it is refused before any byte
is inflated:
small <- archive_open(bytes, limits = archive_limits(max_member = 4))
archive_read(small, "data.csv")
#> Error in `archive_read()`:
#> ! Refusing member "data.csv" of the archive: it exceeds `max_member` (uncompressed bytes of one member). See archive_limits().A refusal is a zuarchive_error with a subclass, so a
caller can tell a limit from a malformed or encrypted archive:
tryCatch(
archive_read(small, "data.csv"),
zuarchive_error_limit = function(e) class(e)
)
#> [1] "zuarchive_error_limit" "zuarchive_error" "error"
#> [4] "condition"archive_extract() writes the members under
exdir, after checking that no member path would land
outside it. It returns the paths it wrote, invisibly:
exdir <- tempfile()
written <- archive_extract(zipfile, exdir = exdir)
basename(written)
#> [1] "hello.txt" "data.csv"
readLines(file.path(exdir, "hello.txt"))
#> [1] "Hello, archive."The same inputs give the same bytes
With fixed modification times, writing the same files twice produces identical archives on every platform.
files <- file.path(src, c("hello.txt", "data.csv"))
a <- tempfile(fileext = ".zip")
b <- tempfile(fileext = ".zip")
archive_write(a, files, format = "zip", mtime = "fixed", compression = "deflate")
archive_write(b, files, format = "zip", mtime = "fixed", compression = "deflate")
identical(readBin(a, "raw", file.size(a)), readBin(b, "raw", file.size(b)))
#> [1] TRUEtar works the same way
format = "tar" writes a POSIX tar, and the same
functions read it. Links in an archive are refused on extraction unless
links = TRUE, and never point outside
exdir.
tarfile <- tempfile(fileext = ".tar")
archive_write(tarfile, files, format = "tar", names = c("hello.txt", "data.csv"))
t <- archive_open(tarfile)
archive_list(t)[, c("name", "size", "type")]
#> name size type
#> 1 hello.txt 16 file
#> 2 data.csv 8 file
archive_read(t, "data.csv", as = "text")
#> [1] "x,y\n1,2\n"A tar compressed with gzip, zstd or lz4 (.tar.gz,
.tar.zst, .tar.lz4) reads the same way: the
compression is found from the bytes, and archive_format()
names the tar inside it.
tgz <- tempfile(fileext = ".tar.gz")
con <- gzfile(tgz, "wb")
writeBin(readBin(tarfile, "raw", file.size(tarfile)), con)
close(con)
archive_format(tgz)
#> [1] "tar"
archive_read(archive_open(tgz), "hello.txt", as = "text")
#> [1] "Hello, archive.\n"Where next
-
archive_limits()lists each limit, its default and what it counts. -
archive_extract()gives the path rules and how links are handled. -
archive_list()says how each format’s members are named, Compound File streams included. - The reference index lists every function.