Skip to content
Jennifer Programming Language

archive - tar / zip containers

Enable with use archive;. Bundles files into a tar or zip container and reads them back, entirely over bytes (no fs dependency), value-semantic. Shares the pack / unpack verbs with compress (byte streams there, file bundles here); the container format is a string argument. Backed by Go's archive/tar + archive/zip; works on both binaries.

An archive.Entry holds a regular file, so unpack refuses a symbolic link, device or FIFO member by default - and a git archive tarball of a repository holding one symlink is the common way to meet that. Pass archive.UnpackOptions{skipSpecial: true} to archive.unpackWith to skip such members and keep the rest:

jennifer
def files as list of archive.Entry init archive.unpackWith("tar.gz", $tgz,
    archive.UnpackOptions{skipSpecial: true});

Surface

CallReturnsNotes
archive.pack(format, entries)bytesBundle a list of archive.Entry.
archive.unpack(format, b)list of archive.EntryRead a bundle back (default caps).
archive.unpackWith(format, b, opts)list of archive.EntryRead a bundle with caller-set caps (archive.UnpackOptions).

format is "tar", "zip", or the gzip combo "tar.gz". Errors are listed below.

unpack bounds untrusted input: the total decompressed payload of one call (summed across every entry) is capped at 256 MiB (32 MiB on jennifer-tiny) and the member count at 65536; past either cap it raises a catchable Error{kind: "limit"} instead of expanding a small "zip bomb" into gigabytes of memory. It also rejects any member whose name is an absolute path, starts with a drive letter (C:/x, and the drive-relative C:foo, on every platform), or contains a .. segment (a "zip-slip" name), so a naive extraction loop (fs.writeBytes($dir + "/" + $e.name, $e.data)) can't be tricked into writing outside the target directory. Peak memory is a small multiple of the cap; lower it with unpackWith on constrained hosts. A .. segment is refused even when the path would stay inside after cleaning (a/../b): unpack returns each name exactly as stored, so a caller that keeps only part of it (stripping a wrapper directory, or taking what follows src/) is never handed a ... pack refuses the same names, so it never writes an archive unpack would reject.

Directories are implied by member paths and skipped. Archive-wide metadata is consumed too, never returned as a member: a pax global header (the one git archive and forge-generated source tarballs write first, to record the commit id) and a GNU volume label. A tar contiguous file and a GNU sparse file come back as ordinary entries. A tar hard link comes back as a regular Entry carrying the content of the member it names (which must appear earlier in the archive); its bytes count against the size caps like a copy. A symbolic link, device, FIFO or socket has no content an Entry can carry, so unpack refuses an archive holding one with an "archive" error naming the member, rather than dropping it unnoticed. To unpack such an archive anyway, pass skipSpecial: true to unpackWith, which leaves those members out (and a hard link to one of them too). Tar and zip behave the same (a zip symlink is not returned as a file holding its target path).

archive.UnpackOptions

archive.unpackWith(format, b, opts) is unpack with caller-set caps, for an input whose trusted size differs from the defaults:

jennifer
def opts as archive.UnpackOptions init archive.UnpackOptions{
    maxTotalBytes: 50 * 1024 * 1024,   # 50 MiB total across all entries
    maxEntryBytes: 10 * 1024 * 1024,   # 10 MiB per entry
    maxEntries: 1000,
    skipSpecial: false                 # true: leave symlinks / devices out
};
def entries as list of archive.Entry init archive.unpackWith("zip", $kmz, $opts);
Cap field valueCap
0the default (so all three at 0, with skipSpecial: false, is unpack)
1 and upthat value
math.MAX_INTnone (for a fully trusted archive)
negativea "runtime" error

Exceeding any cap raises Error{kind: "limit"}, the same as unpack. Like any library struct literal, UnpackOptions{...} may leave fields out; a left-out field takes its zero value (so 0, the default cap).

Errors

KindRaised for
"archive"a bundle unpack / unpackWith cannot read: not the format, truncated (a tar without its end-of-archive marker included), corrupt compressed data or a checksum mismatch (a tar.gz's gzip trailer included), data after the end (anything but NUL padding after a tar's end-of-archive marker, bytes after a tar.gz's gzip member that are not another one), an absolute, drive-letter or .. member name, a symbolic link / device / FIFO / socket member (without skipSpecial), a hard link to no earlier regular file
"limit"a reached cap: the total size, an entry's size, the member count
"runtime"misuse: a wrong argument, an unknown format, a negative UnpackOptions cap, an entry pack refuses (an unsafe name, a mode outside 0 to 0o7777)

Deprecated

UseInsteadRemoved
the format "tgz" (pack, unpack, unpackWith)"tar.gz"0.29.0

"tgz" still works. Lint reports it as L301 where the format is a string literal.

archive.Entry

A struct { name as string, data as bytes, mode as int, mtime as int }:

FieldMeaning
nameThe path within the archive (subdirectories with /).
dataThe file contents.
modeUnix permission bits (e.g. 0o644); 0 means the default 0o644. On unpack, the special bits (setuid / setgid / sticky) and tar file-type bits are masked off, so an untrusted archive never yields a setuid mode an extraction loop would apply. pack refuses a mode outside 0 to 0o7777: a higher bit is a file type (a zip member with the symlink bit is written as a symbolic link).
mtimeModification time, unix seconds. zip stores 0 to 4,294,967,295; pack refuses another value.

Only regular files (and tar hard links, resolved to their content) map to an Entry; see Links and special members.

Example

jennifer
use encoding;
use io;
use archive;

def readme as archive.Entry init archive.Entry{
    name: "README", data: encoding.encode("utf-8", "hello"), mode: 0o644, mtime: 0
};
def blob as bytes init archive.pack("tar.gz", [$readme]);   # one call: tar, then gzip
def back as list of archive.Entry init archive.unpack("tar.gz", $blob);
io.printf("%s = %s\n", $back[0].name, encoding.decode("utf-8", $back[0].data));

"tar.gz" layers compress's gzip over a tar internally, so a .tar.gz is one call.

See also

  • compress.md - pack / unpack for byte streams ("gzip" / "zlib" / "deflate").
  • fs.md - write the packed bytes to disk, or read an archive off disk to unpack.