From 0c7191d050b7e733ebeafdcf254efcaec3c3f3be Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Thu, 4 Jan 2024 13:03:11 +0100 Subject: [PATCH] Add compress utility --- cutekit/shell.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cutekit/shell.py b/cutekit/shell.py index c9ad547..cc2aa40 100644 --- a/cutekit/shell.py +++ b/cutekit/shell.py @@ -280,3 +280,23 @@ def nproc() -> int: Return the number of processors """ return os.cpu_count() or 1 + + +def compress(path: str, dest: Optional[str] = None, format: str = "zstd") -> str: + """ + Compress a file or directory + """ + + if dest is None: + dest = path + "." + format + + _logger.debug(f"Compressing {path} to {dest}") + + if format == "zip": + exec("zip", "-r", dest, path) + elif format == "zstd": + exec("zsdt", "-q", "-o", dest, path) + else: + raise RuntimeError(f"Unknown compression format {format}") + + return dest