comparison zipfiles/zipfile272.diff @ 170:b993d9257400

Updated zipfiles
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 16 Jun 2011 01:24:10 +0100
parents zipfiles/zipfile271.diff@45d4a9dc707b
children
comparison
equal deleted inserted replaced
169:d7f4b051ad79 170:b993d9257400
1 --- /usr/local/lib/python2.7/zipfile.py 2011-06-15 13:20:07.000000000 +0100
2 +++ zipfile27.py 2011-06-03 20:20:40.000000000 +0100
3 @@ -1,6 +1,7 @@
4 """
5 Read and write ZIP files.
6 """
7 +# Improved by Chortos-2 in 2010 (added bzip2 support)
8 import struct, os, time, sys, shutil
9 import binascii, cStringIO, stat
10 import io
11 @@ -13,8 +14,13 @@
12 zlib = None
13 crc32 = binascii.crc32
14
15 +try:
16 + import bz2 # We may need its compression method
17 +except ImportError:
18 + bz2 = None
19 +
20 __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
21 - "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
22 + "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", "ZIP_BZIP2" ]
23
24 class BadZipfile(Exception):
25 pass
26 @@ -35,6 +41,7 @@ class LargeZipFile(Exception):
27 # constants for Zip file compression methods
28 ZIP_STORED = 0
29 ZIP_DEFLATED = 8
30 +ZIP_BZIP2 = 12
31 # Other ZIP compression methods not supported
32
33 # Below are some formats and associated data for reading/writing headers using
34 @@ -483,6 +490,9 @@ def __init__(self, fileobj, mode, zi
35
36 if self._compress_type == ZIP_DEFLATED:
37 self._decompressor = zlib.decompressobj(-15)
38 + elif self._compress_type == ZIP_BZIP2:
39 + self._decompressor = bz2.BZ2Decompressor()
40 + self.MIN_READ_SIZE = 900000
41 self._unconsumed = ''
42
43 self._readbuffer = ''
44 @@ -641,6 +651,13 @@ def read1(self, n):
45 self._update_crc(data, eof=eof)
46 self._readbuffer = self._readbuffer[self._offset:] + data
47 self._offset = 0
48 + elif (len(self._unconsumed) > 0 and n > len_readbuffer and
49 + self._compress_type == ZIP_BZIP2):
50 + data = self._decompressor.decompress(self._unconsumed)
51 +
52 + self._unconsumed = ''
53 + self._readbuffer = self._readbuffer[self._offset:] + data
54 + self._offset = 0
55
56 # Read from buffer.
57 data = self._readbuffer[self._offset: self._offset + n]
58 @@ -657,7 +674,8 @@ class ZipFile:
59 file: Either the path to the file, or a file-like object.
60 If it is a path, the file will be opened and closed by ZipFile.
61 mode: The mode can be either read "r", write "w" or append "a".
62 - compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
63 + compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
64 + or ZIP_BZIP2 (requires bz2).
65 allowZip64: if True ZipFile will create files with ZIP64 extensions when
66 needed, otherwise it will raise an exception when this would
67 be necessary.
68 @@ -677,6 +695,10 @@ def __init__(self, file, mode="r", c
69 if not zlib:
70 raise RuntimeError,\
71 "Compression requires the (missing) zlib module"
72 + elif compression == ZIP_BZIP2:
73 + if not bz2:
74 + raise RuntimeError,\
75 + "Compression requires the (missing) bz2 module"
76 else:
77 raise RuntimeError, "That compression method is not supported"
78
79 @@ -1011,7 +1033,10 @@ def _writecheck(self, zinfo):
80 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
81 raise RuntimeError, \
82 "Compression requires the (missing) zlib module"
83 - if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
84 + if zinfo.compress_type == ZIP_BZIP2 and not bz2:
85 + raise RuntimeError, \
86 + "Compression requires the (missing) bz2 module"
87 + if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2):
88 raise RuntimeError, \
89 "That compression method is not supported"
90 if zinfo.file_size > ZIP64_LIMIT:
91 @@ -1072,6 +1097,8 @@ def write(self, filename, arcname=No
92 if zinfo.compress_type == ZIP_DEFLATED:
93 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
94 zlib.DEFLATED, -15)
95 + elif zinfo.compress_type == ZIP_BZIP2:
96 + cmpr = bz2.BZ2Compressor()
97 else:
98 cmpr = None
99 while 1:
100 @@ -1132,6 +1159,10 @@ def writestr(self, zinfo_or_arcname,
101 zlib.DEFLATED, -15)
102 bytes = co.compress(bytes) + co.flush()
103 zinfo.compress_size = len(bytes) # Compressed size
104 + elif zinfo.compress_type == ZIP_BZIP2:
105 + co = bz2.BZ2Compressor()
106 + bytes = co.compress(bytes) + co.flush()
107 + zinfo.compress_size = len(bytes) # Compressed size
108 else:
109 zinfo.compress_size = zinfo.file_size
110 zinfo.header_offset = self.fp.tell() # Start of header bytes