annotate zipfiles/zipfile321.diff @ 255:d32b14e5a43b default tip

Added support for LZMA/XZ-compressed tar files (Python 3.3+)
author Oleg Oshmyan <chortos@inbox.lv>
date Sun, 19 Oct 2014 01:33:22 +0200
parents 8196d2c0d6f8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
1 --- /usr/local/lib/python3.2/zipfile.py 2011-07-14 16:08:50.000000000 +0300
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
2 +++ zipfile32.py 2011-07-14 18:31:32.000000000 +0300
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3 @@ -22,8 +22,14 @@
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4 zlib = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
5 crc32 = binascii.crc32
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
6
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7 +try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 + import bz2 # We may need its compression method
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
9 +except ImportError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
10 + bz2 = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
11 +
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
12 __all__ = ["BadZipFile", "BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED",
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
13 - "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
14 + "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile",
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
15 + "ZIP_BZIP2"]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
16
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
17 class BadZipFile(Exception):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
18 pass
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
19 @@ -45,6 +51,7 @@ class LargeZipFile(Exception):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 # constants for Zip file compression methods
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 ZIP_STORED = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 ZIP_DEFLATED = 8
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23 +ZIP_BZIP2 = 12
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24 # Other ZIP compression methods not supported
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26 # Below are some formats and associated data for reading/writing headers using
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
27 @@ -483,6 +490,9 @@ def __init__(self, fileobj, mode, zi
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
29 if self._compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30 self._decompressor = zlib.decompressobj(-15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
31 + elif self._compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32 + self._decompressor = bz2.BZ2Decompressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
33 + self.MIN_READ_SIZE = 900000
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
34 self._unconsumed = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
35
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36 self._readbuffer = b''
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
37 @@ -641,6 +651,20 @@ def read1(self, n):
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38 self._update_crc(data, eof=eof)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 self._readbuffer = self._readbuffer[self._offset:] + data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41 + elif (len(self._unconsumed) > 0 and n > len_readbuffer and
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 + self._compress_type == ZIP_BZIP2):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
43 + try:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
44 + data = self._decompressor.decompress(self._unconsumed)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
45 + except EOFError:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46 + eof = self._compress_left
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 + data = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 + else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 + eof = False
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 + self._unconsumed = b''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 +
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 + self._update_crc(data, eof=eof)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
53 + self._readbuffer = self._readbuffer[self._offset:] + data
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 + self._offset = 0
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56 # Read from buffer.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 data = self._readbuffer[self._offset: self._offset + n]
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
58 @@ -663,7 +687,8 @@ class ZipFile:
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 file: Either the path to the file, or a file-like object.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 If it is a path, the file will be opened and closed by ZipFile.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 mode: The mode can be either read "r", write "w" or append "a".
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 - compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 + compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 + or ZIP_BZIP2 (requires bz2).
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65 allowZip64: if True ZipFile will create files with ZIP64 extensions when
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66 needed, otherwise it will raise an exception when this would
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67 be necessary.
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
68 @@ -683,6 +708,10 @@ def __init__(self, file, mode="r", c
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 if not zlib:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 "Compression requires the (missing) zlib module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
72 + elif compression == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
73 + if not bz2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
74 + raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 + "Compression requires the (missing) bz2 module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 raise RuntimeError("That compression method is not supported")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
79 @@ -1051,7 +1080,10 @@ def _writecheck(self, zinfo):
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
80 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
81 raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82 "Compression requires the (missing) zlib module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83 - if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
84 + if zinfo.compress_type == ZIP_BZIP2 and not bz2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
85 + raise RuntimeError(
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 + "Compression requires the (missing) bz2 module")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
87 + if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2):
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
88 raise RuntimeError("That compression method is not supported")
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 if zinfo.file_size > ZIP64_LIMIT:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 if not self._allowZip64:
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
91 @@ -1112,6 +1144,8 @@ def write(self, filename, arcname=No
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 if zinfo.compress_type == ZIP_DEFLATED:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94 zlib.DEFLATED, -15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
95 + elif zinfo.compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
96 + cmpr = bz2.BZ2Compressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
97 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
98 cmpr = None
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
99 while 1:
183
8196d2c0d6f8 Updated zipfiles for Python 3.2.1
Oleg Oshmyan <chortos@inbox.lv>
parents: 170
diff changeset
100 @@ -1175,6 +1209,10 @@ def writestr(self, zinfo_or_arcname,
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
101 zlib.DEFLATED, -15)
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
102 data = co.compress(data) + co.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
103 zinfo.compress_size = len(data) # Compressed size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
104 + elif zinfo.compress_type == ZIP_BZIP2:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
105 + co = bz2.BZ2Compressor()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
106 + data = co.compress(data) + co.flush()
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
107 + zinfo.compress_size = len(data) # Compressed size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
108 else:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
109 zinfo.compress_size = zinfo.file_size
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
110 zinfo.header_offset = self.fp.tell() # Start of header data