Add compress utility

This commit is contained in:
Sleepy Monax 2024-01-04 13:03:11 +01:00
parent 78130b4f7f
commit 0c7191d050

View file

@ -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