Mercurial > ~astiob > upreckon > hgweb
comparison zipfile320.diff @ 97:bbf9c434fa57
Added zipfile-with-bzip2 implementation for Python 3.2
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Wed, 02 Mar 2011 19:00:57 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
96:c3afa2b0c14c | 97:bbf9c434fa57 |
---|---|
1 --- zipfile32-original.py 2011-03-02 16:20:51.000000000 +0000 | |
2 +++ zipfile32.py 2011-03-02 18:38:37.000000000 +0000 | |
3 @@ -22,8 +22,14 @@ | |
4 zlib = None | |
5 crc32 = binascii.crc32 | |
6 | |
7 +try: | |
8 + import bz2 # We may need its compression method | |
9 +except ImportError: | |
10 + bz2 = None | |
11 + | |
12 __all__ = ["BadZipFile", "BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", | |
13 - "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"] | |
14 + "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", | |
15 + "ZIP_BZIP2"] | |
16 | |
17 class BadZipFile(Exception): | |
18 pass | |
19 @@ -45,6 +51,7 @@ class LargeZipFile(Exception): | |
20 # constants for Zip file compression methods | |
21 ZIP_STORED = 0 | |
22 ZIP_DEFLATED = 8 | |
23 +ZIP_BZIP2 = 12 | |
24 # Other ZIP compression methods not supported | |
25 | |
26 # Below are some formats and associated data for reading/writing headers using | |
27 @@ -485,6 +492,9 @@ def __init__(self, fileobj, mode, zi | |
28 | |
29 if self._compress_type == ZIP_DEFLATED: | |
30 self._decompressor = zlib.decompressobj(-15) | |
31 + elif self._compress_type == ZIP_BZIP2: | |
32 + self._decompressor = bz2.BZ2Decompressor() | |
33 + self.MIN_READ_SIZE = 900000 | |
34 self._unconsumed = b'' | |
35 | |
36 self._readbuffer = b'' | |
37 @@ -643,6 +653,20 @@ def read1(self, n): | |
38 self._update_crc(data, eof=eof) | |
39 self._readbuffer = self._readbuffer[self._offset:] + data | |
40 self._offset = 0 | |
41 + elif (len(self._unconsumed) > 0 and n > len_readbuffer and | |
42 + self._compress_type == ZIP_BZIP2): | |
43 + try: | |
44 + data = self._decompressor.decompress(self._unconsumed) | |
45 + except EOFError: | |
46 + eof = self._compress_left | |
47 + data = b'' | |
48 + else: | |
49 + eof = False | |
50 + self._unconsumed = b'' | |
51 + | |
52 + self._update_crc(data, eof=eof) | |
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 @@ -665,7 +689,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 @@ -685,6 +710,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 @@ -1053,7 +1082,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("That compression method is not supported") | |
89 if zinfo.file_size > ZIP64_LIMIT: | |
90 if not self._allowZip64: | |
91 @@ -1114,6 +1146,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 @@ -1177,6 +1211,10 @@ def writestr(self, zinfo_or_arcname, | |
101 zlib.DEFLATED, -15) | |
102 data = co.compress(data) + co.flush() | |
103 zinfo.compress_size = len(data) # Compressed size | |
104 + elif zinfo.compress_type == ZIP_BZIP2: | |
105 + co = bz2.BZ2Compressor() | |
106 + data = co.compress(data) + co.flush() | |
107 + zinfo.compress_size = len(data) # Compressed size | |
108 else: | |
109 zinfo.compress_size = zinfo.file_size | |
110 zinfo.header_offset = self.fp.tell() # Start of header data |