Mercurial > ~astiob > upreckon > hgweb
changeset 183:8196d2c0d6f8
Updated zipfiles for Python 3.2.1
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Thu, 14 Jul 2011 18:52:18 +0300 |
parents | 760d38ee86d6 |
children | 4df9537ed261 |
files | zipfiles/zipfile32.py zipfiles/zipfile320.diff zipfiles/zipfile321.diff |
diffstat | 3 files changed, 117 insertions(+), 119 deletions(-) [+] |
line wrap: on
line diff
--- a/zipfiles/zipfile32.py Wed Jun 29 01:26:33 2011 +0300 +++ b/zipfiles/zipfile32.py Thu Jul 14 18:52:18 2011 +0300 @@ -253,16 +253,14 @@ # found the magic number; attempt to unpack and interpret recData = data[start:start+sizeEndCentDir] endrec = list(struct.unpack(structEndArchive, recData)) - comment = data[start+sizeEndCentDir:] - # check that comment length is correct - if endrec[_ECD_COMMENT_SIZE] == len(comment): - # Append the archive comment and start offset - endrec.append(comment) - endrec.append(maxCommentStart + start) + commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file + comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize] + endrec.append(comment) + endrec.append(maxCommentStart + start) - # Try to read the "Zip64 end of central directory" structure - return _EndRecData64(fpin, maxCommentStart + start - filesize, - endrec) + # Try to read the "Zip64 end of central directory" structure + return _EndRecData64(fpin, maxCommentStart + start - filesize, + endrec) # Unable to find a valid end of central directory structure return
--- a/zipfiles/zipfile320.diff Wed Jun 29 01:26:33 2011 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,110 +0,0 @@ ---- /usr/local/lib/python3.2/zipfile.py 2011-03-28 14:47:09.000000000 +0100 -+++ zipfile32.py 2011-06-03 20:20:40.000000000 +0100 -@@ -22,8 +22,14 @@ - zlib = None - crc32 = binascii.crc32 - -+try: -+ import bz2 # We may need its compression method -+except ImportError: -+ bz2 = None -+ - __all__ = ["BadZipFile", "BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", -- "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"] -+ "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", -+ "ZIP_BZIP2"] - - class BadZipFile(Exception): - pass -@@ -45,6 +51,7 @@ class LargeZipFile(Exception): - # constants for Zip file compression methods - ZIP_STORED = 0 - ZIP_DEFLATED = 8 -+ZIP_BZIP2 = 12 - # Other ZIP compression methods not supported - - # Below are some formats and associated data for reading/writing headers using -@@ -485,6 +492,9 @@ def __init__(self, fileobj, mode, zi - - if self._compress_type == ZIP_DEFLATED: - self._decompressor = zlib.decompressobj(-15) -+ elif self._compress_type == ZIP_BZIP2: -+ self._decompressor = bz2.BZ2Decompressor() -+ self.MIN_READ_SIZE = 900000 - self._unconsumed = b'' - - self._readbuffer = b'' -@@ -643,6 +653,20 @@ def read1(self, n): - self._update_crc(data, eof=eof) - self._readbuffer = self._readbuffer[self._offset:] + data - self._offset = 0 -+ elif (len(self._unconsumed) > 0 and n > len_readbuffer and -+ self._compress_type == ZIP_BZIP2): -+ try: -+ data = self._decompressor.decompress(self._unconsumed) -+ except EOFError: -+ eof = self._compress_left -+ data = b'' -+ else: -+ eof = False -+ self._unconsumed = b'' -+ -+ self._update_crc(data, eof=eof) -+ self._readbuffer = self._readbuffer[self._offset:] + data -+ self._offset = 0 - - # Read from buffer. - data = self._readbuffer[self._offset: self._offset + n] -@@ -665,7 +689,8 @@ class ZipFile: - file: Either the path to the file, or a file-like object. - If it is a path, the file will be opened and closed by ZipFile. - mode: The mode can be either read "r", write "w" or append "a". -- compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib). -+ compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib) -+ or ZIP_BZIP2 (requires bz2). - allowZip64: if True ZipFile will create files with ZIP64 extensions when - needed, otherwise it will raise an exception when this would - be necessary. -@@ -685,6 +710,10 @@ def __init__(self, file, mode="r", c - if not zlib: - raise RuntimeError( - "Compression requires the (missing) zlib module") -+ elif compression == ZIP_BZIP2: -+ if not bz2: -+ raise RuntimeError( -+ "Compression requires the (missing) bz2 module") - else: - raise RuntimeError("That compression method is not supported") - -@@ -1053,7 +1082,10 @@ def _writecheck(self, zinfo): - if zinfo.compress_type == ZIP_DEFLATED and not zlib: - raise RuntimeError( - "Compression requires the (missing) zlib module") -- if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED): -+ if zinfo.compress_type == ZIP_BZIP2 and not bz2: -+ raise RuntimeError( -+ "Compression requires the (missing) bz2 module") -+ if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2): - raise RuntimeError("That compression method is not supported") - if zinfo.file_size > ZIP64_LIMIT: - if not self._allowZip64: -@@ -1114,6 +1146,8 @@ def write(self, filename, arcname=No - if zinfo.compress_type == ZIP_DEFLATED: - cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, - zlib.DEFLATED, -15) -+ elif zinfo.compress_type == ZIP_BZIP2: -+ cmpr = bz2.BZ2Compressor() - else: - cmpr = None - while 1: -@@ -1177,6 +1211,10 @@ def writestr(self, zinfo_or_arcname, - zlib.DEFLATED, -15) - data = co.compress(data) + co.flush() - zinfo.compress_size = len(data) # Compressed size -+ elif zinfo.compress_type == ZIP_BZIP2: -+ co = bz2.BZ2Compressor() -+ data = co.compress(data) + co.flush() -+ zinfo.compress_size = len(data) # Compressed size - else: - zinfo.compress_size = zinfo.file_size - zinfo.header_offset = self.fp.tell() # Start of header data
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/zipfiles/zipfile321.diff Thu Jul 14 18:52:18 2011 +0300 @@ -0,0 +1,110 @@ +--- /usr/local/lib/python3.2/zipfile.py 2011-07-14 16:08:50.000000000 +0300 ++++ zipfile32.py 2011-07-14 18:31:32.000000000 +0300 +@@ -22,8 +22,14 @@ + zlib = None + crc32 = binascii.crc32 + ++try: ++ import bz2 # We may need its compression method ++except ImportError: ++ bz2 = None ++ + __all__ = ["BadZipFile", "BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", +- "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"] ++ "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", ++ "ZIP_BZIP2"] + + class BadZipFile(Exception): + pass +@@ -45,6 +51,7 @@ class LargeZipFile(Exception): + # constants for Zip file compression methods + ZIP_STORED = 0 + ZIP_DEFLATED = 8 ++ZIP_BZIP2 = 12 + # Other ZIP compression methods not supported + + # Below are some formats and associated data for reading/writing headers using +@@ -483,6 +490,9 @@ def __init__(self, fileobj, mode, zi + + if self._compress_type == ZIP_DEFLATED: + self._decompressor = zlib.decompressobj(-15) ++ elif self._compress_type == ZIP_BZIP2: ++ self._decompressor = bz2.BZ2Decompressor() ++ self.MIN_READ_SIZE = 900000 + self._unconsumed = b'' + + self._readbuffer = b'' +@@ -641,6 +651,20 @@ def read1(self, n): + self._update_crc(data, eof=eof) + self._readbuffer = self._readbuffer[self._offset:] + data + self._offset = 0 ++ elif (len(self._unconsumed) > 0 and n > len_readbuffer and ++ self._compress_type == ZIP_BZIP2): ++ try: ++ data = self._decompressor.decompress(self._unconsumed) ++ except EOFError: ++ eof = self._compress_left ++ data = b'' ++ else: ++ eof = False ++ self._unconsumed = b'' ++ ++ self._update_crc(data, eof=eof) ++ self._readbuffer = self._readbuffer[self._offset:] + data ++ self._offset = 0 + + # Read from buffer. + data = self._readbuffer[self._offset: self._offset + n] +@@ -663,7 +687,8 @@ class ZipFile: + file: Either the path to the file, or a file-like object. + If it is a path, the file will be opened and closed by ZipFile. + mode: The mode can be either read "r", write "w" or append "a". +- compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib). ++ compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib) ++ or ZIP_BZIP2 (requires bz2). + allowZip64: if True ZipFile will create files with ZIP64 extensions when + needed, otherwise it will raise an exception when this would + be necessary. +@@ -683,6 +708,10 @@ def __init__(self, file, mode="r", c + if not zlib: + raise RuntimeError( + "Compression requires the (missing) zlib module") ++ elif compression == ZIP_BZIP2: ++ if not bz2: ++ raise RuntimeError( ++ "Compression requires the (missing) bz2 module") + else: + raise RuntimeError("That compression method is not supported") + +@@ -1051,7 +1080,10 @@ def _writecheck(self, zinfo): + if zinfo.compress_type == ZIP_DEFLATED and not zlib: + raise RuntimeError( + "Compression requires the (missing) zlib module") +- if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED): ++ if zinfo.compress_type == ZIP_BZIP2 and not bz2: ++ raise RuntimeError( ++ "Compression requires the (missing) bz2 module") ++ if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2): + raise RuntimeError("That compression method is not supported") + if zinfo.file_size > ZIP64_LIMIT: + if not self._allowZip64: +@@ -1112,6 +1144,8 @@ def write(self, filename, arcname=No + if zinfo.compress_type == ZIP_DEFLATED: + cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, + zlib.DEFLATED, -15) ++ elif zinfo.compress_type == ZIP_BZIP2: ++ cmpr = bz2.BZ2Compressor() + else: + cmpr = None + while 1: +@@ -1175,6 +1209,10 @@ def writestr(self, zinfo_or_arcname, + zlib.DEFLATED, -15) + data = co.compress(data) + co.flush() + zinfo.compress_size = len(data) # Compressed size ++ elif zinfo.compress_type == ZIP_BZIP2: ++ co = bz2.BZ2Compressor() ++ data = co.compress(data) + co.flush() ++ zinfo.compress_size = len(data) # Compressed size + else: + zinfo.compress_size = zinfo.file_size + zinfo.header_offset = self.fp.tell() # Start of header data