comments: containers Avi / Zip
This commit is contained in:
1 parent
24cbddaafa
commit
9c096a6331
2 files changed
+11
-60
No files matched your search
@@ -1,23 +1,9 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- containers/Zip: the ZIP container (PKWARE APPNOTE 6.3): a reader and a writer. The reader is absorbed from
|
||||
-- zip-luau 121d4b0 (same author, no third-party code): the end of central directory record (searched back over an
|
||||
-- archive comment), the central directory, ZIP64 (records, locator, extra fields), a prefix shift (self-extractor
|
||||
-- stubs), stored and deflated entries with a CRC-32 check, data descriptors, a wrong local header fallback,
|
||||
-- case-insensitive / basename lookup that skips __MACOSX. The DEFLATE codec is now required directly
|
||||
-- (codecs/compress/Deflate) instead of injected. The writer is new: stored / deflated entries (codecs/compress/Deflate compress, stored
|
||||
-- when that is not smaller), UTF-8 names, DOS times, raw copies of existing entries, ZIP64 when needed.
|
||||
--
|
||||
-- local zip, err = Zip.open(buf) -- buffer, string or core/Bytes Reader
|
||||
-- for _, e in zip:list() do print(e.name, e.size) end
|
||||
-- local data, err = zip:read("Main.bmp") -- exact, then case-insensitive, then by basename; CRC-checked
|
||||
--
|
||||
-- local w = Zip.writer()
|
||||
-- w:add("readme.txt", "hello", { level = 9 }) -- or { method = "store" }, mtime = unix seconds
|
||||
-- local file = w:finish({ comment = "..." })
|
||||
--
|
||||
-- Never throws: corrupt archives and bad arguments return nil, Err. Parsing runs interpreted (it is cold); the native
|
||||
-- code it reaches (Deflate, Crc32) does not throw either.
|
||||
-- containers/Zip: ZIP (PKWARE APPNOTE 6.3), read and written. The reader is absorbed from zip-luau 121d4b0 (same author,
|
||||
-- no third-party code): EOCD search, ZIP64, prefix shifts (self-extractor stubs), stored / deflated entries, data
|
||||
-- descriptors. The writer is new: stored / deflated, UTF-8 names, DOS times, raw entry copies, ZIP64 when needed.
|
||||
-- Never throws: corrupt archives and bad arguments return nil, Err.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
@@ -50,7 +36,6 @@ export type Entry = {
|
||||
cdExtraLen: number,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ read
|
||||
local readu8, readu16, readu32 = buffer.readu8, buffer.readu16, buffer.readu32
|
||||
|
||||
local SIG_LOCAL, SIG_CD, SIG_EOCD = 0x04034b50, 0x02014b50, 0x06054b50
|
||||
@@ -85,7 +70,6 @@ local Archive = {}
|
||||
Archive.__index = Archive
|
||||
|
||||
|
||||
-- isZip(buf) -> true when buf starts like a zip (a local header, or the end record of an empty archive)
|
||||
function Zip.isZip(buf: buffer | string): boolean
|
||||
local b = if type(buf) == "string" then buffer.fromstring(string.sub(buf, 1, 4)) else buf
|
||||
return sigAt(b, 0, SIG_LOCAL) or sigAt(b, 0, SIG_EOCD)
|
||||
@@ -253,8 +237,6 @@ local function parse(b: buffer): (any, Err.Err?)
|
||||
}, Archive)
|
||||
end
|
||||
|
||||
-- open(src) -> Archive | nil, Err. src: a buffer, a string, or a core/Bytes Reader (its range is copied when it is a
|
||||
-- part of a bigger buffer)
|
||||
function Zip.open(src: any): (any, Err.Err?)
|
||||
local b: buffer
|
||||
if type(src) == "string" then
|
||||
@@ -279,14 +261,11 @@ function Zip.open(src: any): (any, Err.Err?)
|
||||
return res, err
|
||||
end
|
||||
|
||||
-- every entry (directories included), in central directory order
|
||||
function Archive.list(self: any): { Entry }
|
||||
return self.entries
|
||||
end
|
||||
|
||||
-- find(name) -> entry | nil: the exact path, else the path case-insensitively, else the basename
|
||||
-- case-insensitively (skins sometimes nest everything in a folder). "\" and "/" are the same; directories and
|
||||
-- macOS "__MACOSX/" / "._" files never match by basename.
|
||||
-- lookup: exact, then case-insensitive, then by basename (never __MACOSX/ or ._ files); "\\" and "/" are the same
|
||||
function Archive.find(self: any, name: string): Entry?
|
||||
local n = normalize(name)
|
||||
local e = self._exact[n]
|
||||
@@ -327,8 +306,7 @@ local function extract(b: buffer, e: Entry, dataStart: number): (buffer?, any)
|
||||
return out
|
||||
end
|
||||
|
||||
-- the data offset candidates of an entry: the local header's own lengths, then the central directory's (some writers
|
||||
-- get the local extra length wrong)
|
||||
-- data offset candidates: the local header's lengths, then the central directory's (some writers get the local extra wrong)
|
||||
function Zip._starts(b: buffer, e: Entry): { number }
|
||||
local o = e.offset
|
||||
local starts = {}
|
||||
@@ -345,7 +323,6 @@ function Zip._starts(b: buffer, e: Entry): { number }
|
||||
return starts
|
||||
end
|
||||
|
||||
-- read(entry | name, verify = true) -> buffer | nil, Err
|
||||
function Archive.read(self: any, entry: Entry | string, verify: boolean?): (buffer?, Err.Err?)
|
||||
local e: Entry? = if type(entry) == "string" then self:find(entry) else entry :: Entry
|
||||
if not e then
|
||||
@@ -390,7 +367,6 @@ function Archive.read(self: any, entry: Entry | string, verify: boolean?): (buff
|
||||
return nil, Err.new(if firstErr and string.find(firstErr, "CRC", 1, true) then Err.CHECKSUM else code, e.name .. ": " .. tostring(firstErr))
|
||||
end
|
||||
|
||||
-- readString(entry | name, verify?) -> string | nil, Err
|
||||
function Archive.readString(self: any, entry: Entry | string, verify: boolean?): (string?, Err.Err?)
|
||||
local out, err = self:read(entry, verify)
|
||||
if not out then
|
||||
@@ -399,7 +375,6 @@ function Archive.readString(self: any, entry: Entry | string, verify: boolean?):
|
||||
return buffer.tostring(out), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ write
|
||||
local Writer = {}
|
||||
Writer.__index = Writer
|
||||
|
||||
@@ -416,7 +391,6 @@ export type FinishOptions = {
|
||||
zip64: boolean?, -- force the ZIP64 records (default: only when counts, sizes or offsets need them)
|
||||
}
|
||||
|
||||
-- writer() -> Writer
|
||||
function Zip.writer(): any
|
||||
return setmetatable({ files = {} :: { any } }, Writer)
|
||||
end
|
||||
@@ -463,7 +437,6 @@ local function isAscii(s: string): boolean
|
||||
return string.find(s, "[\128-\255]") == nil
|
||||
end
|
||||
|
||||
-- add(name, data, opts?) -> true | false, Err: compresses now (data: buffer or string)
|
||||
function Writer.add(self: any, name: string, data: any, opts: AddOptions?): (boolean, Err.Err?)
|
||||
if type(name) ~= "string" or name == "" or #name > 65535 then
|
||||
return false, Err.new(Err.ARG, "add: bad entry name")
|
||||
@@ -513,7 +486,6 @@ function Writer.add(self: any, name: string, data: any, opts: AddOptions?): (boo
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- addRaw(entry, archive): copy an entry of an opened archive without recompressing it -> true | false, Err
|
||||
function Writer.addRaw(self: any, e: Entry, archive: any, newName: string?): (boolean, Err.Err?)
|
||||
local b = archive.buf
|
||||
local start: number? = nil
|
||||
@@ -544,7 +516,6 @@ function Writer.addRaw(self: any, e: Entry, archive: any, newName: string?): (bo
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- finish(opts?) -> the archive | nil, Err
|
||||
function Writer.finish(self: any, opts: FinishOptions?): (buffer?, Err.Err?)
|
||||
local comment = opts and opts.comment or ""
|
||||
if #comment > 65535 then
|
||||
|
||||
Reference in new issue
Block a user