# 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(" 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(" 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)