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"))archive_open() returns a handle. Members are read one at
a time, as raw bytes or as text, without extracting anything to
disk.
The input is a path or a raw vector in memory, and the format is detected from its content, never from the file name.
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:
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] TRUEformat = "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.
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.