unify zlib initialization
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 27 Mar 2017 12:10:51 +0000 (14:10 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
deltatar/tarfile.py

index 9708f97..e4b6910 100644 (file)
@@ -610,6 +610,22 @@ class _Stream:
         self.__write(self.name + NUL)
 
 
+    def _finalize_write_gz (self):
+        if self.cmp is not None:
+            chunk = self.buf + self.cmp.flush()
+            if chunk:
+                if self.comptype == "gz":
+                    # The native zlib crc is an unsigned 32-bit integer, but
+                    # the Python wrapper implicitly casts that to a signed C
+                    # long.  So, on a 32-bit box self.crc may "look negative",
+                    # while the same crc on a 64-bit box may "look positive".
+                    # To avoid irksome warnings from the `struct` module, force
+                    # it to look positive on all boxes.
+                    chunk += struct.pack("<L", self.crc & 0xffffffff)
+                    chunk += struct.pack("<L", self.concat_pos & 0xffffFFFF)
+                self.__enc_write (chunk)
+
+
     def new_compression_block(self):
         '''
         Used to notify a new tar block is coming to create a new zip block
@@ -733,26 +749,12 @@ class _Stream:
         if self.closed:
             return
 
-        if self.mode == "w" and self.cmp is not None:
-            self.buf += self.cmp.flush()
-
-        if self.mode == "w" and self.buf:
-            chunk = self.buf
+        if self.mode == "w":
+            self._finalize_write_gz ()
+            self.__enc_write(self.buf)
+            if self.enc is not None:
+                self._finalize_write_encrypt ()
             self.buf = b""
-            if self.comptype == "gz":
-                # The native zlib crc is an unsigned 32-bit integer, but
-                # the Python wrapper implicitly casts that to a signed C
-                # long.  So, on a 32-bit box self.crc may "look negative",
-                # while the same crc on a 64-bit box may "look positive".
-                # To avoid irksome warnings from the `struct` module, force
-                # it to look positive on all boxes.
-                chunk += struct.pack("<L", self.crc & 0xffffffff)
-                chunk += struct.pack("<L", self.concat_pos & 0xffffFFFF)
-            self.__enc_write(chunk)
-            finalize_e = getattr (self, "_finalize_write_encrypt", None)
-            finalize_c = getattr (self, "_finalize_write_gz"     , None)
-            if finalize_c is not None: finalize_c ()
-            if finalize_e is not None: finalize_e ()
 
         if close_fileobj and not self._extfileobj:
             self.fileobj.close()