9.5 KiB
Archive
Every archive format loads as one Archive value: a list of named entries whose bytes you read on demand. Zip,
tar and 7z hold many entries; gzip, xz, zstd, bzip2 and Brotli files are single-stream archives with one entry
(the decompressed data). A .tar.gz (.tgz, .tar.xz, .tar.zst, .tar.bz2, .tar.br) loads straight as the tar
inside. Zip, tar and gzip are writable; any Archive can be written as any of them.
local zip = assert(F.archive.load(zipBytes))
for _, e in zip:list() do
print(e.name, e.size, e.method, e.dir) -- "readme.txt" 116 "deflate" false
end
local readme = assert(zip:read("readme.txt")) -- a buffer
assert(string.find(buffer.tostring(readme), "archive example", 1, true))
zip:add("notes/todo.txt", "write the docs") -- a string or a buffer
local out = assert(F.save(zip)) -- a buffer, in the source format (zip)
assert(F.archive.load(out):read("notes/todo.txt"))
The type
Archive:
| Field / method | Type | Meaning |
|---|---|---|
kind |
"archive" |
|
format |
string | "zip", "tar", "7z", "gzip", "xz", "zst", "bz2", "br" |
list(self) |
{ ArchiveEntry } |
the entries in archive order (directories included) |
read(self, name) |
buffer?, Err? |
a fresh copy of the entry's bytes, decompressed (lazily, on each call) |
add(self, name, data, opts?) |
add or replace an entry; data is a buffer or a string |
|
write(self, opts?) |
buffer?, Err? |
the archive in its own format (F.save does the same and can pick another format) |
meta |
table | source.format; stream = true for single-stream archives; compression (the outer format of an unwrapped .tar.gz); comment (zip, gzip) |
ArchiveEntry (each list() call returns new tables; editing them changes nothing):
| Field | Type | Meaning |
|---|---|---|
name |
string | the path inside the archive, / separated, no leading /; directories end in / in zip |
size |
number | uncompressed bytes (0 for directories) |
compressedSize |
number? | stored bytes (zip, gzip) |
method |
string? | "store", "deflate", "lzma2", ... |
mtime |
number? | modification time, Unix seconds (UTC) |
dir |
boolean | a directory entry |
crc32, comment |
number?, string? | zip only |
mode, uid, gid, uname, gname, type, link |
tar only (mode is the Unix permission bits, e.g. 420 = 0644) |
Load
local tar = assert(F.archive.load(tgzBytes)) -- a .tar.gz: the tar inside
assert(tar.format == "tar" and tar.meta.compression == "gzip")
for _, e in tar:list() do
assert(e.mtime == 1767225600) -- 2026-01-01T00:00:00Z, Unix seconds
end
local seven = assert(F.archive.load(sevenzBytes)) -- 7z: LZMA / LZMA2 / Deflate / BZip2 / BCJ / delta
local csv = assert(seven:read("data/scores.csv"))
assert(buffer.tostring(csv):sub(1, 4) == "name")
Single-stream files hold one entry, named after the file without its compression extension when the loader knows
the name (a path, or opts.name), else after the name stored in the file (gzip FNAME), else "data":
local xz = assert(F.archive.load(xzBytes, { name = "notes.txt.xz" }))
assert(xz.meta.stream and #xz:list() == 1)
assert(xz:list()[1].name == "notes.txt")
local text = buffer.tostring(assert(xz:read("notes.txt")))
local zst = assert(F.archive.load(zstBytes)) -- no name given: the entry is "data"
assert(buffer.tostring(assert(zst:read("data"))) == text)
Pass unwrap = false to keep the outer single-stream archive of a .tar.gz (its one entry is the raw tar):
local gz = assert(F.archive.load(tgzBytes, { unwrap = false }))
assert(gz.format == "gzip" and gz.meta.stream)
local inner = assert(gz:read(gz:list()[1].name))
assert(F.detect(inner) == "tar")
Options for loading:
| Option | Formats | Meaning |
|---|---|---|
unwrap |
gzip, xz, zst, bz2, br | false: do not open an archive inside a single-stream file (also off when format is given) |
name |
single-stream | the file name, used to name the entry (set for you when loading a path) |
verify |
zip | check each entry's CRC-32 on read (default true) |
maxEntries |
tar | refuse more entries (default 1e6) |
maxBytes |
bz2, br | refuse a bigger decompressed size |
A path works on hosts with a file system:
local a = assert(F.load("assets/files.zip"))
assert(F.save(a, "out/files.tar")) -- the format from the extension
assert(F.archive.load("out/files.tar"):read("readme.txt"))
Inspect
read matches the exact name first; zip also finds entries case-insensitively and by base name. A missing entry is
nil, Err (code ARG), never an error:
local zip = assert(F.archive.load(zipBytes))
local data, err = zip:read("missing.txt")
assert(data == nil and err.code == F.Err.ARG)
assert(zip:read("README.TXT")) -- zip: case-insensitive lookup
local files, bytes = 0, 0
for _, e in zip:list() do
if not e.dir then
files += 1
bytes += e.size
end
end
assert(files == 3)
Edit
add appends an entry or replaces the one with the same name. Its options depend on the format the archive came
from:
| Option | zip | tar |
|---|---|---|
method |
"deflate" (default; stored when deflate does not shrink it) or "store" |
|
level |
deflate level 0..9 (default 6) | |
mtime |
Unix seconds (default 1980-01-01, for reproducible output) | Unix seconds (default 0) |
mode |
permission bits (default 0644) |
|
dir |
name the entry "dir/" with data = nil |
{ dir = true } |
comment, utf8, attributes |
per-entry comment, UTF-8 name flag, external attributes |
Zip archives also have remove(name) (true when the entry existed). Unchanged zip entries are copied as they are,
without recompressing.
New archives come from the format modules: F.format("zip").create() and F.format("tar").new().
local zip = F.format("zip").create()
zip:add("assets/", nil) -- a directory
zip:add("assets/hello.txt", "hello", { level = 9, mtime = 1767225600 })
zip:add("assets/raw.bin", buffer.create(1024), { method = "store" })
local bytes = assert(F.save(zip, nil, { format = "zip", comment = "made by formats-luau" }))
local back = assert(F.archive.load(bytes))
assert(#back:list() == 3 and back.meta.comment == "made by formats-luau")
assert(back:list()[3].method == "store")
assert(back:remove("assets/raw.bin") and #back:list() == 2)
local tar = F.format("tar").new()
tar:add("bin/", "", { dir = true })
tar:add("bin/run.sh", "#!/bin/sh\necho hi\n", { mode = 493, mtime = 1767225600 }) -- 493 = 0755
local tarData = assert(F.save(tar)) -- the tar's own format
assert(F.archive.load(tarData):list()[2].mode == 493)
Save and convert
F.save(archive, dest?, opts?) writes the archive's own format unless opts.format (or the destination's
extension) names another one; any Archive (a 7z, a tar, a zip) can be written as zip, tar or gzip.
| Writing as | Options | What it holds |
|---|---|---|
| zip | comment (archive comment), zip64 (force ZIP64 records) |
every entry |
| tar | format: "pax" (default: ustar plus pax records when needed), "gnu", "ustar" |
every entry, with mode / mtime |
gzip (gz, tgz) |
level 0..9, name (FNAME), mtime |
one entry: that file; several entries: a tar inside (a .tar.gz) |
local seven = assert(F.archive.load(sevenzBytes))
local zipData = assert(F.save(seven, nil, { format = "zip" })) -- 7z -> zip
local tgz = assert(F.save(seven, nil, { format = "tgz" })) -- 7z -> .tar.gz
local back = assert(F.archive.load(tgz))
assert(back.format == "tar" and back.meta.compression == "gzip")
assert(F.detect(zipData) == "zip")
local converted = assert(F.convert(tgzBytes, "zip")) -- load + save in one call
assert(F.archive.load(converted):read("src/main.luau"))
local gzipped = assert(F.save("some text", nil, { format = "gzip", name = "note.txt" })) -- a buffer or a string
assert(F.archive.load(gzipped, { unwrap = false }):list()[1].name == "note.txt")
Formats
| Format | Read | Write | Notes |
|---|---|---|---|
zip (.zip) |
yes | yes | store / deflate entries, ZIP64, data descriptors, comments; no encryption |
tar (.tar) |
yes | yes | ustar, pax, GNU long names |
7z (.7z) |
yes | - | copy / LZMA / LZMA2 / Deflate / BZip2 / BCJ / delta; no AES, PPMd, BCJ2 |
gzip (.gz, .tgz) |
yes | yes | single stream; concatenated members read as one |
xz / lzma (.xz, .txz, .lzma) |
yes | - | single stream |
zstd (.zst, .tzst) |
yes | - | single stream; no dictionaries |
bzip2 (.bz2, .tbz2) |
yes | - | single stream |
Brotli (.br) |
yes | - | single stream; no magic number, so pass a .br name or format = "br" |
Full capability list: FORMATS.md.
Gotchas
readreturns a new buffer every call and decompresses again; keep the buffer if you need it twice.F.save(tar)of an unwrapped.tar.gzwrites a plain tar: the value is the tar. Passformat = "tgz"(or a.tar.gz/.tgzdestination) to compress it again.- Writing gzip from an archive with exactly one entry writes that file, not a tar (
format = "tgz"included); a.tar.gzneeds two or more entries. - Brotli has no magic bytes: detection needs a
.brname orformat = "br". - Entry names are stored as given. Before extracting to disk, reject names containing
..or starting with/. - Directory entries: zip lists
"src/"(trailing slash), 7z and tar list"src"withdir = true.