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:
1 parent
b6e2b9e68d
commit
17778cff1b
7 files changed
+1349
No files matched your search
@@ -0,0 +1,161 @@
|
||||
# ZIP fixtures (tools/fixtures.py): containers/Zip + archive/zip. Ported from zip-luau tools/fixtures.py.
|
||||
# tests/gen/zip_cases.luau generated archives with Python zipfile's view of every entry ({ name, size, crc, method }):
|
||||
# stored and deflate entries, an archive comment holding a fake end record, data descriptors,
|
||||
# force_zip64, a hand-built all-ZIP64 archive, 66000 entries (ZIP64 end record), a prefix
|
||||
# stub, a wrong local extra length, unicode names, empty; plus every .wsz skin in $SKINS_DIR
|
||||
# when it is set (the Webamp demo skins; not redistributable, so not committed). Archives
|
||||
# over 1 MB are stored zlib-compressed (dataZ)
|
||||
import io, os, random, struct, sys, zipfile, zlib
|
||||
|
||||
SKINS = os.environ.get("SKINS_DIR", "")
|
||||
TEXT = b"".join(b"line %d: the quick brown fox jumps over the lazy dog\n" % i for i in range(3000))
|
||||
|
||||
|
||||
class Unseekable(io.RawIOBase):
|
||||
def __init__(self):
|
||||
self.buf = bytearray()
|
||||
def writable(self):
|
||||
return True
|
||||
def write(self, b):
|
||||
self.buf += b
|
||||
return len(b)
|
||||
|
||||
def mk(files, comment=b"", stream=False, force64=False):
|
||||
out = Unseekable() if stream else io.BytesIO()
|
||||
with zipfile.ZipFile(out, "w") as z:
|
||||
for name, data, method in files:
|
||||
zi = zipfile.ZipInfo(name, (2020, 5, 17, 13, 37, 42))
|
||||
zi.compress_type = method
|
||||
if force64:
|
||||
with z.open(zi, "w", force_zip64=True) as f:
|
||||
f.write(data)
|
||||
else:
|
||||
z.writestr(zi, data)
|
||||
z.comment = comment
|
||||
return bytes(out.buf if stream else out.getvalue())
|
||||
|
||||
def zip64_by_hand(files):
|
||||
# every size / offset field is 0xFFFFFFFF with the values in ZIP64 extras, plus the ZIP64 end record + locator
|
||||
local, cd = bytearray(), bytearray()
|
||||
for name, data in files:
|
||||
n = name.encode()
|
||||
c = zlib.compressobj(9, zlib.DEFLATED, -15)
|
||||
comp = c.compress(data) + c.flush()
|
||||
crc = zlib.crc32(data)
|
||||
off = len(local)
|
||||
lx = struct.pack("<HHQQ", 1, 16, len(data), len(comp))
|
||||
local += struct.pack("<IHHHHHIIIHH", 0x04034b50, 45, 0, 8, 0, 0x21, crc, 0xFFFFFFFF, 0xFFFFFFFF, len(n), len(lx)) + n + lx + comp
|
||||
cx = struct.pack("<HHQQQ", 1, 24, len(data), len(comp), off)
|
||||
cd += struct.pack("<IHHHHHHIIIHHHHHII", 0x02014b50, 45, 45, 0, 8, 0, 0x21, crc, 0xFFFFFFFF, 0xFFFFFFFF, len(n), len(cx), 0, 0, 0, 0, 0xFFFFFFFF) + n + cx
|
||||
cdoff = len(local)
|
||||
z64 = len(local) + len(cd)
|
||||
rec = struct.pack("<IQHHIIQQQQ", 0x06064b50, 44, 45, 45, 0, 0, len(files), len(files), len(cd), cdoff)
|
||||
loc = struct.pack("<IIQI", 0x07064b50, 0, z64, 1)
|
||||
eocd = struct.pack("<IHHHHIIH", 0x06054b50, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0)
|
||||
return bytes(local + cd + rec + loc + eocd)
|
||||
|
||||
|
||||
def entries(data, limit=None):
|
||||
z = zipfile.ZipFile(io.BytesIO(data))
|
||||
return len(z.infolist()), [[i.filename, i.file_size, i.CRC, i.compress_type] for i in z.infolist()[:limit]]
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
S, D = zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED
|
||||
rng = random.Random(3)
|
||||
TEXT = b"".join(b"line %d: the quick brown fox jumps over the lazy dog\n" % i for i in range(3000))
|
||||
|
||||
class Unseekable(io.RawIOBase):
|
||||
def __init__(self):
|
||||
self.buf = bytearray()
|
||||
def writable(self):
|
||||
return True
|
||||
def write(self, b):
|
||||
self.buf += b
|
||||
return len(b)
|
||||
|
||||
def mk(files, comment=b"", stream=False, force64=False):
|
||||
out = Unseekable() if stream else io.BytesIO()
|
||||
with zipfile.ZipFile(out, "w") as z:
|
||||
for name, data, method in files:
|
||||
zi = zipfile.ZipInfo(name, (2020, 5, 17, 13, 37, 42))
|
||||
zi.compress_type = method
|
||||
if force64:
|
||||
with z.open(zi, "w", force_zip64=True) as f:
|
||||
f.write(data)
|
||||
else:
|
||||
z.writestr(zi, data)
|
||||
z.comment = comment
|
||||
return bytes(out.buf if stream else out.getvalue())
|
||||
|
||||
def zip64_by_hand(files):
|
||||
# every size / offset field is 0xFFFFFFFF with the values in ZIP64 extras, plus the ZIP64 end record + locator
|
||||
local, cd = bytearray(), bytearray()
|
||||
for name, data in files:
|
||||
n = name.encode()
|
||||
c = zlib.compressobj(9, zlib.DEFLATED, -15)
|
||||
comp = c.compress(data) + c.flush()
|
||||
crc = zlib.crc32(data)
|
||||
off = len(local)
|
||||
lx = struct.pack("<HHQQ", 1, 16, len(data), len(comp))
|
||||
local += struct.pack("<IHHHHHIIIHH", 0x04034b50, 45, 0, 8, 0, 0x21, crc, 0xFFFFFFFF, 0xFFFFFFFF, len(n), len(lx)) + n + lx + comp
|
||||
cx = struct.pack("<HHQQQ", 1, 24, len(data), len(comp), off)
|
||||
cd += struct.pack("<IHHHHHHIIIHHHHHII", 0x02014b50, 45, 45, 0, 8, 0, 0x21, crc, 0xFFFFFFFF, 0xFFFFFFFF, len(n), len(cx), 0, 0, 0, 0, 0xFFFFFFFF) + n + cx
|
||||
cdoff = len(local)
|
||||
z64 = len(local) + len(cd)
|
||||
rec = struct.pack("<IQHHIIQQQQ", 0x06064b50, 44, 45, 45, 0, 0, len(files), len(files), len(cd), cdoff)
|
||||
loc = struct.pack("<IIQI", 0x07064b50, 0, z64, 1)
|
||||
eocd = struct.pack("<IHHHHIIH", 0x06054b50, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0)
|
||||
return bytes(local + cd + rec + loc + eocd)
|
||||
|
||||
S, D = zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED
|
||||
basic = [
|
||||
("readme.txt", TEXT, D),
|
||||
("Skin/Main.BMP", rng.randbytes(5000) + bytes(20000), D),
|
||||
("Skin/stored.bin", rng.randbytes(3000), S),
|
||||
("Skin/", b"", S),
|
||||
("Skin/sub/deep.txt", b"deep", D),
|
||||
("empty.txt", b"", D),
|
||||
("__MACOSX/Skin/._Main.BMP", b"\0\5\x16\7junk", S),
|
||||
("Skïn/ünicode.txt", "héllo".encode(), D),
|
||||
]
|
||||
archives = [] # (label, bytes, expect: list of (name, size, crc) or None for "parse with zipfile")
|
||||
REF = {} # label -> the archive zipfile reads for the expected listing (when zipfile cannot read the real one)
|
||||
def add(label, data, ref=None):
|
||||
archives.append((label, data))
|
||||
if ref is not None:
|
||||
REF[label] = ref
|
||||
|
||||
add("basic", mk(basic))
|
||||
# zipfile itself takes the fake end record in this comment (it searches for the last signature)
|
||||
add("comment", mk(basic[:3], comment=b"a comment with a fake end record PK\x05\x06" + b"\0" * 18 + b" and more" * 100), mk(basic[:3]))
|
||||
add("descriptors", mk(basic[:6], stream=True))
|
||||
add("force_zip64", mk(basic[:6], force64=True))
|
||||
add("zip64_by_hand", zip64_by_hand([("a.txt", TEXT), ("b/c.bin", rng.randbytes(4000)), ("z.txt", b"")]))
|
||||
add("prefixed", b"MZ" + b"\x90" * 3000 + mk(basic[:3]))
|
||||
add("empty", mk([]))
|
||||
w = bytearray(mk([("x.txt", TEXT, D), ("y.txt", b"why" * 1000, S)]))
|
||||
struct.pack_into("<H", w, 28, 7) # first local header: wrong extra length (the central directory has 0)
|
||||
add("wrong_local_extra", bytes(w), mk([("x.txt", TEXT, D), ("y.txt", b"why" * 1000, S)]))
|
||||
many = mk([("d/%05d.txt" % i, b"%d" % i, S) for i in range(66000)])
|
||||
add("many", many)
|
||||
|
||||
skins = []
|
||||
if os.path.isdir(SKINS):
|
||||
for name in sorted(os.listdir(SKINS)):
|
||||
if name.lower().endswith(".wsz"):
|
||||
with open(os.path.join(SKINS, name), "rb") as f:
|
||||
skins.append((name, f.read()))
|
||||
|
||||
|
||||
out = {"archives": [], "skins": []}
|
||||
for key, group in (("archives", archives), ("skins", skins)):
|
||||
for label, data in group:
|
||||
count, ents = entries(REF.get(label, data), 200 if label == "many" else None)
|
||||
c = {"name": label, "count": count, "entries": ents}
|
||||
if len(data) > 1000000:
|
||||
c["dataZ"] = zlib.compress(data, 9) # big archives (66000 entries): zlib, inflated by the test
|
||||
else:
|
||||
c["data"] = data
|
||||
out[key].append(c)
|
||||
ctx.emit("zip_cases", out)
|
||||
Reference in new issue
Block a user