| 1234567891011121314151617181920212223 |
- #!/usr/bin/env python3
- """Apply Unix modes stored in ZIP external_attr. ZipFile.extractall() ignores them."""
- from __future__ import annotations
- import zipfile
- from pathlib import Path
- def extract_zip_with_unix_modes(zip_path: Path, dest: Path) -> None:
- dest.mkdir(parents=True, exist_ok=True)
- with zipfile.ZipFile(zip_path) as zf:
- for info in zf.infolist():
- name = info.filename.replace("\\", "/")
- if not name or name.endswith("/"):
- (dest / name).mkdir(parents=True, exist_ok=True)
- continue
- target = dest / name
- target.parent.mkdir(parents=True, exist_ok=True)
- target.write_bytes(zf.read(info))
- mode = (info.external_attr >> 16) & 0o777
- if mode:
- target.chmod(mode)
|