summaryrefslogtreecommitdiff
path: root/linuxnamespaces/tarutils.py
diff options
context:
space:
mode:
Diffstat (limited to 'linuxnamespaces/tarutils.py')
-rw-r--r--linuxnamespaces/tarutils.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/linuxnamespaces/tarutils.py b/linuxnamespaces/tarutils.py
index 6285d5a..5ad60cd 100644
--- a/linuxnamespaces/tarutils.py
+++ b/linuxnamespaces/tarutils.py
@@ -31,8 +31,16 @@ class ZstdTarFile(tarfile.TarFile):
name: str,
mode: typing.Literal["r", "w", "x"] = "r",
fileobj: typing.BinaryIO | None = None,
+ *,
+ compresslevel: int | None = None,
+ threads: int | None = None,
**kwargs: typing.Any,
) -> tarfile.TarFile:
+ """Open a zstd compressed tar archive with the given name for readin or
+ writing. Appending is not supported. The class allows customizing the
+ compression level and the compression concurrency (default parallel)
+ while decompression ignores those arguments.
+ """
if mode not in ("r", "w", "x"):
raise ValueError("mode must be 'r', 'w' or 'x'")
openobj: str | typing.BinaryIO = name if fileobj is None else fileobj
@@ -45,11 +53,21 @@ class ZstdTarFile(tarfile.TarFile):
if mode == "r":
zfobj = zstandard.open(openobj, "rb")
else:
- zfobj = zstandard.open(
- openobj,
- mode + "b",
- cctx=zstandard.ZstdCompressor(write_checksum=True, threads=-1),
- )
+ if threads is None:
+ threads = -1
+ if compresslevel is not None:
+ if compresslevel > 22:
+ raise ValueError(
+ "invalid compression level {compresslevel}"
+ )
+ cctx = zstandard.ZstdCompressor(
+ write_checksum=True, threads=threads, level=compresslevel
+ )
+ else:
+ cctx = zstandard.ZstdCompressor(
+ write_checksum=True, threads=threads
+ )
+ zfobj = zstandard.open(openobj, mode + "b", cctx=cctx)
try:
tarobj = cls.taropen(name, mode, zfobj, **kwargs)
except (OSError, EOFError, zstandard.ZstdError) as exc: