Skip to content

Commit

Permalink
feat(utils): recursive unzip function
Browse files Browse the repository at this point in the history
  • Loading branch information
M4RC0Sx committed Aug 16, 2024
1 parent c52320b commit f394789
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions esiosapy/utils/zip_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pathlib import Path
from typing import Union
from zipfile import ZipFile


def recursive_unzip(
zip_path: Union[str, Path], unzip_path: Union[str, Path], remove: bool = False
) -> None:
zip_path, unzip_path = Path(zip_path), Path(unzip_path)

with ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(unzip_path)

for zip_subfile in unzip_path.rglob("*.zip"):
nested_unzip_path = zip_subfile.parent / zip_subfile.stem
with ZipFile(zip_subfile, "r") as zip_ref:
zip_ref.extractall(nested_unzip_path)

recursive_unzip(zip_subfile, nested_unzip_path, remove)

if remove and zip_subfile.exists():
zip_subfile.unlink()

if remove and zip_path.exists():
zip_path.unlink()

0 comments on commit f394789

Please sign in to comment.