annotate zipfile320.diff @ 98:62a96d51bf94

Fixed ZipArchive.extract with relative paths on Windows
author Oleg Oshmyan <chortos@inbox.lv>
date Wed, 02 Mar 2011 19:52:03 +0000
parents bbf9c434fa57
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
97
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1 --- zipfile32-original.py 2011-03-02 16:20:51.000000000 +0000
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2 +++ zipfile32.py 2011-03-02 18:38:37.000000000 +0000
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
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27 @@ -485,6 +492,9 @@ def __init__(self, fileobj, mode, zi
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''
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37 @@ -643,6 +653,20 @@ def read1(self, n):
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]
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58 @@ -665,7 +689,8 @@ class ZipFile:
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.
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68 @@ -685,6 +710,10 @@ def __init__(self, file, mode="r", c
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
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79 @@ -1053,7 +1082,10 @@ def _writecheck(self, zinfo):
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:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
91 @@ -1114,6 +1146,8 @@ def write(self, filename, arcname=No
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:
bbf9c434fa57 Added zipfile-with-bzip2 implementation for Python 3.2
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
100 @@ -1177,6 +1211,10 @@ def writestr(self, zinfo_or_arcname,
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