team-ljyzipelo

공개
51줄1.9 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 == "wide-names":
17 with zipfile.ZipFile(root / "input", "w") as archive:
18 for entry_id in range(9000):
19 archive.writestr(f"{entry_id:04}" + "x" * 236, b"")
20elif case == "many-entries":
21 with zipfile.ZipFile(root / "input", "w") as archive:
22 for entry_id in range(200_000):
23 archive.writestr(f"e{entry_id}", b"")
24elif case in {"phase-directories", "phase-files"}:
25 with zipfile.ZipFile(root / "input", "w") as archive:
26 for entry_id in range(8):
27 archive.writestr(
28 f"entry-{entry_id}/" if case == "phase-directories" else f"entry-{entry_id}",
29 b"" if case == "phase-directories" else b"x",
30 )
31elif case == "selection-entries":
32 with zipfile.ZipFile(root / "input", "w") as archive:
33 for entry_id in range(32_770):
34 archive.writestr(f"e{entry_id}", b"x")
35elif case == "empty-directories":
36 with zipfile.ZipFile(root / "input", "w") as archive:
37 for entry_id in range(36):
38 archive.writestr(f"entry-{entry_id}/", b"")
39elif case == "split-zip":
40 with zipfile.ZipFile(root / "input", "w", compression=zipfile.ZIP_DEFLATED) as archive:
41 block = bytes(1024 * 1024)
42 for name in ["first.bin", "second.bin"]:
43 with archive.open(name, "w") as output:
44 for _ in range(129):
45 output.write(block)
46elif case in {"bz2", "xz"}:
47 source = Path(__file__).with_name("fixtures").joinpath("sample.tar").read_bytes()
48 encode = bz2.compress if case == "bz2" else lzma.compress
49 (root / "input").write_bytes(encode(source))
50else:
51 raise ValueError(case)