From fe3701da94c5526d2a167c6451e91a1eadc90b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrer=20=C3=96zen?= Date: Mon, 10 Jul 2006 15:11:12 +0000 Subject: [PATCH] =?UTF-8?q?bellek=20sorunu=20i=C3=A7in=20quick&dirty=20?= =?UTF-8?q?=C3=A7=C3=B6z=C3=BCm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pisi/archive.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/pisi/archive.py b/pisi/archive.py index 58496d73..8b636730 100644 --- a/pisi/archive.py +++ b/pisi/archive.py @@ -158,6 +158,82 @@ class ArchiveTar(ArchiveBase): if ret != 0: raise LZMAError(err) + +class MyZipFile(zipfile.ZipFile): + def decompressToFile(self, name, outname): + import zlib + import binascii + + block_size = 1024 * 1024 * 2 + + if self.mode not in ("r", "a"): + raise RuntimeError, 'read() requires mode "r" or "a"' + if not self.fp: + raise RuntimeError, \ + "Attempt to read ZIP archive that was already closed" + zinfo = self.getinfo(name) + filepos = self.fp.tell() + self.fp.seek(zinfo.file_offset, 0) + + destfile = file(outname, 'wb') + + if zinfo.compress_type == zipfile.ZIP_STORED: + total_read = 0 + crc = None + while total_read < zinfo.compress_size: + buff = self.fp.read(block_size) + destfile.write(buff) + total_read += (block_size) + if crc: + crc = binascii.crc32(buff, crc) + else: + crc = binascii.crc32(buff) + destfile.close() + self.fp.seek(filepos, 0) + if crc != zinfo.CRC: + raise zipfile.BadZipfile, "Bad CRC-32 for file %s" % name + return + elif zinfo.compress_type != zipfile.ZIP_DEFLATED: + raise BadZipfile, \ + "Unsupported compression method %d for file %s" % \ + (zinfo.compress_type, name) + + if not zlib: + raise RuntimeError, \ + "De-compression requires the (missing) zlib module" + # zlib compress/decompress code by Jeremy Hylton of CNRI + dc = zlib.decompressobj(-15) + + total_read = 0 + crc = None + while total_read < zinfo.compress_size: + if name == "install.tar.lzma": + print "blalalall", zinfo.compress_size + buff = self.fp.read(block_size) + dcbuff = dc.decompress(dc.unconsumed_tail + buff) + destfile.write(dcbuff) + total_read += block_size + if crc: + crc = binascii.crc32(dcbuff, crc) + else: + crc = binascii.crc32(dcbuff) + + # need to feed in unused pad byte so that zlib won't choke + ex = dc.decompress(dc.unconsumed_tail + 'Z') + dc.flush() + if ex: + if crc: + crc = binascii.crc32(ex, crc) + else: + crc = binascii.crc32(ex) + destfile.write(ex) + + if crc != zinfo.CRC: + raise zipfile.BadZipfile, "Bad CRC-32 for file %s" % name + + destfile.close() + self.fp.seek(filepos, 0) + + class ArchiveZip(ArchiveBase): """ArchiveZip handles zip archives. @@ -170,7 +246,7 @@ class ArchiveZip(ArchiveBase): def __init__(self, file_path, arch_type = "zip", mode = 'r'): super(ArchiveZip, self).__init__(file_path, arch_type) - self.zip_obj = zipfile.ZipFile(self.file_path, mode) + self.zip_obj = MyZipFile(self.file_path, mode) def close(self): """Close the zip archive.""" @@ -274,10 +350,7 @@ class ArchiveZip(ArchiveBase): perm &= 0x08FF0000 perm >>= 16 perm |= 0x00000100 - buff = open (ofile, 'wb') - file_content = zip_obj.read(info.filename) - buff.write(file_content) - buff.close() + zip_obj.decompressToFile(info.filename, ofile) os.chmod(ofile, perm) def unpack_files(self, paths, target_dir):