From ec3c34c63769f54af1953e4a005de9089320756c Mon Sep 17 00:00:00 2001 From: Yidong Ren Date: Tue, 21 Jul 2026 23:29:47 +0000 Subject: [PATCH] zip and rar files to honor --temp-dir cli --- cli.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/cli.py b/cli.py index 030dbf1..6a1e9ac 100755 --- a/cli.py +++ b/cli.py @@ -205,31 +205,30 @@ def main(): @contextlib.contextmanager def prepare_source_path(path: Path): if _is_zip(path): - import tempfile, zipfile - with tempfile.TemporaryDirectory() as tmpdir: + import zipfile + with managed_temp_dir(args.temp_dir) as tmpdir: try: with zipfile.ZipFile(path) as zf: # Path traversal validation (same logic MkPFS used) for member in zf.infolist(): - dest = Path(tmpdir) / member.filename + dest = tmpdir / member.filename try: - dest.resolve().relative_to(Path(tmpdir).resolve()) + dest.resolve().relative_to(tmpdir.resolve()) except ValueError: print(f"[ERROR] ZIP path traversal detected: {member.filename}") sys.exit(1) - zf.extractall(tmpdir, pwd=args.password.encode() if args.password else None) - yield Path(tmpdir) + zf.extractall(str(tmpdir), pwd=args.password.encode() if args.password else None) + yield tmpdir except (zipfile.BadZipFile, RuntimeError) as exc: print(f"[ERROR] ZIP extraction failed: {exc}") sys.exit(1) elif _is_rar(path): - import tempfile from unrar import rarfile - with tempfile.TemporaryDirectory() as tmpdir: + with managed_temp_dir(args.temp_dir) as tmpdir: try: with rarfile.RarFile(path, pwd=args.password) as rf: - rf.extractall(tmpdir) - yield Path(tmpdir) + rf.extractall(str(tmpdir)) + yield tmpdir except rarfile.RarWrongPassword: print("[ERROR] RAR extraction failed: wrong or missing password") sys.exit(1)