team-ljyzipelo

공개
71줄2.8 KB
1import bz2
2import gzip
3import lzma
4from pathlib import Path
5import sys
6import zipfile
7
8
9root = Path(sys.argv[1])
10case = sys.argv[2]
11if case == "bomb":
12 with gzip.open(root / "input", "wb", compresslevel=9) as output:
13 block = bytes(1024 * 1024)
14 for _ in range(257):
15 output.write(block)
16elif case in {"header-budget-zip", "header-budget-tar"}:
17 names = [f"{entry_id:04}-" + "x" * 45 for entry_id in range(1000)]
18 if case.endswith("-zip"):
19 with zipfile.ZipFile(root / "input", "w") as output:
20 for name in names:
21 output.writestr(name, b"")
22 else:
23 import tarfile
24
25 with tarfile.open(root / "input", "w", format=tarfile.USTAR_FORMAT) as output:
26 for name in names:
27 output.addfile(tarfile.TarInfo(name))
28elif case.startswith("header-budget-"):
29 from seven_zip_fixtures import archive
30
31 entries = [(f"{entry_id:04}-" + "x" * 45, b"", 0x20) for entry_id in range(1000)]
32 size = int(case.rsplit("-", 1)[1])
33 if size:
34 entries.append(("payload", b"x" * size, 0x20))
35 (root / "input").write_bytes(archive(entries, encoded="encoded" in case))
36elif case == "wide-names":
37 with zipfile.ZipFile(root / "input", "w") as archive:
38 for entry_id in range(9000):
39 archive.writestr(f"{entry_id:04}" + "x" * 236, b"")
40elif case == "many-entries":
41 with zipfile.ZipFile(root / "input", "w") as archive:
42 for entry_id in range(200_000):
43 archive.writestr(f"e{entry_id}", b"")
44elif case in {"phase-directories", "phase-files"}:
45 with zipfile.ZipFile(root / "input", "w") as archive:
46 for entry_id in range(8):
47 archive.writestr(
48 f"entry-{entry_id}/" if case == "phase-directories" else f"entry-{entry_id}",
49 b"" if case == "phase-directories" else b"x",
50 )
51elif case == "selection-entries":
52 with zipfile.ZipFile(root / "input", "w") as archive:
53 for entry_id in range(32_770):
54 archive.writestr(f"e{entry_id}", b"x")
55elif case == "empty-directories":
56 with zipfile.ZipFile(root / "input", "w") as archive:
57 for entry_id in range(36):
58 archive.writestr(f"entry-{entry_id}/", b"")
59elif case == "split-zip":
60 with zipfile.ZipFile(root / "input", "w", compression=zipfile.ZIP_DEFLATED) as archive:
61 block = bytes(1024 * 1024)
62 for name in ["first.bin", "second.bin"]:
63 with archive.open(name, "w") as output:
64 for _ in range(129):
65 output.write(block)
66elif case in {"bz2", "xz"}:
67 source = Path(__file__).with_name("fixtures").joinpath("sample.tar").read_bytes()
68 encode = bz2.compress if case == "bz2" else lzma.compress
69 (root / "input").write_bytes(encode(source))
70else:
71 raise ValueError(case)