team-ljyzipelo

공개
32줄1.1 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 == "empty-directories":
17 with zipfile.ZipFile(root / "input", "w") as archive:
18 for entry_id in range(36):
19 archive.writestr(f"entry-{entry_id}/", b"")
20elif case == "split-zip":
21 with zipfile.ZipFile(root / "input", "w", compression=zipfile.ZIP_DEFLATED) as archive:
22 block = bytes(1024 * 1024)
23 for name in ["first.bin", "second.bin"]:
24 with archive.open(name, "w") as output:
25 for _ in range(129):
26 output.write(block)
27elif case in {"bz2", "xz"}:
28 source = Path(__file__).with_name("fixtures").joinpath("sample.tar").read_bytes()
29 encode = bz2.compress if case == "bz2" else lzma.compress
30 (root / "input").write_bytes(encode(source))
31else:
32 raise ValueError(case)