Mercurial > ~astiob > upreckon > hgweb
comparison 2.00/zipfile271.diff @ 29:a8cc383b787c
Clean up zipfiles and diff them to stock ones
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Wed, 24 Nov 2010 23:21:31 +0000 |
parents | |
children | 3000bb94addb |
comparison
equal
deleted
inserted
replaced
28:3d535503161f | 29:a8cc383b787c |
---|---|
1 --- ../../zipfile.py 2010-11-24 01:47:05.000000000 +0000 | |
2 +++ zipfile271.py 2010-11-24 01:51:31.000000000 +0000 | |
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 @@ | |
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 @@ -477,6 +484,9 @@ | |
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 @@ -635,6 +645,13 @@ | |
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 @@ -651,7 +668,8 @@ | |
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 @@ -671,6 +689,10 @@ | |
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 @@ -994,7 +1016,10 @@ | |
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 @@ -1055,6 +1080,8 @@ | |
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 @@ -1115,6 +1142,10 @@ | |
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 |