| 1 | import bz2 |
| 2 | import gzip |
| 3 | import lzma |
| 4 | from pathlib import Path |
| 5 | import sys |
| 6 | import zipfile |
| 7 | |
| 8 | |
| 9 | root = Path(sys.argv[1]) |
| 10 | case = sys.argv[2] |
| 11 | if 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) |
| 16 | elif 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"") |
| 20 | elif 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"") |
| 24 | elif 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 | ) |
| 31 | elif 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") |
| 35 | elif 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"") |
| 39 | elif 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) |
| 46 | elif 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)) |
| 50 | else: |
| 51 | raise ValueError(case) |