Skip to content

Commit

Permalink
operation: archive: Preserve directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer290399 committed Aug 22, 2021
1 parent 7f295bb commit 5e41de5
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions dffml/operation/archive.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from os import name
import tarfile
import zipfile
import pathlib


from ..df.base import op
from ..df.types import Definition

Expand All @@ -18,6 +18,22 @@
)


def recursive_add_to_archive(archive_handle, path, archive_path):
if not isinstance(path, pathlib.Path):
path = pathlib.Path(path)
if path.is_file():
archive_handle(path, archive_path)
elif path.is_dir():
if archive_path:
archive_handle(path, archive_path)
path_names = [pth.name for pth in path.iterdir()]
for name in sorted(path_names):
name = pathlib.Path(name)
recursive_add_to_archive(
archive_handle, path / name, archive_path / name
)


@op(
inputs={"input_directory_path": DIRECTORY, "output_file_path": ZIP_FILE},
outputs={"output_path": OUTPUT_ZIPFILE_PATH},
Expand All @@ -41,8 +57,7 @@ async def make_zip_archive(
Path to the output zip file
"""
with zipfile.ZipFile(output_file_path, "w") as zip:
for file in pathlib.Path(input_directory_path).rglob("*"):
zip.write(file, file.name)
recursive_add_to_archive(zip.write, input_directory_path, "")
return {"output_path": output_file_path}


Expand Down Expand Up @@ -96,8 +111,7 @@ async def make_tar_archive(
Path to the created tar file.
"""
with tarfile.open(output_file_path, mode="x") as tar:
for file in pathlib.Path(input_directory_path).rglob("*"):
tar.add(file, file.name)
recursive_add_to_archive(tar.add, input_directory_path, "")
return {"output_path": output_file_path}


Expand Down

0 comments on commit 5e41de5

Please sign in to comment.