529 lines
27 KiB
Markdown
529 lines
27 KiB
Markdown
# 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. The same value holds the
|
|
[game archives](#game-archives) (PAK, WAD, GRP, VPK, MPQ), the [Unix, package and disc archives](#unix-package-and-disc-archives)
|
|
(cpio, ar / .deb, rpm, .Z, .lz4, ISO 9660), [LHA, ARJ and Zoo](#lha-arj-and-zoo), the [text and Mac
|
|
encodings](#encodings) (uuencode, yEnc, BinHex, MacBinary, AppleSingle), [disk images](#disk-images) (D64, ADF, FAT)
|
|
and [Microsoft Cabinet](#microsoft-cabinet) files: [Formats](#formats) lists them all.
|
|
|
|
```luau
|
|
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"`, `"cpio"`, `"ar"`, `"rpm"`, `"z"`, `"lz4"`, `"iso"` |
|
|
| `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, cpio, ar, rpm, iso (`mode` is the Unix permission bits, e.g. `420` = `0644`) |
|
|
|
|
## Load
|
|
|
|
```luau
|
|
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"`:
|
|
|
|
```luau
|
|
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):
|
|
|
|
```luau
|
|
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, z, lz4, deb | `false`: do not open an archive inside a single-stream file or a `.deb` (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, lz4, cpio | check each entry's CRC-32 on read (zip), the XXH32 checksums (lz4), the crc sums (cpio) (default `true`) |
|
|
| `maxEntries` | tar, cpio, ar, rpm, iso | refuse more entries (default 1e6) |
|
|
| `prefer` | iso | the name set: `"rockridge"`, `"joliet"` or `"iso9660"` (default: the richest present) |
|
|
| `maxBytes` | bz2, br, rpm, z, lz4 | refuse a bigger decompressed size |
|
|
|
|
A path works on hosts with a file system:
|
|
|
|
<!-- doc:files -->
|
|
```luau
|
|
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:
|
|
|
|
```luau
|
|
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()`.
|
|
|
|
```luau
|
|
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 rosetta" }))
|
|
|
|
local back = assert(F.archive.load(bytes))
|
|
assert(#back:list() == 3 and back.meta.comment == "made by rosetta")
|
|
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`) |
|
|
|
|
```luau
|
|
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")
|
|
```
|
|
|
|
## Game archives
|
|
|
|
The game archive formats load as the same `Archive`: Quake / Half-Life PAK, Doom WAD, Build GRP, Valve VPK and
|
|
Blizzard MPQ (read-only). PAK, WAD, GRP and VPK are writable (and any Archive converts to them).
|
|
|
|
```luau
|
|
local pak = assert(F.archive.load(pakBytes)) -- Quake PAK: 56-byte names, stored files
|
|
assert(pak.format == "pak" and pak:read("default.cfg"))
|
|
local grp = assert(F.archive.load(grpBytes)) -- Duke Nukem 3D GRP
|
|
assert(grp:list()[1].name == "GAME.CON")
|
|
|
|
local new = F.format("pak").new()
|
|
new:add("maps/e1m1.bsp", "...")
|
|
local bytes = assert(F.save(new)) -- a PAK; F.save(zip, nil, { format = "pak" }) converts
|
|
assert(F.archive.load(bytes):read("maps/e1m1.bsp"))
|
|
```
|
|
|
|
A WAD's lumps keep their order; lumps of a map are named `"<map>/<lump>"` (`"E1M1/THINGS"`) and repeated names get
|
|
`"~2"`, `"~3"`, ... so every entry is unique (writing strips both again). The format module turns lumps into game
|
|
data: graphics (flats, patches, sprites, menu pictures) into Images through the `PLAYPAL` palette, wall textures
|
|
composed from their patches, and maps (binary lumps or UDMF `TEXTMAP`) into a Scene (z up, map units: floors and ceilings triangulated from the
|
|
sector outlines, walls, things as nodes standing on their floor).
|
|
|
|
```luau
|
|
local Wad = F.format("wad")
|
|
local wad = assert(F.archive.load(wadBytes))
|
|
for _, l in Wad.lumps(wad) do
|
|
print(l.name, l.size, l.namespace, l.map) -- "FLOOR" 4096 "flats" nil, "THINGS" 20 nil "E1M1", ...
|
|
end
|
|
local flat = assert(Wad.image(wad, "FLOOR")) -- 64 x 64 rgba8
|
|
local patch = assert(Wad.image(wad, "TALL")) -- a picture: meta.offset = { left, top }, alpha 0 = hole
|
|
assert(flat.width == 64 and patch.height == 300)
|
|
|
|
assert(Wad.maps(wad)[1] == "E1M1")
|
|
local map = assert(Wad.map(wad, "E1M1")) -- a Scene; Wad.mapData(wad, "E1M1") gives the raw tables
|
|
assert(map.meta.up == "z" and #map.meshes[1].primitives > 0)
|
|
local thing = map.nodes[2] -- things are child nodes of the map node
|
|
assert(thing.extras.thing.type == 1) -- player 1 start
|
|
local data = assert(Wad.mapData(wad, "E1M1"))
|
|
assert(#data.sectors == 2 and #data.things == 2)
|
|
```
|
|
|
|
Quake's and Half-Life's texture WADs (WAD2 / WAD3, format `wad2`; Doom's IWAD / PWAD is `wad`) list their lumps
|
|
by name; `image` decodes miptex textures (any mip level), qpic pictures, the console font and the palette lump. Quake 1
|
|
lumps use the WAD's own `palette` lump (gfx.wad), `palette` or `gfx/palette.lmp` through `files` / `resolve` (Quake's
|
|
palette is not redistributable, so rosetta never embeds it); WAD3 lumps carry their own. `addImage` writes images
|
|
back as lumps (WAD3: with a palette made by median cut):
|
|
|
|
```luau
|
|
local Wad2 = F.format("wad2")
|
|
local gfx = assert(F.archive.load(gfxWadBytes))
|
|
assert(gfx.format == "wad2" and gfx.meta.source.variant == "wad2")
|
|
local brick = assert(Wad2.image(gfx, "brick")) -- through the WAD's palette lump
|
|
local small = assert(Wad2.image(gfx, "brick", { mip = 2 })) -- 8 x 8
|
|
assert(brick.width == 32 and small.width == 8)
|
|
local disc = assert(Wad2.image(gfx, "disc")) -- a qpic: index 255 transparent
|
|
assert(disc.meta.source.kind == "qpic")
|
|
|
|
local hl = Wad2.new("wad3")
|
|
assert(Wad2.addImage(hl, "{FENCE", brick)) -- '{' textures key index 255
|
|
local wad3 = assert(F.save(hl))
|
|
assert(F.archive.load(wad3).meta.source.variant == "wad3")
|
|
```
|
|
|
|
VPK directories (`pak01_dir.vpk`) hold small files themselves and point to numbered part files for the rest; pass
|
|
those through `files` or `resolve` (loading a path does it for you):
|
|
|
|
```luau
|
|
local vpk = assert(F.archive.load(vpkBytes))
|
|
assert(vpk:read("scripts/game.txt")) -- CRC-32 checked on read
|
|
-- multi-part: F.archive.load(dir, { name = "pak01_dir.vpk", files = { ["pak01_000.vpk"] = part0 } })
|
|
local out = assert(F.save(vpk)) -- one self-contained version 2 file (with its MD5s)
|
|
assert(F.archive.load(out):read("materials/brick/wall.vmt"))
|
|
```
|
|
|
|
MPQ archives are read through their hash table, so names come from the archive's `(listfile)`; files it does not
|
|
list appear as `File00000012.xxx` (their block index), and `names` adds names to look up:
|
|
|
|
```luau
|
|
local mpq = assert(F.archive.load(mpqBytes)) -- also .w3x / .w3m / .SC2Map
|
|
local script = assert(mpq:read("war3map.j")) -- zlib / PKWARE implode / bzip2 / LZMA / sparse sectors
|
|
assert(buffer.len(script) == buffer.len(assert(mpq:read("readme.txt"))))
|
|
local named = assert(F.archive.load(mpqBytes, { names = { "war3map.w3e" } }))
|
|
assert(#named:list() >= 2)
|
|
```
|
|
|
|
Wolfenstein 3D's page file (`VSWAP.WL6`, also `.WL1`, Spear of Destiny's `.SOD` / `.SDM`) loads as an Archive of
|
|
its pages (`walls/000`, `sprites/000`, `sounds/000`, `soundinfo`). The format module turns walls and sprites into
|
|
64 x 64 Images and the digitized sounds into Audio. The game's palette is not part of the file (nor redistributable):
|
|
pass its 768 bytes as `palette` (6-bit VGA values are scaled up); without it the images are grey.
|
|
|
|
```luau
|
|
local Vswap = F.format("vswap")
|
|
local pages = assert(F.archive.load(vswapBytes, { name = "VSWAP.WL6" }))
|
|
assert(pages:list()[1].name == "walls/000")
|
|
local wall = assert(Vswap.image(pages, "wall", 0, { palette = wolfPaletteBytes }))
|
|
local guard = assert(Vswap.image(pages, "sprite", 0, { palette = wolfPaletteBytes })) -- alpha 0 = hole
|
|
assert(wall.width == 64 and guard.height == 64)
|
|
local sound = assert(Vswap.sound(pages, 1)) -- u8 PCM at 7042 Hz, as f32 Audio
|
|
assert(sound.sampleRate == 7042 and Vswap.sounds(pages) == 2)
|
|
```
|
|
|
|
## Unix, package and disc archives
|
|
|
|
### cpio
|
|
|
|
GNU cpio's and bsdtar's newc, crc, odc and old binary archives read; newc, crc and odc write (`variant`, default the
|
|
source's). Entries carry the Unix fields: `type` (tar letters: `"0"` file, `"5"` directory, `"2"` symlink, ...),
|
|
`mode`, `uid`, `gid`, `nlink`, `ino`, device numbers, `link`, and `hardlink` (every link of a hard-linked file reads
|
|
its data).
|
|
|
|
```luau
|
|
local cpio = assert(F.archive.load(cpioBytes))
|
|
local by = {}
|
|
for _, e in cpio:list() do
|
|
by[e.name] = e
|
|
end
|
|
assert(by["bin"].dir and by["bin/run.sh"].mode == 493) -- 493 = 0755
|
|
assert(by["run"].type == "2" and by["run"].link == "bin/run.sh") -- a symlink
|
|
local odc = assert(cpio:write({ variant = "odc" }))
|
|
assert(F.archive.load(odc).meta.source.variant == "odc")
|
|
```
|
|
|
|
### ar and .deb
|
|
|
|
Unix `ar` archives (GNU / System V long names and symbol tables, BSD `#1/` names) read and write (`variant = "gnu"`
|
|
or `"bsd"`); symbols land in `meta.symbols`. A `.deb` loads as its `data.tar.*` (any compression), with the ar
|
|
archive in `meta.outer` and the control file's fields in `meta.outer.meta.control`:
|
|
|
|
```luau
|
|
local deb = assert(F.archive.load(debBytes))
|
|
assert(deb.format == "tar" and deb.meta.container == "ar")
|
|
assert(deb:read("./usr/bin/hello"))
|
|
local control = deb.meta.outer.meta.control
|
|
assert(control.Package == "hello" and control.Version == "1.0-1")
|
|
local ar = assert(F.archive.load(debBytes, { unwrap = false })) -- the ar itself
|
|
assert(ar:list()[1].name == "debian-binary" and ar.meta.payload == "data.tar.xz")
|
|
```
|
|
|
|
### rpm
|
|
|
|
RPM packages list from their header (no decompression); the payload (gzip, bzip2, xz, lzma or zstd) is decompressed
|
|
on the first `read`. The header fields are in `meta.rpm` (`name`, `version`, `release`, `arch`, `summary`, ... and
|
|
every tag by number in `meta.rpm.tags`). Read only.
|
|
|
|
```luau
|
|
local rpm = assert(F.archive.load(rpmBytes))
|
|
assert(rpm.meta.rpm.name == "hello" and rpm.meta.rpm.version == "1.0" and rpm.meta.rpm.arch == "noarch")
|
|
assert(rpm:list()[1].name == "usr/bin/hello" and rpm:list()[1].mode == 493)
|
|
local hello = buffer.tostring(assert(rpm:read("usr/bin/hello")))
|
|
assert(string.find(hello, "echo hello", 1, true))
|
|
```
|
|
|
|
### .Z and .lz4
|
|
|
|
Unix `compress` (`.Z`, `.taz`) and LZ4 frame (`.lz4`) files are single-stream archives like gzip: a `.tar.Z` or
|
|
`.tar.lz4` loads as the tar. Both write: `.Z` byte for byte as ncompress (`maxBits` 9..16), `.lz4` as one frame of
|
|
independent blocks (`blockSize`, `blockChecksum`, `contentChecksum`, `contentSize`).
|
|
|
|
```luau
|
|
local tar = assert(F.archive.load(tarZBytes))
|
|
assert(tar.format == "tar" and tar.meta.compression == "z")
|
|
local lz = assert(F.archive.load(lz4Bytes, { name = "notes.txt.lz4" }))
|
|
assert(lz.meta.stream and lz:list()[1].name == "notes.txt")
|
|
local z = assert(F.save(tar, nil, { format = "z" })) -- a tar inside
|
|
assert(F.archive.load(z).format == "tar")
|
|
local l4 = assert(F.save("some text", nil, { format = "lz4", blockChecksum = true }))
|
|
assert(buffer.tostring(F.archive.load(l4):read("data")) == "some text")
|
|
```
|
|
|
|
### ISO images
|
|
|
|
ISO 9660 images read with Rock Ridge names, modes and symlinks when present, else Joliet's Unicode names, else the
|
|
plain ISO names (`prefer = "joliet"` / `"iso9660"` picks another set). `meta.iso` has the volume facts and
|
|
`meta.boot` an El Torito boot catalog. Read only.
|
|
|
|
```luau
|
|
local iso = assert(F.archive.load(isoBytes))
|
|
assert(iso.meta.iso.volumeId == "DOCS" and iso.meta.iso.rockRidge and iso.meta.iso.joliet)
|
|
assert(iso.meta.boot and iso.meta.boot.bootable)
|
|
local by = {}
|
|
for _, e in iso:list() do
|
|
by[e.name] = e
|
|
end
|
|
assert(by["bin"].dir and by["bin/run.sh"].mode == 493 and by["run"].link == "bin/run.sh")
|
|
assert(F.archive.load(isoBytes, { prefer = "joliet" }).meta.iso.tree == "joliet")
|
|
```
|
|
|
|
## LHA, ARJ and Zoo
|
|
|
|
DOS-era archives read with their own compressors (`codecs/compress/Lzh`): LHA / LZH at header levels 0 to 3 with
|
|
lh0, lh1, lh4 to lh7, lhx and LArc's lzs / lz5 (Unix modes, symlinks, CRC-16 checked on `read`), ARJ methods 0 to 4
|
|
(CRC-32 checked) and Zoo's stored, LZW and lzh entries (deleted entries skipped). LHA also writes (level 2 headers,
|
|
stored data); ARJ and Zoo are read only.
|
|
|
|
```luau
|
|
local lzh = assert(F.archive.load(lzhBytes))
|
|
assert(lzh.format == "lha" and lzh:list()[1].method == "lh0") -- small files are stored
|
|
assert(buffer.tostring(assert(lzh:read("readme.txt"))) == "rosetta retro archives\n")
|
|
local arj = assert(F.archive.load(arjBytes))
|
|
assert(arj:read("src/main.c"))
|
|
local zoo = assert(F.archive.load(zooBytes))
|
|
assert(zoo:list()[1].method == "lzw" and zoo:read("readme.txt"))
|
|
local stored = assert(F.save(lzh, nil, { format = "lha" })) -- level 2, lh0
|
|
assert(F.archive.load(stored):list()[1].method == "lh0")
|
|
```
|
|
|
|
## Encodings
|
|
|
|
Text and Mac encodings load as archives too: uuencode / xxencode / `begin-base64` blocks (`uu`), yEnc (multi-part
|
|
postings in one text are joined), BinHex 4.0, MacBinary I / II / III and AppleSingle / AppleDouble. All of them read
|
|
and write. Mac files keep the data fork as the entry `name` and a resource fork as `name .. ".rsrc"`
|
|
(`fork = "resource"`), with `macType`, `macCreator` and `finderFlags`. A single-file encoding is a stream, so a
|
|
uuencoded `.tar.gz` loads as the tar.
|
|
|
|
```luau
|
|
local uu = assert(F.archive.load(uuText))
|
|
assert(uu:list()[1].name == "readme.txt" and uu:list()[1].mode == 420) -- 420 = 0644
|
|
local a = F.format("binhex").new()
|
|
a:add("Read Me", "the data fork")
|
|
a:add("Read Me.rsrc", "resources")
|
|
a._byName["Read Me.rsrc"].fork = "resource"
|
|
local hqx = assert(a:write())
|
|
local back = assert(F.archive.load(hqx))
|
|
assert(back:list()[2].fork == "resource" and buffer.tostring(back:read("Read Me")) == "the data fork")
|
|
local y = assert(F.save(uu, nil, { format = "yenc" }))
|
|
assert(string.sub(buffer.tostring(y), 1, 8) == "=ybegin ")
|
|
```
|
|
|
|
## Disk images
|
|
|
|
Floppy and disk images list their files: Commodore D64 / D71 / D81 (PETSCII names as VICE's c1541 shows them,
|
|
`type` = `prg` / `seq` / `usr` / `rel`, disk name and id in `meta`), Amiga ADF (OFS / FFS, international and
|
|
directory-cache modes, protection bits, comments, links) and FAT12 / FAT16 / FAT32 images (VFAT long names; a
|
|
hard-disk image opens its first FAT partition, `partition` picks another). D64 and FAT floppies also write.
|
|
|
|
```luau
|
|
local d64 = assert(F.archive.load(d64Bytes))
|
|
assert(d64.meta.disk == "games" and d64:list()[1].type == "prg")
|
|
local adf = assert(F.archive.load(adfBytes))
|
|
assert(adf.meta.volume == "Work" and adf.meta.fs == "ffs" and adf:read("s/readme"))
|
|
local fat = assert(F.archive.load(floppyBytes))
|
|
assert(fat.meta.fat == 12 and fat.meta.label == "DOCS" and fat:read("src/main.c"))
|
|
local floppy = assert(F.save(adf, nil, { format = "fat", size = 720 })) -- copy the ADF's files to a FAT floppy
|
|
assert(F.archive.load(floppy):read("s/readme"))
|
|
```
|
|
|
|
## Microsoft Cabinet
|
|
|
|
Cabinet files (`.cab`) read with stored, MSZIP, LZX and Quantum folders; each data block's checksum is checked when
|
|
a file of its folder is read. Entries carry the MS-DOS `attributes` (`readonly`, `executable`), `utf8` (the name flag)
|
|
and `folder`; DOS times are local times without a zone, read as UTC. Files continued from or into another cabinet of
|
|
a set read as `UNSUPPORTED`. Writing makes one folder, MSZIP (default, `level`) or stored (`compression = "none"`):
|
|
|
|
```luau
|
|
local cab = assert(F.archive.load(cabBytes))
|
|
assert(cab.format == "cab" and cab.meta.folders[1].method == "mszip")
|
|
local by = {}
|
|
for _, e in cab:list() do
|
|
by[e.name] = e
|
|
end
|
|
assert(by["bin/tool.dll"].size == 16384 and by["readme.txt"].mtime == 1767225600)
|
|
local out = assert(F.save(cab, nil, { format = "cab", compression = "none" }))
|
|
assert(F.archive.load(out):list()[1].method == "store")
|
|
```
|
|
|
|
## Unity packages
|
|
|
|
A `.unitypackage` is a gzip'd tar of one folder per asset guid (`asset`, `asset.meta`, `pathname`). It loads as an
|
|
Archive keyed by the asset paths, each `.meta` next to its asset as in a Unity project folder; folders are `dir`
|
|
entries with their own `.meta`. Every entry carries its `guid`, and an asset's `preview` (when the package has a
|
|
`preview.png`) is a function returning its bytes. Without a name the file is plain gzip (it unwraps to the tar of
|
|
guid folders): pass a `.unitypackage` name or `format = "unitypackage"`. Any Archive saves as a package: guids come
|
|
from `entry.guid`, then the `.meta` entry's `guid:` line, else the path's MD5; missing `.meta` files are made.
|
|
|
|
```luau
|
|
local pack = F.format("unitypackage").new()
|
|
pack:add("Assets/Scripts", "", { dir = true })
|
|
pack:add("Assets/Scripts/Player.cs", "public class Player { }\n")
|
|
local bytes = assert(F.save(pack))
|
|
local back = assert(F.load(bytes, { name = "game.unitypackage" }))
|
|
assert(back.format == "unitypackage")
|
|
local meta = buffer.tostring(assert(back:read("Assets/Scripts/Player.cs.meta")))
|
|
assert(string.match(meta, "guid: (%x+)") == back:list()[3].guid)
|
|
```
|
|
|
|
## 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"` |
|
|
| PAK (`.pak`) | yes | yes | Quake / Quake II / Half-Life (and SiN `SPAK`) |
|
|
| WAD (`.wad`) | yes | yes | IWAD / PWAD; lumps, graphics, wall textures and maps (binary and UDMF) through `F.format("wad")` |
|
|
| VSWAP (Wolfenstein 3D `.wl6` / `.wl1` / `.sod` / `.sdm`) | yes | | pages; walls, sprites and sounds through `F.format("vswap")` |
|
|
| GRP (`.grp`) | yes | yes | Build engine |
|
|
| VPK (`.vpk`) | yes | yes | versions 1 / 2, multi-part reading, preload bytes; writes one directory file |
|
|
| MPQ (`.mpq`, `.w3x`, `.sc2map`, ...) | yes | - | versions 1-4 (classic tables), encryption; no Huffman / ADPCM (WAVE) sectors |
|
|
| cpio (`.cpio`) | yes | yes | newc, crc, odc, old binary (read); hard links, symlinks, devices |
|
|
| ar (`.a`, `.ar`, `.deb`) | yes | yes | GNU / BSD names, symbol tables; a `.deb` loads as its data tar |
|
|
| rpm (`.rpm`) | yes | - | gzip / bzip2 / xz / lzma / zstd payloads |
|
|
| compress (`.Z`, `.taz`) | yes | yes | single stream; 9..16-bit LZW |
|
|
| LZ4 (`.lz4`) | yes | yes | single stream; frames, legacy frames, XXH32 checks; no dictionaries |
|
|
| ISO 9660 (`.iso`) | yes | - | Joliet, Rock Ridge, El Torito, multi-extent; no UDF |
|
|
| LHA (`.lzh`, `.lha`, `.lzs`) | yes | yes | levels 0-3; lh0 / lh1 / lh4-lh7 / lhx / lzs / lz5; writes stored level 2 |
|
|
| ARJ (`.arj`) | yes | - | methods 0-4; no garbled entries, no multi-volume |
|
|
| Zoo (`.zoo`) | yes | - | stored / LZW / lzh |
|
|
| uuencode / xxencode (`.uu`, `.xxe`) | yes | yes | several blocks per text; GNU `begin-base64` too |
|
|
| yEnc (`.yenc`) | yes | yes | multi-part postings in one text |
|
|
| BinHex 4.0 (`.hqx`) | yes | yes | data and resource forks |
|
|
| MacBinary (`.bin`) | yes | yes | I / II / III; writes III |
|
|
| AppleSingle / AppleDouble | yes | yes | versions 1 and 2 |
|
|
| C64 disks (`.d64`, `.d71`, `.d81`) | yes | yes | PETSCII names; no GEOS VLIR |
|
|
| Amiga disks (`.adf`) | yes | - | OFS / FFS; no long-name (DOS 6 / 7) volumes |
|
|
| FAT images (`.img`, `.ima`) | yes | yes | FAT12 / 16 / 32, VFAT names, MBR partitions; writes FAT12 floppies |
|
|
| Unity package (`.unitypackage`) | yes | yes | assets by path, `.meta` files beside them, guids, previews; needs the extension to beat gzip |
|
|
| Cabinet (`.cab`) | yes | yes | stored / MSZIP / LZX / Quantum (read), stored / MSZIP (write); no spanned files |
|
|
|
|
Full capability list: [FORMATS.md](../FORMATS.md).
|
|
|
|
## Gotchas
|
|
|
|
- `read` returns a new buffer every call and decompresses again; keep the buffer if you need it twice.
|
|
- `F.save(tar)` of an unwrapped `.tar.gz` writes a plain tar: the value *is* the tar. Pass `format = "tgz"` (or a
|
|
`.tar.gz` / `.tgz` destination) to compress it again.
|
|
- Writing gzip from an archive with exactly one entry writes that file, not a tar (`format = "tgz"` included); a
|
|
`.tar.gz` needs two or more entries.
|
|
- Brotli has no magic bytes: detection needs a `.br` name or `format = "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"` with `dir = true`.
|