zip_unix_modes.py 818 B

1234567891011121314151617181920212223
  1. #!/usr/bin/env python3
  2. """Apply Unix modes stored in ZIP external_attr. ZipFile.extractall() ignores them."""
  3. from __future__ import annotations
  4. import zipfile
  5. from pathlib import Path
  6. def extract_zip_with_unix_modes(zip_path: Path, dest: Path) -> None:
  7. dest.mkdir(parents=True, exist_ok=True)
  8. with zipfile.ZipFile(zip_path) as zf:
  9. for info in zf.infolist():
  10. name = info.filename.replace("\\", "/")
  11. if not name or name.endswith("/"):
  12. (dest / name).mkdir(parents=True, exist_ok=True)
  13. continue
  14. target = dest / name
  15. target.parent.mkdir(parents=True, exist_ok=True)
  16. target.write_bytes(zf.read(info))
  17. mode = (info.external_attr >> 16) & 0o777
  18. if mode:
  19. target.chmod(mode)