annotate zipfiles/zipfile314.diff @ 213:ad4362bf9858 2.03 2.03.0

Created a branch for the 2.03 release
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 18 Aug 2011 22:37:22 +0300
parents b993d9257400
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
1 --- /usr/local/lib/python3.1/zipfile.py 2011-06-16 01:02:30.000000000 +0100
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
2 +++ zipfile31.py 2011-06-16 01:16:53.000000000 +0100
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3 @@ -3,6 +3,7 @@
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
5 XXX references to utf-8 need further investigation.
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
6 """
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7 +# Improved by Chortos-2 in 2010 (added bzip2 support)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 import struct, os, time, sys, shutil
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
9 import binascii, io, stat
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
10
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
11 @@ -13,8 +14,13 @@
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
12 zlib = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
13 crc32 = binascii.crc32
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
14
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
15 +try:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
16 + import bz2 # We may need its compression method
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
17 +except ImportError:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
18 + bz2 = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
19 +
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 - "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 + "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", "ZIP_BZIP2" ]
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24 class BadZipfile(Exception):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25 pass
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
26 @@ -35,6 +41,7 @@ class LargeZipFile(Exception):
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27 # constants for Zip file compression methods
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28 ZIP_STORED = 0
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
29 ZIP_DEFLATED = 8
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30 +ZIP_BZIP2 = 12
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
31 # Other ZIP compression methods not supported
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
33 # Below are some formats and associated data for reading/writing headers using
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
34 @@ -477,6 +484,9 @@ def __init__(self, fileobj, zipinfo,
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
35 self.compreadsize = 64*1024
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36 if self.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37 self.dc = zlib.decompressobj(-15)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38 + elif self.compress_type == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 + self.dc = bz2.BZ2Decompressor()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 + self.compreadsize = 900000
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 if hasattr(zipinfo, 'CRC'):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
43 self._expected_crc = zipinfo.CRC
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
44 @@ -604,7 +614,7 @@ def read(self, size = None):
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
45 if self.compress_type == ZIP_STORED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46 lr = len(self.readbuffer)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 bytesToRead = min(bytesToRead, size - lr)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 - elif self.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 + else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 if len(self.readbuffer) > size:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 # the user has requested fewer bytes than we've already
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 # pulled through the decompressor; don't read any more
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
53 @@ -639,14 +649,17 @@ def read(self, size = None):
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 newdata = bytes(map(self.decrypter, newdata))
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56 # decompress newly read data if necessary
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 - if newdata and self.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58 + if newdata and self.compress_type != ZIP_STORED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 newdata = self.dc.decompress(newdata)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 - self.rawbuffer = self.dc.unconsumed_tail
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 + self.rawbuffer = self.dc.unconsumed_tail if self.compress_type == ZIP_DEFLATED else ''
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 if self.eof and len(self.rawbuffer) == 0:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 # we're out of raw bytes (both from the file and
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 # the local buffer); flush just to make sure the
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65 # decompressor is done
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66 - newdata += self.dc.flush()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67 + try:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68 + newdata += self.dc.flush()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 + except AttributeError:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 + pass
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 # prevent decompressor from being used again
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
72 self.dc = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
73
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
74 @@ -674,7 +687,8 @@ class ZipFile:
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 file: Either the path to the file, or a file-like object.
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 If it is a path, the file will be opened and closed by ZipFile.
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 mode: The mode can be either read "r", write "w" or append "a".
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78 - compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79 + compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
80 + or ZIP_BZIP2 (requires bz2).
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
81 allowZip64: if True ZipFile will create files with ZIP64 extensions when
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82 needed, otherwise it will raise an exception when this would
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83 be necessary.
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
84 @@ -694,6 +708,10 @@ def __init__(self, file, mode="r", c
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
85 if not zlib:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 raise RuntimeError(
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
87 "Compression requires the (missing) zlib module")
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
88 + elif compression == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 + if not bz2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 + raise RuntimeError(
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
91 + "Compression requires the (missing) bz2 module")
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 raise RuntimeError("That compression method is not supported")
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
95 @@ -1053,7 +1071,10 @@ def _writecheck(self, zinfo):
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
96 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
97 raise RuntimeError(
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
98 "Compression requires the (missing) zlib module")
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
99 - if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
100 + if zinfo.compress_type == ZIP_BZIP2 and not bz2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
101 + raise RuntimeError(
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
102 + "Compression requires the (missing) bz2 module")
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
103 + if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
104 raise RuntimeError("That compression method is not supported")
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
105 if zinfo.file_size > ZIP64_LIMIT:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
106 if not self._allowZip64:
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
107 @@ -1114,6 +1135,8 @@ def write(self, filename, arcname=No
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
108 if zinfo.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
109 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
110 zlib.DEFLATED, -15)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
111 + elif zinfo.compress_type == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
112 + cmpr = bz2.BZ2Compressor()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
113 else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
114 cmpr = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
115 while 1:
170
b993d9257400 Updated zipfiles
Oleg Oshmyan <chortos@inbox.lv>
parents: 153
diff changeset
116 @@ -1174,6 +1197,10 @@ def writestr(self, zinfo_or_arcname,
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
117 zlib.DEFLATED, -15)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
118 data = co.compress(data) + co.flush()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 zinfo.compress_size = len(data) # Compressed size
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 + elif zinfo.compress_type == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 + co = bz2.BZ2Compressor()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 + data = co.compress(data) + co.flush()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
123 + zinfo.compress_size = len(data) # Compressed size
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
124 else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 zinfo.compress_size = zinfo.file_size
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126 zinfo.header_offset = self.fp.tell() # Start of header data