]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Lib/zipfile.py
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 4/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Lib / zipfile.py
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Lib/zipfile.py b/AppPkg/Applications/Python/Python-2.7.10/Lib/zipfile.py
new file mode 100644 (file)
index 0000000..96010ec
--- /dev/null
@@ -0,0 +1,1539 @@
+"""\r
+Read and write ZIP files.\r
+"""\r
+import struct, os, time, sys, shutil\r
+import binascii, cStringIO, stat\r
+import io\r
+import re\r
+import string\r
+\r
+try:\r
+    import zlib # We may need its compression method\r
+    crc32 = zlib.crc32\r
+except ImportError:\r
+    zlib = None\r
+    crc32 = binascii.crc32\r
+\r
+__all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",\r
+           "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]\r
+\r
+class BadZipfile(Exception):\r
+    pass\r
+\r
+\r
+class LargeZipFile(Exception):\r
+    """\r
+    Raised when writing a zipfile, the zipfile requires ZIP64 extensions\r
+    and those extensions are disabled.\r
+    """\r
+\r
+error = BadZipfile      # The exception raised by this module\r
+\r
+ZIP64_LIMIT = (1 << 31) - 1\r
+ZIP_FILECOUNT_LIMIT = (1 << 16) - 1\r
+ZIP_MAX_COMMENT = (1 << 16) - 1\r
+\r
+# constants for Zip file compression methods\r
+ZIP_STORED = 0\r
+ZIP_DEFLATED = 8\r
+# Other ZIP compression methods not supported\r
+\r
+# Below are some formats and associated data for reading/writing headers using\r
+# the struct module.  The names and structures of headers/records are those used\r
+# in the PKWARE description of the ZIP file format:\r
+#     http://www.pkware.com/documents/casestudies/APPNOTE.TXT\r
+# (URL valid as of January 2008)\r
+\r
+# The "end of central directory" structure, magic number, size, and indices\r
+# (section V.I in the format document)\r
+structEndArchive = "<4s4H2LH"\r
+stringEndArchive = "PK\005\006"\r
+sizeEndCentDir = struct.calcsize(structEndArchive)\r
+\r
+_ECD_SIGNATURE = 0\r
+_ECD_DISK_NUMBER = 1\r
+_ECD_DISK_START = 2\r
+_ECD_ENTRIES_THIS_DISK = 3\r
+_ECD_ENTRIES_TOTAL = 4\r
+_ECD_SIZE = 5\r
+_ECD_OFFSET = 6\r
+_ECD_COMMENT_SIZE = 7\r
+# These last two indices are not part of the structure as defined in the\r
+# spec, but they are used internally by this module as a convenience\r
+_ECD_COMMENT = 8\r
+_ECD_LOCATION = 9\r
+\r
+# The "central directory" structure, magic number, size, and indices\r
+# of entries in the structure (section V.F in the format document)\r
+structCentralDir = "<4s4B4HL2L5H2L"\r
+stringCentralDir = "PK\001\002"\r
+sizeCentralDir = struct.calcsize(structCentralDir)\r
+\r
+# indexes of entries in the central directory structure\r
+_CD_SIGNATURE = 0\r
+_CD_CREATE_VERSION = 1\r
+_CD_CREATE_SYSTEM = 2\r
+_CD_EXTRACT_VERSION = 3\r
+_CD_EXTRACT_SYSTEM = 4\r
+_CD_FLAG_BITS = 5\r
+_CD_COMPRESS_TYPE = 6\r
+_CD_TIME = 7\r
+_CD_DATE = 8\r
+_CD_CRC = 9\r
+_CD_COMPRESSED_SIZE = 10\r
+_CD_UNCOMPRESSED_SIZE = 11\r
+_CD_FILENAME_LENGTH = 12\r
+_CD_EXTRA_FIELD_LENGTH = 13\r
+_CD_COMMENT_LENGTH = 14\r
+_CD_DISK_NUMBER_START = 15\r
+_CD_INTERNAL_FILE_ATTRIBUTES = 16\r
+_CD_EXTERNAL_FILE_ATTRIBUTES = 17\r
+_CD_LOCAL_HEADER_OFFSET = 18\r
+\r
+# The "local file header" structure, magic number, size, and indices\r
+# (section V.A in the format document)\r
+structFileHeader = "<4s2B4HL2L2H"\r
+stringFileHeader = "PK\003\004"\r
+sizeFileHeader = struct.calcsize(structFileHeader)\r
+\r
+_FH_SIGNATURE = 0\r
+_FH_EXTRACT_VERSION = 1\r
+_FH_EXTRACT_SYSTEM = 2\r
+_FH_GENERAL_PURPOSE_FLAG_BITS = 3\r
+_FH_COMPRESSION_METHOD = 4\r
+_FH_LAST_MOD_TIME = 5\r
+_FH_LAST_MOD_DATE = 6\r
+_FH_CRC = 7\r
+_FH_COMPRESSED_SIZE = 8\r
+_FH_UNCOMPRESSED_SIZE = 9\r
+_FH_FILENAME_LENGTH = 10\r
+_FH_EXTRA_FIELD_LENGTH = 11\r
+\r
+# The "Zip64 end of central directory locator" structure, magic number, and size\r
+structEndArchive64Locator = "<4sLQL"\r
+stringEndArchive64Locator = "PK\x06\x07"\r
+sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator)\r
+\r
+# The "Zip64 end of central directory" record, magic number, size, and indices\r
+# (section V.G in the format document)\r
+structEndArchive64 = "<4sQ2H2L4Q"\r
+stringEndArchive64 = "PK\x06\x06"\r
+sizeEndCentDir64 = struct.calcsize(structEndArchive64)\r
+\r
+_CD64_SIGNATURE = 0\r
+_CD64_DIRECTORY_RECSIZE = 1\r
+_CD64_CREATE_VERSION = 2\r
+_CD64_EXTRACT_VERSION = 3\r
+_CD64_DISK_NUMBER = 4\r
+_CD64_DISK_NUMBER_START = 5\r
+_CD64_NUMBER_ENTRIES_THIS_DISK = 6\r
+_CD64_NUMBER_ENTRIES_TOTAL = 7\r
+_CD64_DIRECTORY_SIZE = 8\r
+_CD64_OFFSET_START_CENTDIR = 9\r
+\r
+def _check_zipfile(fp):\r
+    try:\r
+        if _EndRecData(fp):\r
+            return True         # file has correct magic number\r
+    except IOError:\r
+        pass\r
+    return False\r
+\r
+def is_zipfile(filename):\r
+    """Quickly see if a file is a ZIP file by checking the magic number.\r
+\r
+    The filename argument may be a file or file-like object too.\r
+    """\r
+    result = False\r
+    try:\r
+        if hasattr(filename, "read"):\r
+            result = _check_zipfile(fp=filename)\r
+        else:\r
+            with open(filename, "rb") as fp:\r
+                result = _check_zipfile(fp)\r
+    except IOError:\r
+        pass\r
+    return result\r
+\r
+def _EndRecData64(fpin, offset, endrec):\r
+    """\r
+    Read the ZIP64 end-of-archive records and use that to update endrec\r
+    """\r
+    try:\r
+        fpin.seek(offset - sizeEndCentDir64Locator, 2)\r
+    except IOError:\r
+        # If the seek fails, the file is not large enough to contain a ZIP64\r
+        # end-of-archive record, so just return the end record we were given.\r
+        return endrec\r
+\r
+    data = fpin.read(sizeEndCentDir64Locator)\r
+    if len(data) != sizeEndCentDir64Locator:\r
+        return endrec\r
+    sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)\r
+    if sig != stringEndArchive64Locator:\r
+        return endrec\r
+\r
+    if diskno != 0 or disks != 1:\r
+        raise BadZipfile("zipfiles that span multiple disks are not supported")\r
+\r
+    # Assume no 'zip64 extensible data'\r
+    fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)\r
+    data = fpin.read(sizeEndCentDir64)\r
+    if len(data) != sizeEndCentDir64:\r
+        return endrec\r
+    sig, sz, create_version, read_version, disk_num, disk_dir, \\r
+            dircount, dircount2, dirsize, diroffset = \\r
+            struct.unpack(structEndArchive64, data)\r
+    if sig != stringEndArchive64:\r
+        return endrec\r
+\r
+    # Update the original endrec using data from the ZIP64 record\r
+    endrec[_ECD_SIGNATURE] = sig\r
+    endrec[_ECD_DISK_NUMBER] = disk_num\r
+    endrec[_ECD_DISK_START] = disk_dir\r
+    endrec[_ECD_ENTRIES_THIS_DISK] = dircount\r
+    endrec[_ECD_ENTRIES_TOTAL] = dircount2\r
+    endrec[_ECD_SIZE] = dirsize\r
+    endrec[_ECD_OFFSET] = diroffset\r
+    return endrec\r
+\r
+\r
+def _EndRecData(fpin):\r
+    """Return data from the "End of Central Directory" record, or None.\r
+\r
+    The data is a list of the nine items in the ZIP "End of central dir"\r
+    record followed by a tenth item, the file seek offset of this record."""\r
+\r
+    # Determine file size\r
+    fpin.seek(0, 2)\r
+    filesize = fpin.tell()\r
+\r
+    # Check to see if this is ZIP file with no archive comment (the\r
+    # "end of central directory" structure should be the last item in the\r
+    # file if this is the case).\r
+    try:\r
+        fpin.seek(-sizeEndCentDir, 2)\r
+    except IOError:\r
+        return None\r
+    data = fpin.read()\r
+    if (len(data) == sizeEndCentDir and\r
+        data[0:4] == stringEndArchive and\r
+        data[-2:] == b"\000\000"):\r
+        # the signature is correct and there's no comment, unpack structure\r
+        endrec = struct.unpack(structEndArchive, data)\r
+        endrec=list(endrec)\r
+\r
+        # Append a blank comment and record start offset\r
+        endrec.append("")\r
+        endrec.append(filesize - sizeEndCentDir)\r
+\r
+        # Try to read the "Zip64 end of central directory" structure\r
+        return _EndRecData64(fpin, -sizeEndCentDir, endrec)\r
+\r
+    # Either this is not a ZIP file, or it is a ZIP file with an archive\r
+    # comment.  Search the end of the file for the "end of central directory"\r
+    # record signature. The comment is the last item in the ZIP file and may be\r
+    # up to 64K long.  It is assumed that the "end of central directory" magic\r
+    # number does not appear in the comment.\r
+    maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0)\r
+    fpin.seek(maxCommentStart, 0)\r
+    data = fpin.read()\r
+    start = data.rfind(stringEndArchive)\r
+    if start >= 0:\r
+        # found the magic number; attempt to unpack and interpret\r
+        recData = data[start:start+sizeEndCentDir]\r
+        if len(recData) != sizeEndCentDir:\r
+            # Zip file is corrupted.\r
+            return None\r
+        endrec = list(struct.unpack(structEndArchive, recData))\r
+        commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file\r
+        comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]\r
+        endrec.append(comment)\r
+        endrec.append(maxCommentStart + start)\r
+\r
+        # Try to read the "Zip64 end of central directory" structure\r
+        return _EndRecData64(fpin, maxCommentStart + start - filesize,\r
+                             endrec)\r
+\r
+    # Unable to find a valid end of central directory structure\r
+    return None\r
+\r
+\r
+class ZipInfo (object):\r
+    """Class with attributes describing each file in the ZIP archive."""\r
+\r
+    __slots__ = (\r
+            'orig_filename',\r
+            'filename',\r
+            'date_time',\r
+            'compress_type',\r
+            'comment',\r
+            'extra',\r
+            'create_system',\r
+            'create_version',\r
+            'extract_version',\r
+            'reserved',\r
+            'flag_bits',\r
+            'volume',\r
+            'internal_attr',\r
+            'external_attr',\r
+            'header_offset',\r
+            'CRC',\r
+            'compress_size',\r
+            'file_size',\r
+            '_raw_time',\r
+        )\r
+\r
+    def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):\r
+        self.orig_filename = filename   # Original file name in archive\r
+\r
+        # Terminate the file name at the first null byte.  Null bytes in file\r
+        # names are used as tricks by viruses in archives.\r
+        null_byte = filename.find(chr(0))\r
+        if null_byte >= 0:\r
+            filename = filename[0:null_byte]\r
+        # This is used to ensure paths in generated ZIP files always use\r
+        # forward slashes as the directory separator, as required by the\r
+        # ZIP format specification.\r
+        if os.sep != "/" and os.sep in filename:\r
+            filename = filename.replace(os.sep, "/")\r
+\r
+        self.filename = filename        # Normalized file name\r
+        self.date_time = date_time      # year, month, day, hour, min, sec\r
+\r
+        if date_time[0] < 1980:\r
+            raise ValueError('ZIP does not support timestamps before 1980')\r
+\r
+        # Standard values:\r
+        self.compress_type = ZIP_STORED # Type of compression for the file\r
+        self.comment = ""               # Comment for each file\r
+        self.extra = ""                 # ZIP extra data\r
+        if sys.platform == 'win32':\r
+            self.create_system = 0          # System which created ZIP archive\r
+        else:\r
+            # Assume everything else is unix-y\r
+            self.create_system = 3          # System which created ZIP archive\r
+        self.create_version = 20        # Version which created ZIP archive\r
+        self.extract_version = 20       # Version needed to extract archive\r
+        self.reserved = 0               # Must be zero\r
+        self.flag_bits = 0              # ZIP flag bits\r
+        self.volume = 0                 # Volume number of file header\r
+        self.internal_attr = 0          # Internal attributes\r
+        self.external_attr = 0          # External file attributes\r
+        # Other attributes are set by class ZipFile:\r
+        # header_offset         Byte offset to the file header\r
+        # CRC                   CRC-32 of the uncompressed file\r
+        # compress_size         Size of the compressed file\r
+        # file_size             Size of the uncompressed file\r
+\r
+    def FileHeader(self, zip64=None):\r
+        """Return the per-file header as a string."""\r
+        dt = self.date_time\r
+        dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]\r
+        dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)\r
+        if self.flag_bits & 0x08:\r
+            # Set these to zero because we write them after the file data\r
+            CRC = compress_size = file_size = 0\r
+        else:\r
+            CRC = self.CRC\r
+            compress_size = self.compress_size\r
+            file_size = self.file_size\r
+\r
+        extra = self.extra\r
+\r
+        if zip64 is None:\r
+            zip64 = file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT\r
+        if zip64:\r
+            fmt = '<HHQQ'\r
+            extra = extra + struct.pack(fmt,\r
+                    1, struct.calcsize(fmt)-4, file_size, compress_size)\r
+        if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT:\r
+            if not zip64:\r
+                raise LargeZipFile("Filesize would require ZIP64 extensions")\r
+            # File is larger than what fits into a 4 byte integer,\r
+            # fall back to the ZIP64 extension\r
+            file_size = 0xffffffff\r
+            compress_size = 0xffffffff\r
+            self.extract_version = max(45, self.extract_version)\r
+            self.create_version = max(45, self.extract_version)\r
+\r
+        filename, flag_bits = self._encodeFilenameFlags()\r
+        header = struct.pack(structFileHeader, stringFileHeader,\r
+                 self.extract_version, self.reserved, flag_bits,\r
+                 self.compress_type, dostime, dosdate, CRC,\r
+                 compress_size, file_size,\r
+                 len(filename), len(extra))\r
+        return header + filename + extra\r
+\r
+    def _encodeFilenameFlags(self):\r
+        if isinstance(self.filename, unicode):\r
+            try:\r
+                return self.filename.encode('ascii'), self.flag_bits\r
+            except UnicodeEncodeError:\r
+                return self.filename.encode('utf-8'), self.flag_bits | 0x800\r
+        else:\r
+            return self.filename, self.flag_bits\r
+\r
+    def _decodeFilename(self):\r
+        if self.flag_bits & 0x800:\r
+            return self.filename.decode('utf-8')\r
+        else:\r
+            return self.filename\r
+\r
+    def _decodeExtra(self):\r
+        # Try to decode the extra field.\r
+        extra = self.extra\r
+        unpack = struct.unpack\r
+        while len(extra) >= 4:\r
+            tp, ln = unpack('<HH', extra[:4])\r
+            if tp == 1:\r
+                if ln >= 24:\r
+                    counts = unpack('<QQQ', extra[4:28])\r
+                elif ln == 16:\r
+                    counts = unpack('<QQ', extra[4:20])\r
+                elif ln == 8:\r
+                    counts = unpack('<Q', extra[4:12])\r
+                elif ln == 0:\r
+                    counts = ()\r
+                else:\r
+                    raise RuntimeError, "Corrupt extra field %s"%(ln,)\r
+\r
+                idx = 0\r
+\r
+                # ZIP64 extension (large files and/or large archives)\r
+                if self.file_size in (0xffffffffffffffffL, 0xffffffffL):\r
+                    self.file_size = counts[idx]\r
+                    idx += 1\r
+\r
+                if self.compress_size == 0xFFFFFFFFL:\r
+                    self.compress_size = counts[idx]\r
+                    idx += 1\r
+\r
+                if self.header_offset == 0xffffffffL:\r
+                    old = self.header_offset\r
+                    self.header_offset = counts[idx]\r
+                    idx+=1\r
+\r
+            extra = extra[ln+4:]\r
+\r
+\r
+class _ZipDecrypter:\r
+    """Class to handle decryption of files stored within a ZIP archive.\r
+\r
+    ZIP supports a password-based form of encryption. Even though known\r
+    plaintext attacks have been found against it, it is still useful\r
+    to be able to get data out of such a file.\r
+\r
+    Usage:\r
+        zd = _ZipDecrypter(mypwd)\r
+        plain_char = zd(cypher_char)\r
+        plain_text = map(zd, cypher_text)\r
+    """\r
+\r
+    def _GenerateCRCTable():\r
+        """Generate a CRC-32 table.\r
+\r
+        ZIP encryption uses the CRC32 one-byte primitive for scrambling some\r
+        internal keys. We noticed that a direct implementation is faster than\r
+        relying on binascii.crc32().\r
+        """\r
+        poly = 0xedb88320\r
+        table = [0] * 256\r
+        for i in range(256):\r
+            crc = i\r
+            for j in range(8):\r
+                if crc & 1:\r
+                    crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly\r
+                else:\r
+                    crc = ((crc >> 1) & 0x7FFFFFFF)\r
+            table[i] = crc\r
+        return table\r
+    crctable = _GenerateCRCTable()\r
+\r
+    def _crc32(self, ch, crc):\r
+        """Compute the CRC32 primitive on one byte."""\r
+        return ((crc >> 8) & 0xffffff) ^ self.crctable[(crc ^ ord(ch)) & 0xff]\r
+\r
+    def __init__(self, pwd):\r
+        self.key0 = 305419896\r
+        self.key1 = 591751049\r
+        self.key2 = 878082192\r
+        for p in pwd:\r
+            self._UpdateKeys(p)\r
+\r
+    def _UpdateKeys(self, c):\r
+        self.key0 = self._crc32(c, self.key0)\r
+        self.key1 = (self.key1 + (self.key0 & 255)) & 4294967295\r
+        self.key1 = (self.key1 * 134775813 + 1) & 4294967295\r
+        self.key2 = self._crc32(chr((self.key1 >> 24) & 255), self.key2)\r
+\r
+    def __call__(self, c):\r
+        """Decrypt a single character."""\r
+        c = ord(c)\r
+        k = self.key2 | 2\r
+        c = c ^ (((k * (k^1)) >> 8) & 255)\r
+        c = chr(c)\r
+        self._UpdateKeys(c)\r
+        return c\r
+\r
+\r
+compressor_names = {\r
+    0: 'store',\r
+    1: 'shrink',\r
+    2: 'reduce',\r
+    3: 'reduce',\r
+    4: 'reduce',\r
+    5: 'reduce',\r
+    6: 'implode',\r
+    7: 'tokenize',\r
+    8: 'deflate',\r
+    9: 'deflate64',\r
+    10: 'implode',\r
+    12: 'bzip2',\r
+    14: 'lzma',\r
+    18: 'terse',\r
+    19: 'lz77',\r
+    97: 'wavpack',\r
+    98: 'ppmd',\r
+}\r
+\r
+\r
+class ZipExtFile(io.BufferedIOBase):\r
+    """File-like object for reading an archive member.\r
+       Is returned by ZipFile.open().\r
+    """\r
+\r
+    # Max size supported by decompressor.\r
+    MAX_N = 1 << 31 - 1\r
+\r
+    # Read from compressed files in 4k blocks.\r
+    MIN_READ_SIZE = 4096\r
+\r
+    # Search for universal newlines or line chunks.\r
+    PATTERN = re.compile(r'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)')\r
+\r
+    def __init__(self, fileobj, mode, zipinfo, decrypter=None,\r
+            close_fileobj=False):\r
+        self._fileobj = fileobj\r
+        self._decrypter = decrypter\r
+        self._close_fileobj = close_fileobj\r
+\r
+        self._compress_type = zipinfo.compress_type\r
+        self._compress_size = zipinfo.compress_size\r
+        self._compress_left = zipinfo.compress_size\r
+\r
+        if self._compress_type == ZIP_DEFLATED:\r
+            self._decompressor = zlib.decompressobj(-15)\r
+        elif self._compress_type != ZIP_STORED:\r
+            descr = compressor_names.get(self._compress_type)\r
+            if descr:\r
+                raise NotImplementedError("compression type %d (%s)" % (self._compress_type, descr))\r
+            else:\r
+                raise NotImplementedError("compression type %d" % (self._compress_type,))\r
+        self._unconsumed = ''\r
+\r
+        self._readbuffer = ''\r
+        self._offset = 0\r
+\r
+        self._universal = 'U' in mode\r
+        self.newlines = None\r
+\r
+        # Adjust read size for encrypted files since the first 12 bytes\r
+        # are for the encryption/password information.\r
+        if self._decrypter is not None:\r
+            self._compress_left -= 12\r
+\r
+        self.mode = mode\r
+        self.name = zipinfo.filename\r
+\r
+        if hasattr(zipinfo, 'CRC'):\r
+            self._expected_crc = zipinfo.CRC\r
+            self._running_crc = crc32(b'') & 0xffffffff\r
+        else:\r
+            self._expected_crc = None\r
+\r
+    def readline(self, limit=-1):\r
+        """Read and return a line from the stream.\r
+\r
+        If limit is specified, at most limit bytes will be read.\r
+        """\r
+\r
+        if not self._universal and limit < 0:\r
+            # Shortcut common case - newline found in buffer.\r
+            i = self._readbuffer.find('\n', self._offset) + 1\r
+            if i > 0:\r
+                line = self._readbuffer[self._offset: i]\r
+                self._offset = i\r
+                return line\r
+\r
+        if not self._universal:\r
+            return io.BufferedIOBase.readline(self, limit)\r
+\r
+        line = ''\r
+        while limit < 0 or len(line) < limit:\r
+            readahead = self.peek(2)\r
+            if readahead == '':\r
+                return line\r
+\r
+            #\r
+            # Search for universal newlines or line chunks.\r
+            #\r
+            # The pattern returns either a line chunk or a newline, but not\r
+            # both. Combined with peek(2), we are assured that the sequence\r
+            # '\r\n' is always retrieved completely and never split into\r
+            # separate newlines - '\r', '\n' due to coincidental readaheads.\r
+            #\r
+            match = self.PATTERN.search(readahead)\r
+            newline = match.group('newline')\r
+            if newline is not None:\r
+                if self.newlines is None:\r
+                    self.newlines = []\r
+                if newline not in self.newlines:\r
+                    self.newlines.append(newline)\r
+                self._offset += len(newline)\r
+                return line + '\n'\r
+\r
+            chunk = match.group('chunk')\r
+            if limit >= 0:\r
+                chunk = chunk[: limit - len(line)]\r
+\r
+            self._offset += len(chunk)\r
+            line += chunk\r
+\r
+        return line\r
+\r
+    def peek(self, n=1):\r
+        """Returns buffered bytes without advancing the position."""\r
+        if n > len(self._readbuffer) - self._offset:\r
+            chunk = self.read(n)\r
+            if len(chunk) > self._offset:\r
+                self._readbuffer = chunk + self._readbuffer[self._offset:]\r
+                self._offset = 0\r
+            else:\r
+                self._offset -= len(chunk)\r
+\r
+        # Return up to 512 bytes to reduce allocation overhead for tight loops.\r
+        return self._readbuffer[self._offset: self._offset + 512]\r
+\r
+    def readable(self):\r
+        return True\r
+\r
+    def read(self, n=-1):\r
+        """Read and return up to n bytes.\r
+        If the argument is omitted, None, or negative, data is read and returned until EOF is reached..\r
+        """\r
+        buf = ''\r
+        if n is None:\r
+            n = -1\r
+        while True:\r
+            if n < 0:\r
+                data = self.read1(n)\r
+            elif n > len(buf):\r
+                data = self.read1(n - len(buf))\r
+            else:\r
+                return buf\r
+            if len(data) == 0:\r
+                return buf\r
+            buf += data\r
+\r
+    def _update_crc(self, newdata, eof):\r
+        # Update the CRC using the given data.\r
+        if self._expected_crc is None:\r
+            # No need to compute the CRC if we don't have a reference value\r
+            return\r
+        self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff\r
+        # Check the CRC if we're at the end of the file\r
+        if eof and self._running_crc != self._expected_crc:\r
+            raise BadZipfile("Bad CRC-32 for file %r" % self.name)\r
+\r
+    def read1(self, n):\r
+        """Read up to n bytes with at most one read() system call."""\r
+\r
+        # Simplify algorithm (branching) by transforming negative n to large n.\r
+        if n < 0 or n is None:\r
+            n = self.MAX_N\r
+\r
+        # Bytes available in read buffer.\r
+        len_readbuffer = len(self._readbuffer) - self._offset\r
+\r
+        # Read from file.\r
+        if self._compress_left > 0 and n > len_readbuffer + len(self._unconsumed):\r
+            nbytes = n - len_readbuffer - len(self._unconsumed)\r
+            nbytes = max(nbytes, self.MIN_READ_SIZE)\r
+            nbytes = min(nbytes, self._compress_left)\r
+\r
+            data = self._fileobj.read(nbytes)\r
+            self._compress_left -= len(data)\r
+\r
+            if data and self._decrypter is not None:\r
+                data = ''.join(map(self._decrypter, data))\r
+\r
+            if self._compress_type == ZIP_STORED:\r
+                self._update_crc(data, eof=(self._compress_left==0))\r
+                self._readbuffer = self._readbuffer[self._offset:] + data\r
+                self._offset = 0\r
+            else:\r
+                # Prepare deflated bytes for decompression.\r
+                self._unconsumed += data\r
+\r
+        # Handle unconsumed data.\r
+        if (len(self._unconsumed) > 0 and n > len_readbuffer and\r
+            self._compress_type == ZIP_DEFLATED):\r
+            data = self._decompressor.decompress(\r
+                self._unconsumed,\r
+                max(n - len_readbuffer, self.MIN_READ_SIZE)\r
+            )\r
+\r
+            self._unconsumed = self._decompressor.unconsumed_tail\r
+            eof = len(self._unconsumed) == 0 and self._compress_left == 0\r
+            if eof:\r
+                data += self._decompressor.flush()\r
+\r
+            self._update_crc(data, eof=eof)\r
+            self._readbuffer = self._readbuffer[self._offset:] + data\r
+            self._offset = 0\r
+\r
+        # Read from buffer.\r
+        data = self._readbuffer[self._offset: self._offset + n]\r
+        self._offset += len(data)\r
+        return data\r
+\r
+    def close(self):\r
+        try :\r
+            if self._close_fileobj:\r
+                self._fileobj.close()\r
+        finally:\r
+            super(ZipExtFile, self).close()\r
+\r
+\r
+class ZipFile(object):\r
+    """ Class with methods to open, read, write, close, list zip files.\r
+\r
+    z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False)\r
+\r
+    file: Either the path to the file, or a file-like object.\r
+          If it is a path, the file will be opened and closed by ZipFile.\r
+    mode: The mode can be either read "r", write "w" or append "a".\r
+    compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).\r
+    allowZip64: if True ZipFile will create files with ZIP64 extensions when\r
+                needed, otherwise it will raise an exception when this would\r
+                be necessary.\r
+\r
+    """\r
+\r
+    fp = None                   # Set here since __del__ checks it\r
+\r
+    def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):\r
+        """Open the ZIP file with mode read "r", write "w" or append "a"."""\r
+        if mode not in ("r", "w", "a"):\r
+            raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')\r
+\r
+        if compression == ZIP_STORED:\r
+            pass\r
+        elif compression == ZIP_DEFLATED:\r
+            if not zlib:\r
+                raise RuntimeError,\\r
+                      "Compression requires the (missing) zlib module"\r
+        else:\r
+            raise RuntimeError, "That compression method is not supported"\r
+\r
+        self._allowZip64 = allowZip64\r
+        self._didModify = False\r
+        self.debug = 0  # Level of printing: 0 through 3\r
+        self.NameToInfo = {}    # Find file info given name\r
+        self.filelist = []      # List of ZipInfo instances for archive\r
+        self.compression = compression  # Method of compression\r
+        self.mode = key = mode.replace('b', '')[0]\r
+        self.pwd = None\r
+        self._comment = ''\r
+\r
+        # Check if we were passed a file-like object\r
+        if isinstance(file, basestring):\r
+            self._filePassed = 0\r
+            self.filename = file\r
+            modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}\r
+            try:\r
+                self.fp = open(file, modeDict[mode])\r
+            except IOError:\r
+                if mode == 'a':\r
+                    mode = key = 'w'\r
+                    self.fp = open(file, modeDict[mode])\r
+                else:\r
+                    raise\r
+        else:\r
+            self._filePassed = 1\r
+            self.fp = file\r
+            self.filename = getattr(file, 'name', None)\r
+\r
+        try:\r
+            if key == 'r':\r
+                self._RealGetContents()\r
+            elif key == 'w':\r
+                # set the modified flag so central directory gets written\r
+                # even if no files are added to the archive\r
+                self._didModify = True\r
+            elif key == 'a':\r
+                try:\r
+                    # See if file is a zip file\r
+                    self._RealGetContents()\r
+                    # seek to start of directory and overwrite\r
+                    self.fp.seek(self.start_dir, 0)\r
+                except BadZipfile:\r
+                    # file is not a zip file, just append\r
+                    self.fp.seek(0, 2)\r
+\r
+                    # set the modified flag so central directory gets written\r
+                    # even if no files are added to the archive\r
+                    self._didModify = True\r
+            else:\r
+                raise RuntimeError('Mode must be "r", "w" or "a"')\r
+        except:\r
+            fp = self.fp\r
+            self.fp = None\r
+            if not self._filePassed:\r
+                fp.close()\r
+            raise\r
+\r
+    def __enter__(self):\r
+        return self\r
+\r
+    def __exit__(self, type, value, traceback):\r
+        self.close()\r
+\r
+    def _RealGetContents(self):\r
+        """Read in the table of contents for the ZIP file."""\r
+        fp = self.fp\r
+        try:\r
+            endrec = _EndRecData(fp)\r
+        except IOError:\r
+            raise BadZipfile("File is not a zip file")\r
+        if not endrec:\r
+            raise BadZipfile, "File is not a zip file"\r
+        if self.debug > 1:\r
+            print endrec\r
+        size_cd = endrec[_ECD_SIZE]             # bytes in central directory\r
+        offset_cd = endrec[_ECD_OFFSET]         # offset of central directory\r
+        self._comment = endrec[_ECD_COMMENT]    # archive comment\r
+\r
+        # "concat" is zero, unless zip was concatenated to another file\r
+        concat = endrec[_ECD_LOCATION] - size_cd - offset_cd\r
+        if endrec[_ECD_SIGNATURE] == stringEndArchive64:\r
+            # If Zip64 extension structures are present, account for them\r
+            concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)\r
+\r
+        if self.debug > 2:\r
+            inferred = concat + offset_cd\r
+            print "given, inferred, offset", offset_cd, inferred, concat\r
+        # self.start_dir:  Position of start of central directory\r
+        self.start_dir = offset_cd + concat\r
+        fp.seek(self.start_dir, 0)\r
+        data = fp.read(size_cd)\r
+        fp = cStringIO.StringIO(data)\r
+        total = 0\r
+        while total < size_cd:\r
+            centdir = fp.read(sizeCentralDir)\r
+            if len(centdir) != sizeCentralDir:\r
+                raise BadZipfile("Truncated central directory")\r
+            centdir = struct.unpack(structCentralDir, centdir)\r
+            if centdir[_CD_SIGNATURE] != stringCentralDir:\r
+                raise BadZipfile("Bad magic number for central directory")\r
+            if self.debug > 2:\r
+                print centdir\r
+            filename = fp.read(centdir[_CD_FILENAME_LENGTH])\r
+            # Create ZipInfo instance to store file information\r
+            x = ZipInfo(filename)\r
+            x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])\r
+            x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])\r
+            x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET]\r
+            (x.create_version, x.create_system, x.extract_version, x.reserved,\r
+                x.flag_bits, x.compress_type, t, d,\r
+                x.CRC, x.compress_size, x.file_size) = centdir[1:12]\r
+            x.volume, x.internal_attr, x.external_attr = centdir[15:18]\r
+            # Convert date/time code to (year, month, day, hour, min, sec)\r
+            x._raw_time = t\r
+            x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,\r
+                                     t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )\r
+\r
+            x._decodeExtra()\r
+            x.header_offset = x.header_offset + concat\r
+            x.filename = x._decodeFilename()\r
+            self.filelist.append(x)\r
+            self.NameToInfo[x.filename] = x\r
+\r
+            # update total bytes read from central directory\r
+            total = (total + sizeCentralDir + centdir[_CD_FILENAME_LENGTH]\r
+                     + centdir[_CD_EXTRA_FIELD_LENGTH]\r
+                     + centdir[_CD_COMMENT_LENGTH])\r
+\r
+            if self.debug > 2:\r
+                print "total", total\r
+\r
+\r
+    def namelist(self):\r
+        """Return a list of file names in the archive."""\r
+        l = []\r
+        for data in self.filelist:\r
+            l.append(data.filename)\r
+        return l\r
+\r
+    def infolist(self):\r
+        """Return a list of class ZipInfo instances for files in the\r
+        archive."""\r
+        return self.filelist\r
+\r
+    def printdir(self):\r
+        """Print a table of contents for the zip file."""\r
+        print "%-46s %19s %12s" % ("File Name", "Modified    ", "Size")\r
+        for zinfo in self.filelist:\r
+            date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]\r
+            print "%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size)\r
+\r
+    def testzip(self):\r
+        """Read all the files and check the CRC."""\r
+        chunk_size = 2 ** 20\r
+        for zinfo in self.filelist:\r
+            try:\r
+                # Read by chunks, to avoid an OverflowError or a\r
+                # MemoryError with very large embedded files.\r
+                with self.open(zinfo.filename, "r") as f:\r
+                    while f.read(chunk_size):     # Check CRC-32\r
+                        pass\r
+            except BadZipfile:\r
+                return zinfo.filename\r
+\r
+    def getinfo(self, name):\r
+        """Return the instance of ZipInfo given 'name'."""\r
+        info = self.NameToInfo.get(name)\r
+        if info is None:\r
+            raise KeyError(\r
+                'There is no item named %r in the archive' % name)\r
+\r
+        return info\r
+\r
+    def setpassword(self, pwd):\r
+        """Set default password for encrypted files."""\r
+        self.pwd = pwd\r
+\r
+    @property\r
+    def comment(self):\r
+        """The comment text associated with the ZIP file."""\r
+        return self._comment\r
+\r
+    @comment.setter\r
+    def comment(self, comment):\r
+        # check for valid comment length\r
+        if len(comment) > ZIP_MAX_COMMENT:\r
+            import warnings\r
+            warnings.warn('Archive comment is too long; truncating to %d bytes'\r
+                          % ZIP_MAX_COMMENT, stacklevel=2)\r
+            comment = comment[:ZIP_MAX_COMMENT]\r
+        self._comment = comment\r
+        self._didModify = True\r
+\r
+    def read(self, name, pwd=None):\r
+        """Return file bytes (as a string) for name."""\r
+        return self.open(name, "r", pwd).read()\r
+\r
+    def open(self, name, mode="r", pwd=None):\r
+        """Return file-like object for 'name'."""\r
+        if mode not in ("r", "U", "rU"):\r
+            raise RuntimeError, 'open() requires mode "r", "U", or "rU"'\r
+        if not self.fp:\r
+            raise RuntimeError, \\r
+                  "Attempt to read ZIP archive that was already closed"\r
+\r
+        # Only open a new file for instances where we were not\r
+        # given a file object in the constructor\r
+        if self._filePassed:\r
+            zef_file = self.fp\r
+            should_close = False\r
+        else:\r
+            zef_file = open(self.filename, 'rb')\r
+            should_close = True\r
+\r
+        try:\r
+            # Make sure we have an info object\r
+            if isinstance(name, ZipInfo):\r
+                # 'name' is already an info object\r
+                zinfo = name\r
+            else:\r
+                # Get info object for name\r
+                zinfo = self.getinfo(name)\r
+\r
+            zef_file.seek(zinfo.header_offset, 0)\r
+\r
+            # Skip the file header:\r
+            fheader = zef_file.read(sizeFileHeader)\r
+            if len(fheader) != sizeFileHeader:\r
+                raise BadZipfile("Truncated file header")\r
+            fheader = struct.unpack(structFileHeader, fheader)\r
+            if fheader[_FH_SIGNATURE] != stringFileHeader:\r
+                raise BadZipfile("Bad magic number for file header")\r
+\r
+            fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])\r
+            if fheader[_FH_EXTRA_FIELD_LENGTH]:\r
+                zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])\r
+\r
+            if fname != zinfo.orig_filename:\r
+                raise BadZipfile, \\r
+                        'File name in directory "%s" and header "%s" differ.' % (\r
+                            zinfo.orig_filename, fname)\r
+\r
+            # check for encrypted flag & handle password\r
+            is_encrypted = zinfo.flag_bits & 0x1\r
+            zd = None\r
+            if is_encrypted:\r
+                if not pwd:\r
+                    pwd = self.pwd\r
+                if not pwd:\r
+                    raise RuntimeError, "File %s is encrypted, " \\r
+                        "password required for extraction" % name\r
+\r
+                zd = _ZipDecrypter(pwd)\r
+                # The first 12 bytes in the cypher stream is an encryption header\r
+                #  used to strengthen the algorithm. The first 11 bytes are\r
+                #  completely random, while the 12th contains the MSB of the CRC,\r
+                #  or the MSB of the file time depending on the header type\r
+                #  and is used to check the correctness of the password.\r
+                bytes = zef_file.read(12)\r
+                h = map(zd, bytes[0:12])\r
+                if zinfo.flag_bits & 0x8:\r
+                    # compare against the file type from extended local headers\r
+                    check_byte = (zinfo._raw_time >> 8) & 0xff\r
+                else:\r
+                    # compare against the CRC otherwise\r
+                    check_byte = (zinfo.CRC >> 24) & 0xff\r
+                if ord(h[11]) != check_byte:\r
+                    raise RuntimeError("Bad password for file", name)\r
+\r
+            return ZipExtFile(zef_file, mode, zinfo, zd,\r
+                    close_fileobj=should_close)\r
+        except:\r
+            if should_close:\r
+                zef_file.close()\r
+            raise\r
+\r
+    def extract(self, member, path=None, pwd=None):\r
+        """Extract a member from the archive to the current working directory,\r
+           using its full name. Its file information is extracted as accurately\r
+           as possible. `member' may be a filename or a ZipInfo object. You can\r
+           specify a different directory using `path'.\r
+        """\r
+        if not isinstance(member, ZipInfo):\r
+            member = self.getinfo(member)\r
+\r
+        if path is None:\r
+            path = os.getcwd()\r
+\r
+        return self._extract_member(member, path, pwd)\r
+\r
+    def extractall(self, path=None, members=None, pwd=None):\r
+        """Extract all members from the archive to the current working\r
+           directory. `path' specifies a different directory to extract to.\r
+           `members' is optional and must be a subset of the list returned\r
+           by namelist().\r
+        """\r
+        if members is None:\r
+            members = self.namelist()\r
+\r
+        for zipinfo in members:\r
+            self.extract(zipinfo, path, pwd)\r
+\r
+    def _extract_member(self, member, targetpath, pwd):\r
+        """Extract the ZipInfo object 'member' to a physical\r
+           file on the path targetpath.\r
+        """\r
+        # build the destination pathname, replacing\r
+        # forward slashes to platform specific separators.\r
+        arcname = member.filename.replace('/', os.path.sep)\r
+\r
+        if os.path.altsep:\r
+            arcname = arcname.replace(os.path.altsep, os.path.sep)\r
+        # interpret absolute pathname as relative, remove drive letter or\r
+        # UNC path, redundant separators, "." and ".." components.\r
+        arcname = os.path.splitdrive(arcname)[1]\r
+        arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)\r
+                    if x not in ('', os.path.curdir, os.path.pardir))\r
+        if os.path.sep == '\\':\r
+            # filter illegal characters on Windows\r
+            illegal = ':<>|"?*'\r
+            if isinstance(arcname, unicode):\r
+                table = {ord(c): ord('_') for c in illegal}\r
+            else:\r
+                table = string.maketrans(illegal, '_' * len(illegal))\r
+            arcname = arcname.translate(table)\r
+            # remove trailing dots\r
+            arcname = (x.rstrip('.') for x in arcname.split(os.path.sep))\r
+            arcname = os.path.sep.join(x for x in arcname if x)\r
+\r
+        targetpath = os.path.join(targetpath, arcname)\r
+        targetpath = os.path.normpath(targetpath)\r
+\r
+        # Create all upper directories if necessary.\r
+        upperdirs = os.path.dirname(targetpath)\r
+        if upperdirs and not os.path.exists(upperdirs):\r
+            os.makedirs(upperdirs)\r
+\r
+        if member.filename[-1] == '/':\r
+            if not os.path.isdir(targetpath):\r
+                os.mkdir(targetpath)\r
+            return targetpath\r
+\r
+        with self.open(member, pwd=pwd) as source, \\r
+             file(targetpath, "wb") as target:\r
+            shutil.copyfileobj(source, target)\r
+\r
+        return targetpath\r
+\r
+    def _writecheck(self, zinfo):\r
+        """Check for errors before writing a file to the archive."""\r
+        if zinfo.filename in self.NameToInfo:\r
+            import warnings\r
+            warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3)\r
+        if self.mode not in ("w", "a"):\r
+            raise RuntimeError, 'write() requires mode "w" or "a"'\r
+        if not self.fp:\r
+            raise RuntimeError, \\r
+                  "Attempt to write ZIP archive that was already closed"\r
+        if zinfo.compress_type == ZIP_DEFLATED and not zlib:\r
+            raise RuntimeError, \\r
+                  "Compression requires the (missing) zlib module"\r
+        if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):\r
+            raise RuntimeError, \\r
+                  "That compression method is not supported"\r
+        if not self._allowZip64:\r
+            requires_zip64 = None\r
+            if len(self.filelist) >= ZIP_FILECOUNT_LIMIT:\r
+                requires_zip64 = "Files count"\r
+            elif zinfo.file_size > ZIP64_LIMIT:\r
+                requires_zip64 = "Filesize"\r
+            elif zinfo.header_offset > ZIP64_LIMIT:\r
+                requires_zip64 = "Zipfile size"\r
+            if requires_zip64:\r
+                raise LargeZipFile(requires_zip64 +\r
+                                   " would require ZIP64 extensions")\r
+\r
+    def write(self, filename, arcname=None, compress_type=None):\r
+        """Put the bytes from filename into the archive under the name\r
+        arcname."""\r
+        if not self.fp:\r
+            raise RuntimeError(\r
+                  "Attempt to write to ZIP archive that was already closed")\r
+\r
+        st = os.stat(filename)\r
+        isdir = stat.S_ISDIR(st.st_mode)\r
+        mtime = time.localtime(st.st_mtime)\r
+        date_time = mtime[0:6]\r
+        # Create ZipInfo instance to store file information\r
+        if arcname is None:\r
+            arcname = filename\r
+        arcname = os.path.normpath(os.path.splitdrive(arcname)[1])\r
+        while arcname[0] in (os.sep, os.altsep):\r
+            arcname = arcname[1:]\r
+        if isdir:\r
+            arcname += '/'\r
+        zinfo = ZipInfo(arcname, date_time)\r
+        zinfo.external_attr = (st[0] & 0xFFFF) << 16L      # Unix attributes\r
+        if compress_type is None:\r
+            zinfo.compress_type = self.compression\r
+        else:\r
+            zinfo.compress_type = compress_type\r
+\r
+        zinfo.file_size = st.st_size\r
+        zinfo.flag_bits = 0x00\r
+        zinfo.header_offset = self.fp.tell()    # Start of header bytes\r
+\r
+        self._writecheck(zinfo)\r
+        self._didModify = True\r
+\r
+        if isdir:\r
+            zinfo.file_size = 0\r
+            zinfo.compress_size = 0\r
+            zinfo.CRC = 0\r
+            zinfo.external_attr |= 0x10  # MS-DOS directory flag\r
+            self.filelist.append(zinfo)\r
+            self.NameToInfo[zinfo.filename] = zinfo\r
+            self.fp.write(zinfo.FileHeader(False))\r
+            return\r
+\r
+        with open(filename, "rb") as fp:\r
+            # Must overwrite CRC and sizes with correct data later\r
+            zinfo.CRC = CRC = 0\r
+            zinfo.compress_size = compress_size = 0\r
+            # Compressed size can be larger than uncompressed size\r
+            zip64 = self._allowZip64 and \\r
+                    zinfo.file_size * 1.05 > ZIP64_LIMIT\r
+            self.fp.write(zinfo.FileHeader(zip64))\r
+            if zinfo.compress_type == ZIP_DEFLATED:\r
+                cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,\r
+                     zlib.DEFLATED, -15)\r
+            else:\r
+                cmpr = None\r
+            file_size = 0\r
+            while 1:\r
+                buf = fp.read(1024 * 8)\r
+                if not buf:\r
+                    break\r
+                file_size = file_size + len(buf)\r
+                CRC = crc32(buf, CRC) & 0xffffffff\r
+                if cmpr:\r
+                    buf = cmpr.compress(buf)\r
+                    compress_size = compress_size + len(buf)\r
+                self.fp.write(buf)\r
+        if cmpr:\r
+            buf = cmpr.flush()\r
+            compress_size = compress_size + len(buf)\r
+            self.fp.write(buf)\r
+            zinfo.compress_size = compress_size\r
+        else:\r
+            zinfo.compress_size = file_size\r
+        zinfo.CRC = CRC\r
+        zinfo.file_size = file_size\r
+        if not zip64 and self._allowZip64:\r
+            if file_size > ZIP64_LIMIT:\r
+                raise RuntimeError('File size has increased during compressing')\r
+            if compress_size > ZIP64_LIMIT:\r
+                raise RuntimeError('Compressed size larger than uncompressed size')\r
+        # Seek backwards and write file header (which will now include\r
+        # correct CRC and file sizes)\r
+        position = self.fp.tell()       # Preserve current position in file\r
+        self.fp.seek(zinfo.header_offset, 0)\r
+        self.fp.write(zinfo.FileHeader(zip64))\r
+        self.fp.seek(position, 0)\r
+        self.filelist.append(zinfo)\r
+        self.NameToInfo[zinfo.filename] = zinfo\r
+\r
+    def writestr(self, zinfo_or_arcname, bytes, compress_type=None):\r
+        """Write a file into the archive.  The contents is the string\r
+        'bytes'.  'zinfo_or_arcname' is either a ZipInfo instance or\r
+        the name of the file in the archive."""\r
+        if not isinstance(zinfo_or_arcname, ZipInfo):\r
+            zinfo = ZipInfo(filename=zinfo_or_arcname,\r
+                            date_time=time.localtime(time.time())[:6])\r
+\r
+            zinfo.compress_type = self.compression\r
+            if zinfo.filename[-1] == '/':\r
+                zinfo.external_attr = 0o40775 << 16   # drwxrwxr-x\r
+                zinfo.external_attr |= 0x10           # MS-DOS directory flag\r
+            else:\r
+                zinfo.external_attr = 0o600 << 16     # ?rw-------\r
+        else:\r
+            zinfo = zinfo_or_arcname\r
+\r
+        if not self.fp:\r
+            raise RuntimeError(\r
+                  "Attempt to write to ZIP archive that was already closed")\r
+\r
+        if compress_type is not None:\r
+            zinfo.compress_type = compress_type\r
+\r
+        zinfo.file_size = len(bytes)            # Uncompressed size\r
+        zinfo.header_offset = self.fp.tell()    # Start of header bytes\r
+        self._writecheck(zinfo)\r
+        self._didModify = True\r
+        zinfo.CRC = crc32(bytes) & 0xffffffff       # CRC-32 checksum\r
+        if zinfo.compress_type == ZIP_DEFLATED:\r
+            co = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,\r
+                 zlib.DEFLATED, -15)\r
+            bytes = co.compress(bytes) + co.flush()\r
+            zinfo.compress_size = len(bytes)    # Compressed size\r
+        else:\r
+            zinfo.compress_size = zinfo.file_size\r
+        zip64 = zinfo.file_size > ZIP64_LIMIT or \\r
+                zinfo.compress_size > ZIP64_LIMIT\r
+        if zip64 and not self._allowZip64:\r
+            raise LargeZipFile("Filesize would require ZIP64 extensions")\r
+        self.fp.write(zinfo.FileHeader(zip64))\r
+        self.fp.write(bytes)\r
+        if zinfo.flag_bits & 0x08:\r
+            # Write CRC and file sizes after the file data\r
+            fmt = '<LQQ' if zip64 else '<LLL'\r
+            self.fp.write(struct.pack(fmt, zinfo.CRC, zinfo.compress_size,\r
+                  zinfo.file_size))\r
+        self.fp.flush()\r
+        self.filelist.append(zinfo)\r
+        self.NameToInfo[zinfo.filename] = zinfo\r
+\r
+    def __del__(self):\r
+        """Call the "close()" method in case the user forgot."""\r
+        self.close()\r
+\r
+    def close(self):\r
+        """Close the file, and for mode "w" and "a" write the ending\r
+        records."""\r
+        if self.fp is None:\r
+            return\r
+\r
+        try:\r
+            if self.mode in ("w", "a") and self._didModify: # write ending records\r
+                pos1 = self.fp.tell()\r
+                for zinfo in self.filelist:         # write central directory\r
+                    dt = zinfo.date_time\r
+                    dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]\r
+                    dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)\r
+                    extra = []\r
+                    if zinfo.file_size > ZIP64_LIMIT \\r
+                            or zinfo.compress_size > ZIP64_LIMIT:\r
+                        extra.append(zinfo.file_size)\r
+                        extra.append(zinfo.compress_size)\r
+                        file_size = 0xffffffff\r
+                        compress_size = 0xffffffff\r
+                    else:\r
+                        file_size = zinfo.file_size\r
+                        compress_size = zinfo.compress_size\r
+\r
+                    if zinfo.header_offset > ZIP64_LIMIT:\r
+                        extra.append(zinfo.header_offset)\r
+                        header_offset = 0xffffffffL\r
+                    else:\r
+                        header_offset = zinfo.header_offset\r
+\r
+                    extra_data = zinfo.extra\r
+                    if extra:\r
+                        # Append a ZIP64 field to the extra's\r
+                        extra_data = struct.pack(\r
+                                '<HH' + 'Q'*len(extra),\r
+                                1, 8*len(extra), *extra) + extra_data\r
+\r
+                        extract_version = max(45, zinfo.extract_version)\r
+                        create_version = max(45, zinfo.create_version)\r
+                    else:\r
+                        extract_version = zinfo.extract_version\r
+                        create_version = zinfo.create_version\r
+\r
+                    try:\r
+                        filename, flag_bits = zinfo._encodeFilenameFlags()\r
+                        centdir = struct.pack(structCentralDir,\r
+                        stringCentralDir, create_version,\r
+                        zinfo.create_system, extract_version, zinfo.reserved,\r
+                        flag_bits, zinfo.compress_type, dostime, dosdate,\r
+                        zinfo.CRC, compress_size, file_size,\r
+                        len(filename), len(extra_data), len(zinfo.comment),\r
+                        0, zinfo.internal_attr, zinfo.external_attr,\r
+                        header_offset)\r
+                    except DeprecationWarning:\r
+                        print >>sys.stderr, (structCentralDir,\r
+                        stringCentralDir, create_version,\r
+                        zinfo.create_system, extract_version, zinfo.reserved,\r
+                        zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,\r
+                        zinfo.CRC, compress_size, file_size,\r
+                        len(zinfo.filename), len(extra_data), len(zinfo.comment),\r
+                        0, zinfo.internal_attr, zinfo.external_attr,\r
+                        header_offset)\r
+                        raise\r
+                    self.fp.write(centdir)\r
+                    self.fp.write(filename)\r
+                    self.fp.write(extra_data)\r
+                    self.fp.write(zinfo.comment)\r
+\r
+                pos2 = self.fp.tell()\r
+                # Write end-of-zip-archive record\r
+                centDirCount = len(self.filelist)\r
+                centDirSize = pos2 - pos1\r
+                centDirOffset = pos1\r
+                requires_zip64 = None\r
+                if centDirCount > ZIP_FILECOUNT_LIMIT:\r
+                    requires_zip64 = "Files count"\r
+                elif centDirOffset > ZIP64_LIMIT:\r
+                    requires_zip64 = "Central directory offset"\r
+                elif centDirSize > ZIP64_LIMIT:\r
+                    requires_zip64 = "Central directory size"\r
+                if requires_zip64:\r
+                    # Need to write the ZIP64 end-of-archive records\r
+                    if not self._allowZip64:\r
+                        raise LargeZipFile(requires_zip64 +\r
+                                           " would require ZIP64 extensions")\r
+                    zip64endrec = struct.pack(\r
+                            structEndArchive64, stringEndArchive64,\r
+                            44, 45, 45, 0, 0, centDirCount, centDirCount,\r
+                            centDirSize, centDirOffset)\r
+                    self.fp.write(zip64endrec)\r
+\r
+                    zip64locrec = struct.pack(\r
+                            structEndArchive64Locator,\r
+                            stringEndArchive64Locator, 0, pos2, 1)\r
+                    self.fp.write(zip64locrec)\r
+                    centDirCount = min(centDirCount, 0xFFFF)\r
+                    centDirSize = min(centDirSize, 0xFFFFFFFF)\r
+                    centDirOffset = min(centDirOffset, 0xFFFFFFFF)\r
+\r
+                endrec = struct.pack(structEndArchive, stringEndArchive,\r
+                                    0, 0, centDirCount, centDirCount,\r
+                                    centDirSize, centDirOffset, len(self._comment))\r
+                self.fp.write(endrec)\r
+                self.fp.write(self._comment)\r
+                self.fp.flush()\r
+        finally:\r
+            fp = self.fp\r
+            self.fp = None\r
+            if not self._filePassed:\r
+                fp.close()\r
+\r
+\r
+class PyZipFile(ZipFile):\r
+    """Class to create ZIP archives with Python library files and packages."""\r
+\r
+    def writepy(self, pathname, basename = ""):\r
+        """Add all files from "pathname" to the ZIP archive.\r
+\r
+        If pathname is a package directory, search the directory and\r
+        all package subdirectories recursively for all *.py and enter\r
+        the modules into the archive.  If pathname is a plain\r
+        directory, listdir *.py and enter all modules.  Else, pathname\r
+        must be a Python *.py file and the module will be put into the\r
+        archive.  Added modules are always module.pyo or module.pyc.\r
+        This method will compile the module.py into module.pyc if\r
+        necessary.\r
+        """\r
+        dir, name = os.path.split(pathname)\r
+        if os.path.isdir(pathname):\r
+            initname = os.path.join(pathname, "__init__.py")\r
+            if os.path.isfile(initname):\r
+                # This is a package directory, add it\r
+                if basename:\r
+                    basename = "%s/%s" % (basename, name)\r
+                else:\r
+                    basename = name\r
+                if self.debug:\r
+                    print "Adding package in", pathname, "as", basename\r
+                fname, arcname = self._get_codename(initname[0:-3], basename)\r
+                if self.debug:\r
+                    print "Adding", arcname\r
+                self.write(fname, arcname)\r
+                dirlist = os.listdir(pathname)\r
+                dirlist.remove("__init__.py")\r
+                # Add all *.py files and package subdirectories\r
+                for filename in dirlist:\r
+                    path = os.path.join(pathname, filename)\r
+                    root, ext = os.path.splitext(filename)\r
+                    if os.path.isdir(path):\r
+                        if os.path.isfile(os.path.join(path, "__init__.py")):\r
+                            # This is a package directory, add it\r
+                            self.writepy(path, basename)  # Recursive call\r
+                    elif ext == ".py":\r
+                        fname, arcname = self._get_codename(path[0:-3],\r
+                                         basename)\r
+                        if self.debug:\r
+                            print "Adding", arcname\r
+                        self.write(fname, arcname)\r
+            else:\r
+                # This is NOT a package directory, add its files at top level\r
+                if self.debug:\r
+                    print "Adding files from directory", pathname\r
+                for filename in os.listdir(pathname):\r
+                    path = os.path.join(pathname, filename)\r
+                    root, ext = os.path.splitext(filename)\r
+                    if ext == ".py":\r
+                        fname, arcname = self._get_codename(path[0:-3],\r
+                                         basename)\r
+                        if self.debug:\r
+                            print "Adding", arcname\r
+                        self.write(fname, arcname)\r
+        else:\r
+            if pathname[-3:] != ".py":\r
+                raise RuntimeError, \\r
+                      'Files added with writepy() must end with ".py"'\r
+            fname, arcname = self._get_codename(pathname[0:-3], basename)\r
+            if self.debug:\r
+                print "Adding file", arcname\r
+            self.write(fname, arcname)\r
+\r
+    def _get_codename(self, pathname, basename):\r
+        """Return (filename, archivename) for the path.\r
+\r
+        Given a module name path, return the correct file path and\r
+        archive name, compiling if necessary.  For example, given\r
+        /python/lib/string, return (/python/lib/string.pyc, string).\r
+        """\r
+        file_py  = pathname + ".py"\r
+        file_pyc = pathname + ".pyc"\r
+        file_pyo = pathname + ".pyo"\r
+        if os.path.isfile(file_pyo) and \\r
+                            os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:\r
+            fname = file_pyo    # Use .pyo file\r
+        elif not os.path.isfile(file_pyc) or \\r
+             os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:\r
+            import py_compile\r
+            if self.debug:\r
+                print "Compiling", file_py\r
+            try:\r
+                py_compile.compile(file_py, file_pyc, None, True)\r
+            except py_compile.PyCompileError,err:\r
+                print err.msg\r
+            fname = file_pyc\r
+        else:\r
+            fname = file_pyc\r
+        archivename = os.path.split(fname)[1]\r
+        if basename:\r
+            archivename = "%s/%s" % (basename, archivename)\r
+        return (fname, archivename)\r
+\r
+\r
+def main(args = None):\r
+    import textwrap\r
+    USAGE=textwrap.dedent("""\\r
+        Usage:\r
+            zipfile.py -l zipfile.zip        # Show listing of a zipfile\r
+            zipfile.py -t zipfile.zip        # Test if a zipfile is valid\r
+            zipfile.py -e zipfile.zip target # Extract zipfile into target dir\r
+            zipfile.py -c zipfile.zip src ... # Create zipfile from sources\r
+        """)\r
+    if args is None:\r
+        args = sys.argv[1:]\r
+\r
+    if not args or args[0] not in ('-l', '-c', '-e', '-t'):\r
+        print USAGE\r
+        sys.exit(1)\r
+\r
+    if args[0] == '-l':\r
+        if len(args) != 2:\r
+            print USAGE\r
+            sys.exit(1)\r
+        with ZipFile(args[1], 'r') as zf:\r
+            zf.printdir()\r
+\r
+    elif args[0] == '-t':\r
+        if len(args) != 2:\r
+            print USAGE\r
+            sys.exit(1)\r
+        with ZipFile(args[1], 'r') as zf:\r
+            badfile = zf.testzip()\r
+        if badfile:\r
+            print("The following enclosed file is corrupted: {!r}".format(badfile))\r
+        print "Done testing"\r
+\r
+    elif args[0] == '-e':\r
+        if len(args) != 3:\r
+            print USAGE\r
+            sys.exit(1)\r
+\r
+        with ZipFile(args[1], 'r') as zf:\r
+            zf.extractall(args[2])\r
+\r
+    elif args[0] == '-c':\r
+        if len(args) < 3:\r
+            print USAGE\r
+            sys.exit(1)\r
+\r
+        def addToZip(zf, path, zippath):\r
+            if os.path.isfile(path):\r
+                zf.write(path, zippath, ZIP_DEFLATED)\r
+            elif os.path.isdir(path):\r
+                if zippath:\r
+                    zf.write(path, zippath)\r
+                for nm in os.listdir(path):\r
+                    addToZip(zf,\r
+                            os.path.join(path, nm), os.path.join(zippath, nm))\r
+            # else: ignore\r
+\r
+        with ZipFile(args[1], 'w', allowZip64=True) as zf:\r
+            for path in args[2:]:\r
+                zippath = os.path.basename(path)\r
+                if not zippath:\r
+                    zippath = os.path.basename(os.path.dirname(path))\r
+                if zippath in ('', os.curdir, os.pardir):\r
+                    zippath = ''\r
+                addToZip(zf, path, zippath)\r
+\r
+if __name__ == "__main__":\r
+    main()\r