From 2b82f50c6b4cec502c1bf716b8b684f931cb6b1e Mon Sep 17 00:00:00 2001 From: Eduardo Robles Elvira Date: Tue, 22 Jul 2014 19:45:22 +0200 Subject: [PATCH] adding support to set the gzip compression level in tarfile --- deltatar/tarfile.py | 32 +++++++++++++++++++------------- 1 files changed, 19 insertions(+), 13 deletions(-) diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index f0a92f6..356b154 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -356,7 +356,7 @@ class _Stream: def __init__(self, name, mode, comptype, fileobj, bufsize, concat_stream=False, enctype='', password="", - key_length=128): + key_length=128, compresslevel=9): """Construct a _Stream object. """ self._extfileobj = True @@ -389,6 +389,7 @@ class _Stream: self.dbuf = b"" self.aes_buf = b"" self.exception = None + self.compresslevel = compresslevel try: if comptype == "gz": @@ -454,10 +455,11 @@ class _Stream: def _init_write_gz(self): """Initialize for writing with gzip compression. """ - self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED, - -self.zlib.MAX_WBITS, - self.zlib.DEF_MEM_LEVEL, - 0) + self.cmp = self.zlib.compressobj(self.compresslevel, + self.zlib.DEFLATED, + -self.zlib.MAX_WBITS, + self.zlib.DEF_MEM_LEVEL, + 0) # if aes, we encrypt after compression if self.enctype == 'aes': @@ -498,10 +500,11 @@ class _Stream: self.closed = False self.concat_pos = 0 self.crc = self.zlib.crc32(b"") - self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED, - -self.zlib.MAX_WBITS, - self.zlib.DEF_MEM_LEVEL, - 0) + self.cmp = self.zlib.compressobj(self.compresslevel, + self.zlib.DEFLATED, + -self.zlib.MAX_WBITS, + self.zlib.DEF_MEM_LEVEL, + 0) # if aes, we encrypt after compression if self.enctype == 'aes': @@ -1838,7 +1841,8 @@ class TarFile(object): # by adding it to the mapping in OPEN_METH. @classmethod - def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs): + def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, + compresslevel=9, **kwargs): """Open a tar archive for reading, writing or appending. Return an appropriate TarFile class. @@ -1904,7 +1908,7 @@ class TarFile(object): func = getattr(cls, cls.OPEN_METH[comptype]) else: raise CompressionError("unknown compression type %r" % comptype) - return func(name, filemode, fileobj, **kwargs) + return func(name, filemode, fileobj, compresslevel, **kwargs) elif "|" in mode: filemode, comptype = mode.split("|", 1) @@ -1915,7 +1919,8 @@ class TarFile(object): raise ValueError("mode must be 'r' or 'w'") t = cls(name, filemode, - _Stream(name, filemode, comptype, fileobj, bufsize), + _Stream(name, filemode, comptype, fileobj, bufsize, + compresslevel=compresslevel), **kwargs) t._extfileobj = False return t @@ -1959,7 +1964,8 @@ class TarFile(object): stream = _Stream(name, filemode, comptype, fileobj, bufsize, concat_stream=True, enctype=enctype, - password=password, key_length=key_length) + password=password, key_length=key_length, + compresslevel=compresslevel) try: t = cls(name, filemode, stream, **kwargs) except: -- 1.7.1