| 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 == "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"") |
| 20 | elif 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) |
| 27 | elif 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)) |
| 31 | else: |
| 32 | raise ValueError(case) |