adding support to set the gzip compression level in tarfile
authorEduardo Robles Elvira <edulix@wadobo.com>
Tue, 22 Jul 2014 17:45:22 +0000 (19:45 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Tue, 22 Jul 2014 17:45:22 +0000 (19:45 +0200)
deltatar/tarfile.py

index f0a92f6..356b154 100644 (file)
@@ -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: