Skip to content

Commit

Permalink
Add support for storing files without compression
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed May 30, 2024
1 parent c181c3b commit a759930
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/zcmds/cmds/common/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ def zf_write(zf: zipfile.ZipFile, file_abs: str, archive_path: Optional[str]) ->


def make_archive(
folder_or_file: str, archive_name: str, root_dir: Optional[str] = None
folder_or_file: str, archive_name: str, root_dir: Optional[str] = None, no_deflate: bool = False
) -> None:
if archive_name is None:
archive_name = folder_or_file + ".zip"
root_dir = root_dir or os.path.dirname(folder_or_file)

try:
with zipfile.ZipFile(archive_name, "w", zipfile.ZIP_DEFLATED) as zf:
compression = zipfile.ZIP_STORED if no_deflate else zipfile.ZIP_DEFLATED
with zipfile.ZipFile(archive_name, "w", compression) as zf:
if os.path.isfile(folder_or_file):
zf_write(zf=zf, file_abs=folder_or_file, archive_path=None)
else:
Expand Down Expand Up @@ -58,6 +59,11 @@ def make_argparse() -> argparse.ArgumentParser:
help="Name of archive",
nargs="?",
)
parser.add_argument(
"--no-deflate",
action="store_true",
help="Store files without compression",
)
return parser


Expand All @@ -71,7 +77,7 @@ def chop_ext(filename: str) -> str:
def main_args(args: Any) -> int:
folder_or_file = args.folder_or_file
archive_name = args.archive_name or chop_ext(folder_or_file) + ".zip"
make_archive(folder_or_file, archive_name)
make_archive(folder_or_file, archive_name, no_deflate=args.no_deflate)
return 0


Expand Down

0 comments on commit a759930

Please sign in to comment.