absorb: containers/Zip (reader from zip-luau 121d4b0 with a direct codecs/Deflate require, Err values; new writer: store/deflate, UTF-8, DOS times, raw copies, ZIP64) + archive/zip (Types.Archive list/read/add/remove/write, create, encode); tests vs Python zipfile

This commit is contained in:
4DBug committed 2026-10-31 08:51:05 -05:00
1 parent b6e2b9e68d
commit 17778cff1b
7 files changed
+1349

No files matched your search

+238
View File
@@ -0,0 +1,238 @@
--!strict
-- archive/zip: ZIP archives as the standard Archive (Types.Archive: list / read / add / write) on containers/Zip.
-- Absorbed from zip-luau 121d4b0 (same author, no third-party code; the reader, now containers/Zip) plus a new
-- writer. Supports: reading stored and deflated entries (CRC-checked), ZIP64, data descriptors, archive comments,
-- prefixed archives (self-extractors), UTF-8 names, case-insensitive / basename lookup; writing stored and deflated
-- entries (codecs/Deflate), unchanged entries copied without recompressing, ZIP64 when needed.
-- Not supported: encryption (traditional PKWARE or AES), compression methods other than store / deflate (bzip2,
-- LZMA, zstd entries are listed but not readable), multi-disk archives.
--
-- local a = Zip.decode(Bytes.reader(buf)) -- or F.archive.load(buf)
-- for _, e in a:list() do print(e.name, e.size, e.method) end
-- local data = a:read("dir/file.txt")
-- a:add("new.txt", "hello", { level = 9 }) -- or { method = "store", mtime = os.time() }
-- local out = a:write({ comment = "made here" }) -- or Zip.encode(a) / F.save(a, nil, { format = "zip" })
-- local b = Zip.create() -- an empty archive
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Container = require("@formats/containers/Zip")
-- ------------------------------------------------------------------------------------------------ contract
local Zip = {
name = "zip",
category = "archive",
extensions = { "zip", "wsz" },
mime = { "application/zip", "application/x-zip-compressed" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Zip" },
caps = { decode = true, encode = true, methods = { "store", "deflate" }, zip64 = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
verify: boolean?, -- check every entry's CRC-32 on read (default true)
}
export type AddOptions = Container.AddOptions
export type WriteOptions = Container.FinishOptions
-- ------------------------------------------------------------------------------------------------ constants
local METHODS = { [0] = "store", [8] = "deflate", [9] = "deflate64", [12] = "bzip2", [14] = "lzma", [93] = "zstd", [95] = "xz", [99] = "aes" }
local Archive = {}
Archive.__index = Archive
local function new(zip: any, verify: boolean): any
local a = setmetatable({
kind = "archive",
format = "zip",
meta = { source = { format = "zip" }, comment = if zip then zip.comment else nil, zip64 = if zip then zip.zip64 else nil },
_zip = zip,
_verify = verify,
_order = {} :: { string },
_items = {} :: { [string]: any },
}, Archive)
if zip then
for _, e in zip:list() do
if a._items[e.name] == nil then
table.insert(a._order, e.name)
end
a._items[e.name] = { raw = e }
end
end
return a
end
-- ------------------------------------------------------------------------------------------------ sniff
function Zip.sniff(r: Bytes.Reader): number
local s = r:peek(4)
-- ZIP-based formats (docx, jar, 3mf, ...) score higher in their own sniff
if s == "PK\3\4" or s == "PK\5\6" then
return 0.6
end
if string.sub(s, 1, 2) == "MZ" then
return 0 -- a self-extractor opens with Zip.decode / opts.format = "zip"
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(r, opts?) -> Archive | nil, Err
function Zip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?, Err.Err?)
local zip, err = Container.open(r)
if not zip then
return nil, err
end
return new(zip, not (opts and opts.verify == false)), nil
end
-- create() -> an empty Archive
function Zip.create(): Types.Archive
return new(nil, true)
end
-- ------------------------------------------------------------------------------------------------ encode
-- encode(archive, opts?) -> buffer | nil, Err. archive: an Archive (any format's: its entries are read and written
-- here), or a plain table { [name] = buffer | string }.
function Zip.encode(value: any, opts: WriteOptions?): (buffer?, Err.Err?)
if type(value) ~= "table" then
return nil, Err.new(Err.ARG, "encode: needs an Archive or a { name = data } table")
end
if getmetatable(value) == Archive then
return value:write(opts)
end
local a = new(nil, true)
if value.kind == "archive" and type(value.list) == "function" and type(value.read) == "function" then
for _, e in value:list() do
if e.dir then
a:add(e.name, nil, { mtime = e.mtime })
else
local data, err = value:read(e.name)
if not data then
return nil, err
end
a:add(e.name, data, { mtime = e.mtime })
end
end
else
local names = {}
for k, v in value do
if type(k) ~= "string" or (type(v) ~= "buffer" and type(v) ~= "string") then
return nil, Err.new(Err.ARG, "encode: a { name = data } table needs string names and buffer / string data")
end
table.insert(names, k)
end
table.sort(names)
for _, k in names do
a:add(k, value[k])
end
end
return a:write(opts)
end
-- ------------------------------------------------------------------------------------------------ extras
-- the Archive methods (Types.Archive)
-- list() -> { ArchiveEntry } in archive order (+ crc32, comment)
function Archive.list(self: any): { Types.ArchiveEntry }
local out = {}
for _, name in self._order do
local it = self._items[name]
if it.raw then
local e = it.raw
table.insert(out, {
name = e.name,
size = e.size,
compressedSize = e.compressedSize,
method = METHODS[e.method] or ("method " .. e.method),
mtime = e.mtime,
dir = e.isDir,
crc32 = e.crc32,
comment = e.comment,
})
else
table.insert(out, {
name = name,
size = buffer.len(it.data),
compressedSize = nil,
method = it.opts.method or "deflate",
mtime = it.opts.mtime,
dir = string.sub(name, -1) == "/",
})
end
end
return out
end
-- read(name) -> buffer | nil, Err: an exact name, else the reader's case-insensitive / basename lookup
function Archive.read(self: any, name: string): (buffer?, Err.Err?)
if type(name) ~= "string" then
return nil, Err.new(Err.ARG, "read: name must be a string")
end
local it = self._items[name]
if it and not it.raw then
local copy = buffer.create(buffer.len(it.data))
buffer.copy(copy, 0, it.data)
return copy, nil
end
if it then
return self._zip:read(it.raw, self._verify)
end
if self._zip then
local e = self._zip:find(name)
if e and self._items[e.name] and self._items[e.name].raw == e then
return self._zip:read(e, self._verify)
end
end
return nil, Err.new(Err.ARG, "no entry " .. name)
end
-- add(name, data, opts?): add or replace an entry (data: buffer or string; nil for a directory "name/")
function Archive.add(self: any, name: string, data: any, opts: AddOptions?)
local b: buffer = if type(data) == "string" then buffer.fromstring(data) elseif type(data) == "buffer" then data else buffer.create(0)
if self._items[name] == nil then
table.insert(self._order, name)
end
self._items[name] = { data = b, opts = opts or {} }
end
-- remove(name) -> true when there was such an entry
function Archive.remove(self: any, name: string): boolean
if self._items[name] == nil then
return false
end
self._items[name] = nil
local i = table.find(self._order, name)
if i then
table.remove(self._order, i)
end
return true
end
-- write(opts?) -> buffer | nil, Err: the archive with every change; unchanged entries are copied as they are
function Archive.write(self: any, opts: WriteOptions?): (buffer?, Err.Err?)
local w = Container.writer()
for _, name in self._order do
local it = self._items[name]
local ok, err
if it.raw then
ok, err = w:addRaw(it.raw, self._zip)
else
ok, err = w:add(name, it.data, it.opts)
end
if not ok then
return nil, err
end
end
local o: WriteOptions = opts or {}
if o.comment == nil and self.meta.comment and self.meta.comment ~= "" then
o = table.clone(o)
o.comment = self.meta.comment
end
return w:finish(o)
end
Zip.Container = Container
return Zip