archive_open() opens a ZIP, tar or Compound File archive from a path or a raw vector and returns a handle. archive_format() names the format from the bytes’ magic numbers, never from the file name, and archive_limits() sets the limits the readers enforce.
Refusals are conditions of class zuarchive_error, with the subclasses zuarchive_error_format, _path, _not_found, _limit, _malformed, _crc and _encrypted.
Reading
archive_list() lists an archive’s members (name, sizes, method, modification time, type, encryption); archive_read() returns one member as a raw vector or as UTF-8 text; archive_stream() hands a member to a callback in chunks.
ZIP: stored, DEFLATE and Zstandard (method 93) members, ZIP64 included, with every CRC-32 checked and every archive_limits() limit enforced before anything is inflated. Encrypted members raise zuarchive_error_encrypted, other compression methods zuarchive_error_format, corrupt archives zuarchive_error_malformed and a CRC mismatch zuarchive_error_crc.
tar: ustar, pax (extended and global headers), GNU (long names, base-256 numbers), star and V7 archives, every header checked as Go’s archive/tar checks it before anything is read. archive_list()’s type includes "hardlink" and "other", and pax times keep their fraction of a second. GNU sparse files are listed but not read, and devices and FIFOs are not read or extracted (zuarchive_error_format).
A tar compressed with gzip, zstd or lz4 (.tar.gz, .tgz, .tar.zst, .tar.lz4) reads as the plain tar would, the compression found from the bytes; archive_format() says "tar". The whole stream is decoded and checked before anything is read, in constant memory: a stream that is corrupt, cut short or followed by other bytes raises zuarchive_error_malformed, one whose checksum fails zuarchive_error_crc. Concatenated gzip members and zstd or lz4 frames read as one stream. max_total also bounds the bytes the stream decodes to and max_ratio those over the file’s. bzip2, xz and compress are zuarchive_error_format.
Compound Files ([MS-CFB], “OLE2”: legacy .doc, .xls, .ppt, encrypted Office files), versions 3 and 4. Storages and streams are listed by path from the root entry ("Root Entry/WordDocument"), storages as directories; a stream reads by that path, matched byte for byte. Every sector chain is checked before any byte is handed over, and every archive_limits() limit but max_ratio applies. Loops, sectors past the end, two names equal but for case and other corruption raise zuarchive_error_malformed; a name with a NUL or /zuarchive_error_path.
Extracting
archive_extract() writes a ZIP’s or a tar’s members under exdir, and never outside it: every member’s path is checked before anything is written, and .., absolute paths, drive letters, colons, backslashes, NUL bytes and duplicate paths are refused with zuarchive_error_path (on Windows, device names and trailing dots or spaces too). Nothing is overwritten, and a symbolic link on the way is not followed. A member found corrupt part-way is removed, and the condition lists in written the files written before it.
Links are refused by default (links = FALSE). With links = TRUE a symbolic link is written when its target stays inside exdir, and a hard link is written as a copy of an earlier member it names; targets that are absolute or climb out are refused either way. On Windows a symbolic link the process may not create is zuarchive_error_path, naming the missing privilege.
Compound Files are not extracted (zuarchive_error_format).
Writing
archive_write() writes a ZIP (stored or DEFLATE, ZIP64 when sizes or counts need it, UTF-8 names) or, with format = "tar", a POSIX ustar with a pax header for a name over 100 bytes or not ASCII. Entries are written in the order given. compression = NULL, the default, is each format’s own: DEFLATE for ZIP, none for tar.
With the default mtime = "fixed" the same inputs give the same bytes on every platform, locale and time zone; no host-dependent field is written. A failed write leaves path as it was.
Fixes from the critical review
archive_write() refuses a device, FIFO or socket in files with zuarchive_error_path before opening anything (it could block or write bytes it never measured), and a file that grows while it is written is a read error (#50).
archive_write(mtime = "file") dates each member as its file is modified; a time the format cannot hold is zuarchive_error_format naming the file (#55).
Member names reach the C core as UTF-8 in any locale, so a name read in the C or a Latin-1 locale finds its member, and archive_extract() returns paths file.exists() finds there (#49, #51).
A symbolic link to its own directory is written as "." (#52), and a status the R layer has no words for is still a zuarchive_error (#52).
Compound File streams that share a sector are zuarchive_error_malformed at open, so a small file can no longer hand over its bytes once per stream that points at them (#53).
On Windows a symbolic link or junction among extraction’s directories is refused with zuarchive_error_path, as on other systems (#47).
Three refusals named the wrong thing: a raw vector’s archive was quoted as “the archive” or “in memory”, and a refusal with no member said is named . (#58).
C interface: a sink that calls back into the archive it is fed from gets the new ZUAR_E_BUSY (appended to zuar_status) instead of freeing the reader under the stream; its close waits for the stream to end (#48). Path archives read at an offset with pread(), so a forked child no longer moves its parent’s file offset (#56).
C interface
inst/include/zuarchive.h lets other packages open a ZIP, tar or Compound File from memory or a path, list it, find a member by name, read it whole or stream it in chunks, and read a Compound File stream by its path below the root (zuar_archive_cfb_stream()), with the same limits, checks and statuses as the R readers.
A member can also be read by pulling: zuar_archive_member_open(), then zuar_member_read() for as many bytes as the caller wants at a time, then zuar_member_close(). A ZIP member’s CRC-32 is compared after its last byte has been handed over. Several members may be open at once, and closing the archive first leaves them safe to close.
Link the installed lib${R_ARCH}/libzuarchive.a and then zukomp’s libzukomp.a with LinkingTo: zuarchive and no Imports, or reach the same functions through zuarchive_api() (zuarchive-r.h) with Imports: zuarchive.
Paths are UTF-8 for the C interface on every system; zuar_archive_open_path() refuses a path that is not valid UTF-8 with ZUAR_E_ARG.
Installation
miniz, the ZIP writer and the gzip, zstd and lz4 codecs are linked statically from zukomp’s libzukomp.a (LinkingTo: zukomp); zukomp is not loaded at run time. configure checks that the installed zukomp’s archive carries the ZIP writer and zukomp_static_api(), and says how to get one that does when it does not.
On Windows zuarchive opens, creates and extracts through the wide-character functions, so a path outside the system code page works under R 4.1 too.