Last active
August 14, 2024 18:06
-
-
Save MawKKe/47eca8b15855791f2d386716a5cefe80 to your computer and use it in GitHub Desktop.
zip-dir - A little CLI utility for creating a .zip from directory tree. Because the usability of "zip" command is pure ass.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Usage: | |
$ zip-dir -i path/to/directory -o myarchive.zip --level 0 | |
or more verbosely: | |
$ zip-dir --input-dir path/to/directory --output-zip myarchive.zip --level 0 | |
Install: | |
1. Download the script and place it into your path (e.g. ~/bin/zip-dir or ~/.local/bin/zip-dir) | |
2. Make it executable: | |
$ chmod +x <path-to-the-file> | |
3. Run it: | |
$ zip-dir -h | |
""" | |
from pathlib import Path | |
import subprocess | |
import argparse | |
import shlex | |
p = argparse.ArgumentParser( | |
description='A little CLI utility for creating a .zip from directory tree. Because the usability of "zip" command is pure ass.' | |
) | |
p.add_argument("--input-dir", "-i", type=Path, required=True, | |
help=("Input directory root. All files in the directory are archived. " | |
"File paths in the archive will be relative to this directory.")) | |
p.add_argument("--output-zip", "-o", type=Path, required=True, help="Output file") | |
p.add_argument("--level", type=int, default=None, help="Compression level, in range 0..=9 where 0 = store only") | |
args = p.parse_args() | |
assert args.level is None or 0 <= args.level <= 9, "level min=0, max=9 where 0 = store only" | |
input_dir = args.input_dir.resolve().as_posix() | |
output_zip = args.output_zip.resolve().as_posix() | |
cmd = ["zip", "-r", output_zip, "."] | |
print(f"cd {output_zip} && {shlex.join(cmd)}") | |
subprocess.run(cmd, cwd=input_dir, check=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment