Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | """ |
| 2 | Read and write ZIP files. |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 3 | |
| 4 | XXX references to utf-8 need further investigation. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5 | """ |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 6 | import io |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 7 | import os |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 8 | import re |
Brett Cannon | b57a085 | 2013-06-15 17:32:30 -0400 | [diff] [blame] | 9 | import importlib.util |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 10 | import sys |
| 11 | import time |
| 12 | import stat |
| 13 | import shutil |
| 14 | import struct |
| 15 | import binascii |
| 16 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 17 | |
| 18 | try: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 19 | import zlib # We may need its compression method |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 20 | crc32 = zlib.crc32 |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 21 | except ImportError: |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 22 | zlib = None |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 23 | crc32 = binascii.crc32 |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 24 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 25 | try: |
| 26 | import bz2 # We may need its compression method |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 27 | except ImportError: |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 28 | bz2 = None |
| 29 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 30 | try: |
| 31 | import lzma # We may need its compression method |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 32 | except ImportError: |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 33 | lzma = None |
| 34 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 35 | __all__ = ["BadZipFile", "BadZipfile", "error", |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 36 | "ZIP_STORED", "ZIP_DEFLATED", "ZIP_BZIP2", "ZIP_LZMA", |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 37 | "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"] |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 38 | |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 39 | class BadZipFile(Exception): |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 40 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | class LargeZipFile(Exception): |
| 44 | """ |
| 45 | Raised when writing a zipfile, the zipfile requires ZIP64 extensions |
| 46 | and those extensions are disabled. |
| 47 | """ |
| 48 | |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 49 | error = BadZipfile = BadZipFile # Pre-3.2 compatibility names |
| 50 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 51 | |
Amaury Forgeot d'Arc | 0c3f8a4 | 2009-01-17 16:42:26 +0000 | [diff] [blame] | 52 | ZIP64_LIMIT = (1 << 31) - 1 |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 53 | ZIP_FILECOUNT_LIMIT = 1 << 16 |
| 54 | ZIP_MAX_COMMENT = (1 << 16) - 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 56 | # constants for Zip file compression methods |
| 57 | ZIP_STORED = 0 |
| 58 | ZIP_DEFLATED = 8 |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 59 | ZIP_BZIP2 = 12 |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 60 | ZIP_LZMA = 14 |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 61 | # Other ZIP compression methods not supported |
| 62 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 63 | DEFAULT_VERSION = 20 |
| 64 | ZIP64_VERSION = 45 |
| 65 | BZIP2_VERSION = 46 |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 66 | LZMA_VERSION = 63 |
Martin v. Löwis | d099b56 | 2012-05-01 14:08:22 +0200 | [diff] [blame] | 67 | # we recognize (but not necessarily support) all features up to that version |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 68 | MAX_EXTRACT_VERSION = 63 |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 69 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 70 | # Below are some formats and associated data for reading/writing headers using |
| 71 | # the struct module. The names and structures of headers/records are those used |
| 72 | # in the PKWARE description of the ZIP file format: |
| 73 | # http://www.pkware.com/documents/casestudies/APPNOTE.TXT |
| 74 | # (URL valid as of January 2008) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 76 | # The "end of central directory" structure, magic number, size, and indices |
| 77 | # (section V.I in the format document) |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 78 | structEndArchive = b"<4s4H2LH" |
| 79 | stringEndArchive = b"PK\005\006" |
| 80 | sizeEndCentDir = struct.calcsize(structEndArchive) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 81 | |
| 82 | _ECD_SIGNATURE = 0 |
| 83 | _ECD_DISK_NUMBER = 1 |
| 84 | _ECD_DISK_START = 2 |
| 85 | _ECD_ENTRIES_THIS_DISK = 3 |
| 86 | _ECD_ENTRIES_TOTAL = 4 |
| 87 | _ECD_SIZE = 5 |
| 88 | _ECD_OFFSET = 6 |
| 89 | _ECD_COMMENT_SIZE = 7 |
| 90 | # These last two indices are not part of the structure as defined in the |
| 91 | # spec, but they are used internally by this module as a convenience |
| 92 | _ECD_COMMENT = 8 |
| 93 | _ECD_LOCATION = 9 |
| 94 | |
| 95 | # The "central directory" structure, magic number, size, and indices |
| 96 | # of entries in the structure (section V.F in the format document) |
| 97 | structCentralDir = "<4s4B4HL2L5H2L" |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 98 | stringCentralDir = b"PK\001\002" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 99 | sizeCentralDir = struct.calcsize(structCentralDir) |
| 100 | |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 101 | # indexes of entries in the central directory structure |
| 102 | _CD_SIGNATURE = 0 |
| 103 | _CD_CREATE_VERSION = 1 |
| 104 | _CD_CREATE_SYSTEM = 2 |
| 105 | _CD_EXTRACT_VERSION = 3 |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 106 | _CD_EXTRACT_SYSTEM = 4 |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 107 | _CD_FLAG_BITS = 5 |
| 108 | _CD_COMPRESS_TYPE = 6 |
| 109 | _CD_TIME = 7 |
| 110 | _CD_DATE = 8 |
| 111 | _CD_CRC = 9 |
| 112 | _CD_COMPRESSED_SIZE = 10 |
| 113 | _CD_UNCOMPRESSED_SIZE = 11 |
| 114 | _CD_FILENAME_LENGTH = 12 |
| 115 | _CD_EXTRA_FIELD_LENGTH = 13 |
| 116 | _CD_COMMENT_LENGTH = 14 |
| 117 | _CD_DISK_NUMBER_START = 15 |
| 118 | _CD_INTERNAL_FILE_ATTRIBUTES = 16 |
| 119 | _CD_EXTERNAL_FILE_ATTRIBUTES = 17 |
| 120 | _CD_LOCAL_HEADER_OFFSET = 18 |
| 121 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 122 | # The "local file header" structure, magic number, size, and indices |
| 123 | # (section V.A in the format document) |
| 124 | structFileHeader = "<4s2B4HL2L2H" |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 125 | stringFileHeader = b"PK\003\004" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 126 | sizeFileHeader = struct.calcsize(structFileHeader) |
| 127 | |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 128 | _FH_SIGNATURE = 0 |
| 129 | _FH_EXTRACT_VERSION = 1 |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 130 | _FH_EXTRACT_SYSTEM = 2 |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 131 | _FH_GENERAL_PURPOSE_FLAG_BITS = 3 |
| 132 | _FH_COMPRESSION_METHOD = 4 |
| 133 | _FH_LAST_MOD_TIME = 5 |
| 134 | _FH_LAST_MOD_DATE = 6 |
| 135 | _FH_CRC = 7 |
| 136 | _FH_COMPRESSED_SIZE = 8 |
| 137 | _FH_UNCOMPRESSED_SIZE = 9 |
| 138 | _FH_FILENAME_LENGTH = 10 |
| 139 | _FH_EXTRA_FIELD_LENGTH = 11 |
| 140 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 141 | # The "Zip64 end of central directory locator" structure, magic number, and size |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 142 | structEndArchive64Locator = "<4sLQL" |
| 143 | stringEndArchive64Locator = b"PK\x06\x07" |
| 144 | sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 145 | |
| 146 | # The "Zip64 end of central directory" record, magic number, size, and indices |
| 147 | # (section V.G in the format document) |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 148 | structEndArchive64 = "<4sQ2H2L4Q" |
| 149 | stringEndArchive64 = b"PK\x06\x06" |
| 150 | sizeEndCentDir64 = struct.calcsize(structEndArchive64) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 151 | |
| 152 | _CD64_SIGNATURE = 0 |
| 153 | _CD64_DIRECTORY_RECSIZE = 1 |
| 154 | _CD64_CREATE_VERSION = 2 |
| 155 | _CD64_EXTRACT_VERSION = 3 |
| 156 | _CD64_DISK_NUMBER = 4 |
| 157 | _CD64_DISK_NUMBER_START = 5 |
| 158 | _CD64_NUMBER_ENTRIES_THIS_DISK = 6 |
| 159 | _CD64_NUMBER_ENTRIES_TOTAL = 7 |
| 160 | _CD64_DIRECTORY_SIZE = 8 |
| 161 | _CD64_OFFSET_START_CENTDIR = 9 |
| 162 | |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 163 | def _check_zipfile(fp): |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 164 | try: |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 165 | if _EndRecData(fp): |
| 166 | return True # file has correct magic number |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 167 | except OSError: |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 168 | pass |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 169 | return False |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 171 | def is_zipfile(filename): |
| 172 | """Quickly see if a file is a ZIP file by checking the magic number. |
| 173 | |
| 174 | The filename argument may be a file or file-like object too. |
| 175 | """ |
| 176 | result = False |
| 177 | try: |
| 178 | if hasattr(filename, "read"): |
| 179 | result = _check_zipfile(fp=filename) |
| 180 | else: |
| 181 | with open(filename, "rb") as fp: |
| 182 | result = _check_zipfile(fp) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 183 | except OSError: |
Antoine Pitrou | db5fe66 | 2008-12-27 15:50:40 +0000 | [diff] [blame] | 184 | pass |
| 185 | return result |
| 186 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 187 | def _EndRecData64(fpin, offset, endrec): |
| 188 | """ |
| 189 | Read the ZIP64 end-of-archive records and use that to update endrec |
| 190 | """ |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 191 | try: |
| 192 | fpin.seek(offset - sizeEndCentDir64Locator, 2) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 193 | except OSError: |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 194 | # If the seek fails, the file is not large enough to contain a ZIP64 |
| 195 | # end-of-archive record, so just return the end record we were given. |
| 196 | return endrec |
| 197 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 198 | data = fpin.read(sizeEndCentDir64Locator) |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 199 | if len(data) != sizeEndCentDir64Locator: |
| 200 | return endrec |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 201 | sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data) |
| 202 | if sig != stringEndArchive64Locator: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 203 | return endrec |
| 204 | |
| 205 | if diskno != 0 or disks != 1: |
Éric Araujo | ae2d832 | 2010-10-28 13:49:17 +0000 | [diff] [blame] | 206 | raise BadZipFile("zipfiles that span multiple disks are not supported") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 207 | |
| 208 | # Assume no 'zip64 extensible data' |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 209 | fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2) |
| 210 | data = fpin.read(sizeEndCentDir64) |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 211 | if len(data) != sizeEndCentDir64: |
| 212 | return endrec |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 213 | sig, sz, create_version, read_version, disk_num, disk_dir, \ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 214 | dircount, dircount2, dirsize, diroffset = \ |
| 215 | struct.unpack(structEndArchive64, data) |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 216 | if sig != stringEndArchive64: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 217 | return endrec |
| 218 | |
| 219 | # Update the original endrec using data from the ZIP64 record |
Antoine Pitrou | 9e4fdf4 | 2008-09-05 23:43:02 +0000 | [diff] [blame] | 220 | endrec[_ECD_SIGNATURE] = sig |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 221 | endrec[_ECD_DISK_NUMBER] = disk_num |
| 222 | endrec[_ECD_DISK_START] = disk_dir |
| 223 | endrec[_ECD_ENTRIES_THIS_DISK] = dircount |
| 224 | endrec[_ECD_ENTRIES_TOTAL] = dircount2 |
| 225 | endrec[_ECD_SIZE] = dirsize |
| 226 | endrec[_ECD_OFFSET] = diroffset |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 227 | return endrec |
| 228 | |
| 229 | |
Martin v. Löwis | 6f6873b | 2002-10-13 13:54:50 +0000 | [diff] [blame] | 230 | def _EndRecData(fpin): |
| 231 | """Return data from the "End of Central Directory" record, or None. |
| 232 | |
| 233 | The data is a list of the nine items in the ZIP "End of central dir" |
| 234 | record followed by a tenth item, the file seek offset of this record.""" |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 235 | |
| 236 | # Determine file size |
| 237 | fpin.seek(0, 2) |
| 238 | filesize = fpin.tell() |
| 239 | |
| 240 | # Check to see if this is ZIP file with no archive comment (the |
| 241 | # "end of central directory" structure should be the last item in the |
| 242 | # file if this is the case). |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 243 | try: |
| 244 | fpin.seek(-sizeEndCentDir, 2) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 245 | except OSError: |
Amaury Forgeot d'Arc | bc34780 | 2009-07-28 22:18:57 +0000 | [diff] [blame] | 246 | return None |
Martin v. Löwis | 6f6873b | 2002-10-13 13:54:50 +0000 | [diff] [blame] | 247 | data = fpin.read() |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 248 | if (len(data) == sizeEndCentDir and |
| 249 | data[0:4] == stringEndArchive and |
| 250 | data[-2:] == b"\000\000"): |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 251 | # the signature is correct and there's no comment, unpack structure |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 252 | endrec = struct.unpack(structEndArchive, data) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 253 | endrec=list(endrec) |
| 254 | |
| 255 | # Append a blank comment and record start offset |
| 256 | endrec.append(b"") |
| 257 | endrec.append(filesize - sizeEndCentDir) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 258 | |
Amaury Forgeot d'Arc | d3fb4bb | 2009-01-18 00:29:02 +0000 | [diff] [blame] | 259 | # Try to read the "Zip64 end of central directory" structure |
| 260 | return _EndRecData64(fpin, -sizeEndCentDir, endrec) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 261 | |
| 262 | # Either this is not a ZIP file, or it is a ZIP file with an archive |
| 263 | # comment. Search the end of the file for the "end of central directory" |
| 264 | # record signature. The comment is the last item in the ZIP file and may be |
| 265 | # up to 64K long. It is assumed that the "end of central directory" magic |
| 266 | # number does not appear in the comment. |
| 267 | maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0) |
| 268 | fpin.seek(maxCommentStart, 0) |
Martin v. Löwis | 6f6873b | 2002-10-13 13:54:50 +0000 | [diff] [blame] | 269 | data = fpin.read() |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 270 | start = data.rfind(stringEndArchive) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 271 | if start >= 0: |
| 272 | # found the magic number; attempt to unpack and interpret |
| 273 | recData = data[start:start+sizeEndCentDir] |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 274 | if len(recData) != sizeEndCentDir: |
| 275 | # Zip file is corrupted. |
| 276 | return None |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 277 | endrec = list(struct.unpack(structEndArchive, recData)) |
R David Murray | 4fbb9db | 2011-06-09 15:50:51 -0400 | [diff] [blame] | 278 | commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file |
| 279 | comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize] |
| 280 | endrec.append(comment) |
| 281 | endrec.append(maxCommentStart + start) |
Amaury Forgeot d'Arc | d3fb4bb | 2009-01-18 00:29:02 +0000 | [diff] [blame] | 282 | |
R David Murray | 4fbb9db | 2011-06-09 15:50:51 -0400 | [diff] [blame] | 283 | # Try to read the "Zip64 end of central directory" structure |
| 284 | return _EndRecData64(fpin, maxCommentStart + start - filesize, |
| 285 | endrec) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 286 | |
| 287 | # Unable to find a valid end of central directory structure |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 288 | return None |
Martin v. Löwis | 6f6873b | 2002-10-13 13:54:50 +0000 | [diff] [blame] | 289 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 290 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | class ZipInfo (object): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 292 | """Class with attributes describing each file in the ZIP archive.""" |
| 293 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 294 | __slots__ = ( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 295 | 'orig_filename', |
| 296 | 'filename', |
| 297 | 'date_time', |
| 298 | 'compress_type', |
| 299 | 'comment', |
| 300 | 'extra', |
| 301 | 'create_system', |
| 302 | 'create_version', |
| 303 | 'extract_version', |
| 304 | 'reserved', |
| 305 | 'flag_bits', |
| 306 | 'volume', |
| 307 | 'internal_attr', |
| 308 | 'external_attr', |
| 309 | 'header_offset', |
| 310 | 'CRC', |
| 311 | 'compress_size', |
| 312 | 'file_size', |
| 313 | '_raw_time', |
| 314 | ) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 316 | def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)): |
Greg Ward | 8e36d28 | 2003-06-18 00:53:06 +0000 | [diff] [blame] | 317 | self.orig_filename = filename # Original file name in archive |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 318 | |
| 319 | # Terminate the file name at the first null byte. Null bytes in file |
| 320 | # names are used as tricks by viruses in archives. |
Greg Ward | 8e36d28 | 2003-06-18 00:53:06 +0000 | [diff] [blame] | 321 | null_byte = filename.find(chr(0)) |
| 322 | if null_byte >= 0: |
| 323 | filename = filename[0:null_byte] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 324 | # This is used to ensure paths in generated ZIP files always use |
| 325 | # forward slashes as the directory separator, as required by the |
| 326 | # ZIP format specification. |
| 327 | if os.sep != "/" and os.sep in filename: |
Greg Ward | 8e36d28 | 2003-06-18 00:53:06 +0000 | [diff] [blame] | 328 | filename = filename.replace(os.sep, "/") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 329 | |
Greg Ward | 8e36d28 | 2003-06-18 00:53:06 +0000 | [diff] [blame] | 330 | self.filename = filename # Normalized file name |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 331 | self.date_time = date_time # year, month, day, hour, min, sec |
Senthil Kumaran | 29fa9d4 | 2011-10-20 01:46:00 +0800 | [diff] [blame] | 332 | |
| 333 | if date_time[0] < 1980: |
| 334 | raise ValueError('ZIP does not support timestamps before 1980') |
| 335 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 336 | # Standard values: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 337 | self.compress_type = ZIP_STORED # Type of compression for the file |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 338 | self.comment = b"" # Comment for each file |
| 339 | self.extra = b"" # ZIP extra data |
Martin v. Löwis | 0075690 | 2006-02-05 17:09:41 +0000 | [diff] [blame] | 340 | if sys.platform == 'win32': |
| 341 | self.create_system = 0 # System which created ZIP archive |
| 342 | else: |
| 343 | # Assume everything else is unix-y |
| 344 | self.create_system = 3 # System which created ZIP archive |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 345 | self.create_version = DEFAULT_VERSION # Version which created ZIP archive |
| 346 | self.extract_version = DEFAULT_VERSION # Version needed to extract archive |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 347 | self.reserved = 0 # Must be zero |
| 348 | self.flag_bits = 0 # ZIP flag bits |
| 349 | self.volume = 0 # Volume number of file header |
| 350 | self.internal_attr = 0 # Internal attributes |
| 351 | self.external_attr = 0 # External file attributes |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 352 | # Other attributes are set by class ZipFile: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 353 | # header_offset Byte offset to the file header |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 354 | # CRC CRC-32 of the uncompressed file |
| 355 | # compress_size Size of the compressed file |
| 356 | # file_size Size of the uncompressed file |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 357 | |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 358 | def FileHeader(self, zip64=None): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 359 | """Return the per-file header as a string.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 360 | dt = self.date_time |
| 361 | dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2] |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 362 | dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 363 | if self.flag_bits & 0x08: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 364 | # Set these to zero because we write them after the file data |
| 365 | CRC = compress_size = file_size = 0 |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 366 | else: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 367 | CRC = self.CRC |
| 368 | compress_size = self.compress_size |
| 369 | file_size = self.file_size |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 370 | |
| 371 | extra = self.extra |
| 372 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 373 | min_version = 0 |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 374 | if zip64 is None: |
| 375 | zip64 = file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT |
| 376 | if zip64: |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 377 | fmt = '<HHQQ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 378 | extra = extra + struct.pack(fmt, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 379 | 1, struct.calcsize(fmt)-4, file_size, compress_size) |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 380 | if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT: |
| 381 | if not zip64: |
| 382 | raise LargeZipFile("Filesize would require ZIP64 extensions") |
| 383 | # File is larger than what fits into a 4 byte integer, |
| 384 | # fall back to the ZIP64 extension |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 385 | file_size = 0xffffffff |
| 386 | compress_size = 0xffffffff |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 387 | min_version = ZIP64_VERSION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 388 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 389 | if self.compress_type == ZIP_BZIP2: |
| 390 | min_version = max(BZIP2_VERSION, min_version) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 391 | elif self.compress_type == ZIP_LZMA: |
| 392 | min_version = max(LZMA_VERSION, min_version) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 393 | |
| 394 | self.extract_version = max(min_version, self.extract_version) |
| 395 | self.create_version = max(min_version, self.create_version) |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 396 | filename, flag_bits = self._encodeFilenameFlags() |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 397 | header = struct.pack(structFileHeader, stringFileHeader, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 398 | self.extract_version, self.reserved, flag_bits, |
| 399 | self.compress_type, dostime, dosdate, CRC, |
| 400 | compress_size, file_size, |
| 401 | len(filename), len(extra)) |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 402 | return header + filename + extra |
| 403 | |
| 404 | def _encodeFilenameFlags(self): |
| 405 | try: |
| 406 | return self.filename.encode('ascii'), self.flag_bits |
| 407 | except UnicodeEncodeError: |
| 408 | return self.filename.encode('utf-8'), self.flag_bits | 0x800 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 409 | |
| 410 | def _decodeExtra(self): |
| 411 | # Try to decode the extra field. |
| 412 | extra = self.extra |
| 413 | unpack = struct.unpack |
| 414 | while extra: |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 415 | tp, ln = unpack('<HH', extra[:4]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 416 | if tp == 1: |
| 417 | if ln >= 24: |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 418 | counts = unpack('<QQQ', extra[4:28]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 419 | elif ln == 16: |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 420 | counts = unpack('<QQ', extra[4:20]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 421 | elif ln == 8: |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 422 | counts = unpack('<Q', extra[4:12]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 423 | elif ln == 0: |
| 424 | counts = () |
| 425 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 426 | raise RuntimeError("Corrupt extra field %s"%(ln,)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 427 | |
| 428 | idx = 0 |
| 429 | |
| 430 | # ZIP64 extension (large files and/or large archives) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 431 | if self.file_size in (0xffffffffffffffff, 0xffffffff): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 432 | self.file_size = counts[idx] |
| 433 | idx += 1 |
| 434 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 435 | if self.compress_size == 0xFFFFFFFF: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 436 | self.compress_size = counts[idx] |
| 437 | idx += 1 |
| 438 | |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 439 | if self.header_offset == 0xffffffff: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 440 | old = self.header_offset |
| 441 | self.header_offset = counts[idx] |
| 442 | idx+=1 |
| 443 | |
| 444 | extra = extra[ln+4:] |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 445 | |
| 446 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 447 | class _ZipDecrypter: |
| 448 | """Class to handle decryption of files stored within a ZIP archive. |
| 449 | |
| 450 | ZIP supports a password-based form of encryption. Even though known |
| 451 | plaintext attacks have been found against it, it is still useful |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 452 | to be able to get data out of such a file. |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 453 | |
| 454 | Usage: |
| 455 | zd = _ZipDecrypter(mypwd) |
| 456 | plain_char = zd(cypher_char) |
| 457 | plain_text = map(zd, cypher_text) |
| 458 | """ |
| 459 | |
| 460 | def _GenerateCRCTable(): |
| 461 | """Generate a CRC-32 table. |
| 462 | |
| 463 | ZIP encryption uses the CRC32 one-byte primitive for scrambling some |
| 464 | internal keys. We noticed that a direct implementation is faster than |
| 465 | relying on binascii.crc32(). |
| 466 | """ |
| 467 | poly = 0xedb88320 |
| 468 | table = [0] * 256 |
| 469 | for i in range(256): |
| 470 | crc = i |
| 471 | for j in range(8): |
| 472 | if crc & 1: |
| 473 | crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly |
| 474 | else: |
| 475 | crc = ((crc >> 1) & 0x7FFFFFFF) |
| 476 | table[i] = crc |
| 477 | return table |
Daniel Holth | 9dee304 | 2014-01-02 23:17:21 -0500 | [diff] [blame] | 478 | crctable = None |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 479 | |
| 480 | def _crc32(self, ch, crc): |
| 481 | """Compute the CRC32 primitive on one byte.""" |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 482 | return ((crc >> 8) & 0xffffff) ^ self.crctable[(crc ^ ch) & 0xff] |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 483 | |
| 484 | def __init__(self, pwd): |
Daniel Holth | 9dee304 | 2014-01-02 23:17:21 -0500 | [diff] [blame] | 485 | if _ZipDecrypter.crctable is None: |
| 486 | _ZipDecrypter.crctable = _ZipDecrypter._GenerateCRCTable() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 487 | self.key0 = 305419896 |
| 488 | self.key1 = 591751049 |
| 489 | self.key2 = 878082192 |
| 490 | for p in pwd: |
| 491 | self._UpdateKeys(p) |
| 492 | |
| 493 | def _UpdateKeys(self, c): |
| 494 | self.key0 = self._crc32(c, self.key0) |
| 495 | self.key1 = (self.key1 + (self.key0 & 255)) & 4294967295 |
| 496 | self.key1 = (self.key1 * 134775813 + 1) & 4294967295 |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 497 | self.key2 = self._crc32((self.key1 >> 24) & 255, self.key2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 498 | |
| 499 | def __call__(self, c): |
| 500 | """Decrypt a single character.""" |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 501 | assert isinstance(c, int) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 502 | k = self.key2 | 2 |
| 503 | c = c ^ (((k * (k^1)) >> 8) & 255) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 504 | self._UpdateKeys(c) |
| 505 | return c |
| 506 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 507 | |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 508 | class LZMACompressor: |
| 509 | |
| 510 | def __init__(self): |
| 511 | self._comp = None |
| 512 | |
| 513 | def _init(self): |
Nadeem Vawda | a425c3d | 2012-06-21 23:36:48 +0200 | [diff] [blame] | 514 | props = lzma._encode_filter_properties({'id': lzma.FILTER_LZMA1}) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 515 | self._comp = lzma.LZMACompressor(lzma.FORMAT_RAW, filters=[ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 516 | lzma._decode_filter_properties(lzma.FILTER_LZMA1, props) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 517 | ]) |
| 518 | return struct.pack('<BBH', 9, 4, len(props)) + props |
| 519 | |
| 520 | def compress(self, data): |
| 521 | if self._comp is None: |
| 522 | return self._init() + self._comp.compress(data) |
| 523 | return self._comp.compress(data) |
| 524 | |
| 525 | def flush(self): |
| 526 | if self._comp is None: |
| 527 | return self._init() + self._comp.flush() |
| 528 | return self._comp.flush() |
| 529 | |
| 530 | |
| 531 | class LZMADecompressor: |
| 532 | |
| 533 | def __init__(self): |
| 534 | self._decomp = None |
| 535 | self._unconsumed = b'' |
| 536 | self.eof = False |
| 537 | |
| 538 | def decompress(self, data): |
| 539 | if self._decomp is None: |
| 540 | self._unconsumed += data |
| 541 | if len(self._unconsumed) <= 4: |
| 542 | return b'' |
| 543 | psize, = struct.unpack('<H', self._unconsumed[2:4]) |
| 544 | if len(self._unconsumed) <= 4 + psize: |
| 545 | return b'' |
| 546 | |
| 547 | self._decomp = lzma.LZMADecompressor(lzma.FORMAT_RAW, filters=[ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 548 | lzma._decode_filter_properties(lzma.FILTER_LZMA1, |
| 549 | self._unconsumed[4:4 + psize]) |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 550 | ]) |
| 551 | data = self._unconsumed[4 + psize:] |
| 552 | del self._unconsumed |
| 553 | |
| 554 | result = self._decomp.decompress(data) |
| 555 | self.eof = self._decomp.eof |
| 556 | return result |
| 557 | |
| 558 | |
| 559 | compressor_names = { |
| 560 | 0: 'store', |
| 561 | 1: 'shrink', |
| 562 | 2: 'reduce', |
| 563 | 3: 'reduce', |
| 564 | 4: 'reduce', |
| 565 | 5: 'reduce', |
| 566 | 6: 'implode', |
| 567 | 7: 'tokenize', |
| 568 | 8: 'deflate', |
| 569 | 9: 'deflate64', |
| 570 | 10: 'implode', |
| 571 | 12: 'bzip2', |
| 572 | 14: 'lzma', |
| 573 | 18: 'terse', |
| 574 | 19: 'lz77', |
| 575 | 97: 'wavpack', |
| 576 | 98: 'ppmd', |
| 577 | } |
| 578 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 579 | def _check_compression(compression): |
| 580 | if compression == ZIP_STORED: |
| 581 | pass |
| 582 | elif compression == ZIP_DEFLATED: |
| 583 | if not zlib: |
| 584 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 585 | "Compression requires the (missing) zlib module") |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 586 | elif compression == ZIP_BZIP2: |
| 587 | if not bz2: |
| 588 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 589 | "Compression requires the (missing) bz2 module") |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 590 | elif compression == ZIP_LZMA: |
| 591 | if not lzma: |
| 592 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 593 | "Compression requires the (missing) lzma module") |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 594 | else: |
| 595 | raise RuntimeError("That compression method is not supported") |
| 596 | |
| 597 | |
| 598 | def _get_compressor(compress_type): |
| 599 | if compress_type == ZIP_DEFLATED: |
| 600 | return zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 601 | zlib.DEFLATED, -15) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 602 | elif compress_type == ZIP_BZIP2: |
| 603 | return bz2.BZ2Compressor() |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 604 | elif compress_type == ZIP_LZMA: |
| 605 | return LZMACompressor() |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 606 | else: |
| 607 | return None |
| 608 | |
| 609 | |
| 610 | def _get_decompressor(compress_type): |
Martin v. Löwis | b3260f0 | 2012-05-01 08:38:01 +0200 | [diff] [blame] | 611 | if compress_type == ZIP_STORED: |
| 612 | return None |
| 613 | elif compress_type == ZIP_DEFLATED: |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 614 | return zlib.decompressobj(-15) |
| 615 | elif compress_type == ZIP_BZIP2: |
| 616 | return bz2.BZ2Decompressor() |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 617 | elif compress_type == ZIP_LZMA: |
| 618 | return LZMADecompressor() |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 619 | else: |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 620 | descr = compressor_names.get(compress_type) |
Martin v. Löwis | b3260f0 | 2012-05-01 08:38:01 +0200 | [diff] [blame] | 621 | if descr: |
| 622 | raise NotImplementedError("compression type %d (%s)" % (compress_type, descr)) |
| 623 | else: |
| 624 | raise NotImplementedError("compression type %d" % (compress_type,)) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 625 | |
| 626 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 627 | class ZipExtFile(io.BufferedIOBase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 628 | """File-like object for reading an archive member. |
| 629 | Is returned by ZipFile.open(). |
| 630 | """ |
| 631 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 632 | # Max size supported by decompressor. |
| 633 | MAX_N = 1 << 31 - 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 635 | # Read from compressed files in 4k blocks. |
| 636 | MIN_READ_SIZE = 4096 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 638 | # Search for universal newlines or line chunks. |
| 639 | PATTERN = re.compile(br'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)') |
| 640 | |
Łukasz Langa | e94980a | 2010-11-22 23:31:26 +0000 | [diff] [blame] | 641 | def __init__(self, fileobj, mode, zipinfo, decrypter=None, |
| 642 | close_fileobj=False): |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 643 | self._fileobj = fileobj |
| 644 | self._decrypter = decrypter |
Łukasz Langa | e94980a | 2010-11-22 23:31:26 +0000 | [diff] [blame] | 645 | self._close_fileobj = close_fileobj |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 646 | |
Ezio Melotti | 92b4743 | 2010-01-28 01:44:41 +0000 | [diff] [blame] | 647 | self._compress_type = zipinfo.compress_type |
Ezio Melotti | 92b4743 | 2010-01-28 01:44:41 +0000 | [diff] [blame] | 648 | self._compress_left = zipinfo.compress_size |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 649 | self._left = zipinfo.file_size |
Ezio Melotti | 92b4743 | 2010-01-28 01:44:41 +0000 | [diff] [blame] | 650 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 651 | self._decompressor = _get_decompressor(self._compress_type) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 652 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 653 | self._eof = False |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 654 | self._readbuffer = b'' |
| 655 | self._offset = 0 |
| 656 | |
| 657 | self._universal = 'U' in mode |
| 658 | self.newlines = None |
| 659 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 660 | # Adjust read size for encrypted files since the first 12 bytes |
| 661 | # are for the encryption/password information. |
| 662 | if self._decrypter is not None: |
| 663 | self._compress_left -= 12 |
| 664 | |
| 665 | self.mode = mode |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 666 | self.name = zipinfo.filename |
| 667 | |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 668 | if hasattr(zipinfo, 'CRC'): |
| 669 | self._expected_crc = zipinfo.CRC |
| 670 | self._running_crc = crc32(b'') & 0xffffffff |
| 671 | else: |
| 672 | self._expected_crc = None |
| 673 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 674 | def readline(self, limit=-1): |
| 675 | """Read and return a line from the stream. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 676 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 677 | If limit is specified, at most limit bytes will be read. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 678 | """ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 680 | if not self._universal and limit < 0: |
| 681 | # Shortcut common case - newline found in buffer. |
| 682 | i = self._readbuffer.find(b'\n', self._offset) + 1 |
| 683 | if i > 0: |
| 684 | line = self._readbuffer[self._offset: i] |
| 685 | self._offset = i |
| 686 | return line |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 688 | if not self._universal: |
| 689 | return io.BufferedIOBase.readline(self, limit) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 690 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 691 | line = b'' |
| 692 | while limit < 0 or len(line) < limit: |
| 693 | readahead = self.peek(2) |
| 694 | if readahead == b'': |
| 695 | return line |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 697 | # |
| 698 | # Search for universal newlines or line chunks. |
| 699 | # |
| 700 | # The pattern returns either a line chunk or a newline, but not |
| 701 | # both. Combined with peek(2), we are assured that the sequence |
| 702 | # '\r\n' is always retrieved completely and never split into |
| 703 | # separate newlines - '\r', '\n' due to coincidental readaheads. |
| 704 | # |
| 705 | match = self.PATTERN.search(readahead) |
| 706 | newline = match.group('newline') |
| 707 | if newline is not None: |
| 708 | if self.newlines is None: |
| 709 | self.newlines = [] |
| 710 | if newline not in self.newlines: |
| 711 | self.newlines.append(newline) |
| 712 | self._offset += len(newline) |
| 713 | return line + b'\n' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 714 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 715 | chunk = match.group('chunk') |
| 716 | if limit >= 0: |
| 717 | chunk = chunk[: limit - len(line)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 718 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 719 | self._offset += len(chunk) |
| 720 | line += chunk |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 722 | return line |
| 723 | |
| 724 | def peek(self, n=1): |
| 725 | """Returns buffered bytes without advancing the position.""" |
| 726 | if n > len(self._readbuffer) - self._offset: |
| 727 | chunk = self.read(n) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 728 | if len(chunk) > self._offset: |
| 729 | self._readbuffer = chunk + self._readbuffer[self._offset:] |
| 730 | self._offset = 0 |
| 731 | else: |
| 732 | self._offset -= len(chunk) |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 733 | |
| 734 | # Return up to 512 bytes to reduce allocation overhead for tight loops. |
| 735 | return self._readbuffer[self._offset: self._offset + 512] |
| 736 | |
| 737 | def readable(self): |
| 738 | return True |
| 739 | |
| 740 | def read(self, n=-1): |
| 741 | """Read and return up to n bytes. |
| 742 | If the argument is omitted, None, or negative, data is read and returned until EOF is reached.. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 743 | """ |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 744 | if n is None or n < 0: |
| 745 | buf = self._readbuffer[self._offset:] |
| 746 | self._readbuffer = b'' |
| 747 | self._offset = 0 |
| 748 | while not self._eof: |
| 749 | buf += self._read1(self.MAX_N) |
| 750 | return buf |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | 78157b3 | 2012-06-23 16:44:48 +0200 | [diff] [blame] | 752 | end = n + self._offset |
| 753 | if end < len(self._readbuffer): |
| 754 | buf = self._readbuffer[self._offset:end] |
| 755 | self._offset = end |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 756 | return buf |
| 757 | |
Antoine Pitrou | 78157b3 | 2012-06-23 16:44:48 +0200 | [diff] [blame] | 758 | n = end - len(self._readbuffer) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 759 | buf = self._readbuffer[self._offset:] |
| 760 | self._readbuffer = b'' |
| 761 | self._offset = 0 |
| 762 | while n > 0 and not self._eof: |
| 763 | data = self._read1(n) |
| 764 | if n < len(data): |
| 765 | self._readbuffer = data |
| 766 | self._offset = n |
| 767 | buf += data[:n] |
| 768 | break |
| 769 | buf += data |
| 770 | n -= len(data) |
| 771 | return buf |
| 772 | |
| 773 | def _update_crc(self, newdata): |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 774 | # Update the CRC using the given data. |
| 775 | if self._expected_crc is None: |
| 776 | # No need to compute the CRC if we don't have a reference value |
| 777 | return |
| 778 | self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff |
| 779 | # Check the CRC if we're at the end of the file |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 780 | if self._eof and self._running_crc != self._expected_crc: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 781 | raise BadZipFile("Bad CRC-32 for file %r" % self.name) |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 782 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 783 | def read1(self, n): |
| 784 | """Read up to n bytes with at most one read() system call.""" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 785 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 786 | if n is None or n < 0: |
| 787 | buf = self._readbuffer[self._offset:] |
| 788 | self._readbuffer = b'' |
| 789 | self._offset = 0 |
Serhiy Storchaka | d2c07a5 | 2013-09-27 22:11:57 +0300 | [diff] [blame] | 790 | while not self._eof: |
| 791 | data = self._read1(self.MAX_N) |
| 792 | if data: |
| 793 | buf += data |
| 794 | break |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 795 | return buf |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 796 | |
Antoine Pitrou | 78157b3 | 2012-06-23 16:44:48 +0200 | [diff] [blame] | 797 | end = n + self._offset |
| 798 | if end < len(self._readbuffer): |
| 799 | buf = self._readbuffer[self._offset:end] |
| 800 | self._offset = end |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 801 | return buf |
| 802 | |
Antoine Pitrou | 78157b3 | 2012-06-23 16:44:48 +0200 | [diff] [blame] | 803 | n = end - len(self._readbuffer) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 804 | buf = self._readbuffer[self._offset:] |
| 805 | self._readbuffer = b'' |
| 806 | self._offset = 0 |
| 807 | if n > 0: |
Serhiy Storchaka | d2c07a5 | 2013-09-27 22:11:57 +0300 | [diff] [blame] | 808 | while not self._eof: |
| 809 | data = self._read1(n) |
| 810 | if n < len(data): |
| 811 | self._readbuffer = data |
| 812 | self._offset = n |
| 813 | buf += data[:n] |
| 814 | break |
| 815 | if data: |
| 816 | buf += data |
| 817 | break |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 818 | return buf |
| 819 | |
| 820 | def _read1(self, n): |
| 821 | # Read up to n compressed bytes with at most one read() system call, |
| 822 | # decrypt and decompress them. |
| 823 | if self._eof or n <= 0: |
| 824 | return b'' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 825 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 826 | # Read from file. |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 827 | if self._compress_type == ZIP_DEFLATED: |
| 828 | ## Handle unconsumed data. |
| 829 | data = self._decompressor.unconsumed_tail |
| 830 | if n > len(data): |
| 831 | data += self._read2(n - len(data)) |
| 832 | else: |
| 833 | data = self._read2(n) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 834 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 835 | if self._compress_type == ZIP_STORED: |
| 836 | self._eof = self._compress_left <= 0 |
| 837 | elif self._compress_type == ZIP_DEFLATED: |
| 838 | n = max(n, self.MIN_READ_SIZE) |
| 839 | data = self._decompressor.decompress(data, n) |
| 840 | self._eof = (self._decompressor.eof or |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 841 | self._compress_left <= 0 and |
| 842 | not self._decompressor.unconsumed_tail) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 843 | if self._eof: |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 844 | data += self._decompressor.flush() |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 845 | else: |
| 846 | data = self._decompressor.decompress(data) |
| 847 | self._eof = self._decompressor.eof or self._compress_left <= 0 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 848 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 849 | data = data[:self._left] |
| 850 | self._left -= len(data) |
| 851 | if self._left <= 0: |
| 852 | self._eof = True |
| 853 | self._update_crc(data) |
| 854 | return data |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 855 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 856 | def _read2(self, n): |
| 857 | if self._compress_left <= 0: |
| 858 | return b'' |
| 859 | |
| 860 | n = max(n, self.MIN_READ_SIZE) |
| 861 | n = min(n, self._compress_left) |
| 862 | |
| 863 | data = self._fileobj.read(n) |
| 864 | self._compress_left -= len(data) |
Serhiy Storchaka | 5ce3f10 | 2014-01-09 14:50:20 +0200 | [diff] [blame] | 865 | if not data: |
| 866 | raise EOFError |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 867 | |
| 868 | if self._decrypter is not None: |
| 869 | data = bytes(map(self._decrypter, data)) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 870 | return data |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 871 | |
Łukasz Langa | e94980a | 2010-11-22 23:31:26 +0000 | [diff] [blame] | 872 | def close(self): |
| 873 | try: |
| 874 | if self._close_fileobj: |
| 875 | self._fileobj.close() |
| 876 | finally: |
| 877 | super().close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | a32f9a2 | 2010-01-27 21:18:57 +0000 | [diff] [blame] | 879 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 880 | class ZipFile: |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 881 | """ Class with methods to open, read, write, close, list zip files. |
| 882 | |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 883 | z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True) |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 884 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 885 | file: Either the path to the file, or a file-like object. |
| 886 | If it is a path, the file will be opened and closed by ZipFile. |
| 887 | mode: The mode can be either read "r", write "w" or append "a". |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 888 | compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib), |
| 889 | ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma). |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 890 | allowZip64: if True ZipFile will create files with ZIP64 extensions when |
| 891 | needed, otherwise it will raise an exception when this would |
| 892 | be necessary. |
| 893 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 894 | """ |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 895 | |
Fred Drake | 90eac28 | 2001-02-28 05:29:34 +0000 | [diff] [blame] | 896 | fp = None # Set here since __del__ checks it |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 897 | _windows_illegal_name_trans_table = None |
Fred Drake | 90eac28 | 2001-02-28 05:29:34 +0000 | [diff] [blame] | 898 | |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 899 | def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 900 | """Open the ZIP file with mode read "r", write "w" or append "a".""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 901 | if mode not in ("r", "w", "a"): |
| 902 | raise RuntimeError('ZipFile() requires mode "r", "w", or "a"') |
| 903 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 904 | _check_compression(compression) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 905 | |
| 906 | self._allowZip64 = allowZip64 |
| 907 | self._didModify = False |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 908 | self.debug = 0 # Level of printing: 0 through 3 |
| 909 | self.NameToInfo = {} # Find file info given name |
| 910 | self.filelist = [] # List of ZipInfo instances for archive |
| 911 | self.compression = compression # Method of compression |
Raymond Hettinger | 2ca7c19 | 2005-02-16 09:27:49 +0000 | [diff] [blame] | 912 | self.mode = key = mode.replace('b', '')[0] |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 913 | self.pwd = None |
R David Murray | f50b38a | 2012-04-12 18:44:58 -0400 | [diff] [blame] | 914 | self._comment = b'' |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 915 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 916 | # Check if we were passed a file-like object |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 917 | if isinstance(file, str): |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 918 | # No, it's a filename |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 919 | self._filePassed = 0 |
| 920 | self.filename = file |
| 921 | modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'} |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 922 | try: |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 923 | self.fp = io.open(file, modeDict[mode]) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 924 | except OSError: |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 925 | if mode == 'a': |
| 926 | mode = key = 'w' |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 927 | self.fp = io.open(file, modeDict[mode]) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 928 | else: |
| 929 | raise |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 930 | else: |
| 931 | self._filePassed = 1 |
| 932 | self.fp = file |
| 933 | self.filename = getattr(file, 'name', None) |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 934 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 935 | try: |
| 936 | if key == 'r': |
Martin v. Löwis | 6f6873b | 2002-10-13 13:54:50 +0000 | [diff] [blame] | 937 | self._RealGetContents() |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 938 | elif key == 'w': |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 939 | # set the modified flag so central directory gets written |
| 940 | # even if no files are added to the archive |
| 941 | self._didModify = True |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 942 | elif key == 'a': |
| 943 | try: |
| 944 | # See if file is a zip file |
| 945 | self._RealGetContents() |
| 946 | # seek to start of directory and overwrite |
| 947 | self.fp.seek(self.start_dir, 0) |
| 948 | except BadZipFile: |
| 949 | # file is not a zip file, just append |
| 950 | self.fp.seek(0, 2) |
| 951 | |
| 952 | # set the modified flag so central directory gets written |
| 953 | # even if no files are added to the archive |
| 954 | self._didModify = True |
| 955 | else: |
| 956 | raise RuntimeError('Mode must be "r", "w" or "a"') |
| 957 | except: |
| 958 | fp = self.fp |
| 959 | self.fp = None |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 960 | if not self._filePassed: |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 961 | fp.close() |
| 962 | raise |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 963 | |
Ezio Melotti | faa6b7f | 2009-12-30 12:34:59 +0000 | [diff] [blame] | 964 | def __enter__(self): |
| 965 | return self |
| 966 | |
| 967 | def __exit__(self, type, value, traceback): |
| 968 | self.close() |
| 969 | |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 970 | def _RealGetContents(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 971 | """Read in the table of contents for the ZIP file.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 972 | fp = self.fp |
Georg Brandl | 268e4d4 | 2010-10-14 06:59:45 +0000 | [diff] [blame] | 973 | try: |
| 974 | endrec = _EndRecData(fp) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 975 | except OSError: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 976 | raise BadZipFile("File is not a zip file") |
Martin v. Löwis | 6f6873b | 2002-10-13 13:54:50 +0000 | [diff] [blame] | 977 | if not endrec: |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 978 | raise BadZipFile("File is not a zip file") |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 979 | if self.debug > 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 980 | print(endrec) |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 981 | size_cd = endrec[_ECD_SIZE] # bytes in central directory |
| 982 | offset_cd = endrec[_ECD_OFFSET] # offset of central directory |
R David Murray | f50b38a | 2012-04-12 18:44:58 -0400 | [diff] [blame] | 983 | self._comment = endrec[_ECD_COMMENT] # archive comment |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 984 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 985 | # "concat" is zero, unless zip was concatenated to another file |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 986 | concat = endrec[_ECD_LOCATION] - size_cd - offset_cd |
Antoine Pitrou | 9e4fdf4 | 2008-09-05 23:43:02 +0000 | [diff] [blame] | 987 | if endrec[_ECD_SIGNATURE] == stringEndArchive64: |
| 988 | # If Zip64 extension structures are present, account for them |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 989 | concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator) |
| 990 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 991 | if self.debug > 2: |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 992 | inferred = concat + offset_cd |
| 993 | print("given, inferred, offset", offset_cd, inferred, concat) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 994 | # self.start_dir: Position of start of central directory |
| 995 | self.start_dir = offset_cd + concat |
| 996 | fp.seek(self.start_dir, 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 997 | data = fp.read(size_cd) |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 998 | fp = io.BytesIO(data) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 999 | total = 0 |
| 1000 | while total < size_cd: |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1001 | centdir = fp.read(sizeCentralDir) |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 1002 | if len(centdir) != sizeCentralDir: |
| 1003 | raise BadZipFile("Truncated central directory") |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1004 | centdir = struct.unpack(structCentralDir, centdir) |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 1005 | if centdir[_CD_SIGNATURE] != stringCentralDir: |
| 1006 | raise BadZipFile("Bad magic number for central directory") |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1007 | if self.debug > 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1008 | print(centdir) |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 1009 | filename = fp.read(centdir[_CD_FILENAME_LENGTH]) |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 1010 | flags = centdir[5] |
| 1011 | if flags & 0x800: |
| 1012 | # UTF-8 file names extension |
| 1013 | filename = filename.decode('utf-8') |
| 1014 | else: |
| 1015 | # Historical ZIP filename encoding |
| 1016 | filename = filename.decode('cp437') |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1017 | # Create ZipInfo instance to store file information |
Martin v. Löwis | 8570f6a | 2008-05-05 17:44:38 +0000 | [diff] [blame] | 1018 | x = ZipInfo(filename) |
Fred Drake | 3e038e5 | 2001-02-28 17:56:26 +0000 | [diff] [blame] | 1019 | x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH]) |
| 1020 | x.comment = fp.read(centdir[_CD_COMMENT_LENGTH]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1021 | x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1022 | (x.create_version, x.create_system, x.extract_version, x.reserved, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1023 | x.flag_bits, x.compress_type, t, d, |
| 1024 | x.CRC, x.compress_size, x.file_size) = centdir[1:12] |
Martin v. Löwis | d099b56 | 2012-05-01 14:08:22 +0200 | [diff] [blame] | 1025 | if x.extract_version > MAX_EXTRACT_VERSION: |
| 1026 | raise NotImplementedError("zip file version %.1f" % |
| 1027 | (x.extract_version / 10)) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1028 | x.volume, x.internal_attr, x.external_attr = centdir[15:18] |
| 1029 | # Convert date/time code to (year, month, day, hour, min, sec) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 1030 | x._raw_time = t |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1031 | x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1032 | t>>11, (t>>5)&0x3F, (t&0x1F) * 2 ) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1033 | |
| 1034 | x._decodeExtra() |
| 1035 | x.header_offset = x.header_offset + concat |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1036 | self.filelist.append(x) |
| 1037 | self.NameToInfo[x.filename] = x |
Martin v. Löwis | b09b844 | 2008-07-03 14:13:42 +0000 | [diff] [blame] | 1038 | |
| 1039 | # update total bytes read from central directory |
| 1040 | total = (total + sizeCentralDir + centdir[_CD_FILENAME_LENGTH] |
| 1041 | + centdir[_CD_EXTRA_FIELD_LENGTH] |
| 1042 | + centdir[_CD_COMMENT_LENGTH]) |
| 1043 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1044 | if self.debug > 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1045 | print("total", total) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1046 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1047 | |
| 1048 | def namelist(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1049 | """Return a list of file names in the archive.""" |
Ezio Melotti | 006917e | 2012-04-16 21:34:24 -0600 | [diff] [blame] | 1050 | return [data.filename for data in self.filelist] |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1051 | |
| 1052 | def infolist(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1053 | """Return a list of class ZipInfo instances for files in the |
| 1054 | archive.""" |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1055 | return self.filelist |
| 1056 | |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1057 | def printdir(self, file=None): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1058 | """Print a table of contents for the zip file.""" |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1059 | print("%-46s %19s %12s" % ("File Name", "Modified ", "Size"), |
| 1060 | file=file) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1061 | for zinfo in self.filelist: |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 1062 | date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6] |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1063 | print("%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size), |
| 1064 | file=file) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1065 | |
| 1066 | def testzip(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1067 | """Read all the files and check the CRC.""" |
Benjamin Peterson | 4cd6a95 | 2008-08-17 20:23:46 +0000 | [diff] [blame] | 1068 | chunk_size = 2 ** 20 |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1069 | for zinfo in self.filelist: |
| 1070 | try: |
Benjamin Peterson | 4cd6a95 | 2008-08-17 20:23:46 +0000 | [diff] [blame] | 1071 | # Read by chunks, to avoid an OverflowError or a |
| 1072 | # MemoryError with very large embedded files. |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1073 | with self.open(zinfo.filename, "r") as f: |
| 1074 | while f.read(chunk_size): # Check CRC-32 |
| 1075 | pass |
Georg Brandl | 4d54088 | 2010-10-28 06:42:33 +0000 | [diff] [blame] | 1076 | except BadZipFile: |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1077 | return zinfo.filename |
| 1078 | |
| 1079 | def getinfo(self, name): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1080 | """Return the instance of ZipInfo given 'name'.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1081 | info = self.NameToInfo.get(name) |
| 1082 | if info is None: |
| 1083 | raise KeyError( |
| 1084 | 'There is no item named %r in the archive' % name) |
| 1085 | |
| 1086 | return info |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1087 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1088 | def setpassword(self, pwd): |
| 1089 | """Set default password for encrypted files.""" |
R. David Murray | 8d855d8 | 2010-12-21 21:53:37 +0000 | [diff] [blame] | 1090 | if pwd and not isinstance(pwd, bytes): |
| 1091 | raise TypeError("pwd: expected bytes, got %s" % type(pwd)) |
| 1092 | if pwd: |
| 1093 | self.pwd = pwd |
| 1094 | else: |
| 1095 | self.pwd = None |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1096 | |
R David Murray | f50b38a | 2012-04-12 18:44:58 -0400 | [diff] [blame] | 1097 | @property |
| 1098 | def comment(self): |
| 1099 | """The comment text associated with the ZIP file.""" |
| 1100 | return self._comment |
| 1101 | |
| 1102 | @comment.setter |
| 1103 | def comment(self, comment): |
| 1104 | if not isinstance(comment, bytes): |
| 1105 | raise TypeError("comment: expected bytes, got %s" % type(comment)) |
| 1106 | # check for valid comment length |
Serhiy Storchaka | 9b7a1a1 | 2014-01-20 21:57:40 +0200 | [diff] [blame] | 1107 | if len(comment) > ZIP_MAX_COMMENT: |
| 1108 | import warnings |
| 1109 | warnings.warn('Archive comment is too long; truncating to %d bytes' |
| 1110 | % ZIP_MAX_COMMENT, stacklevel=2) |
R David Murray | f50b38a | 2012-04-12 18:44:58 -0400 | [diff] [blame] | 1111 | comment = comment[:ZIP_MAX_COMMENT] |
| 1112 | self._comment = comment |
| 1113 | self._didModify = True |
| 1114 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1115 | def read(self, name, pwd=None): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1116 | """Return file bytes (as a string) for name.""" |
Benjamin Peterson | d285bdb | 2010-10-31 17:57:22 +0000 | [diff] [blame] | 1117 | with self.open(name, "r", pwd) as fp: |
| 1118 | return fp.read() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1119 | |
| 1120 | def open(self, name, mode="r", pwd=None): |
| 1121 | """Return file-like object for 'name'.""" |
| 1122 | if mode not in ("r", "U", "rU"): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 1123 | raise RuntimeError('open() requires mode "r", "U", or "rU"') |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 1124 | if 'U' in mode: |
| 1125 | import warnings |
| 1126 | warnings.warn("'U' mode is deprecated", |
| 1127 | DeprecationWarning, 2) |
R. David Murray | 8d855d8 | 2010-12-21 21:53:37 +0000 | [diff] [blame] | 1128 | if pwd and not isinstance(pwd, bytes): |
| 1129 | raise TypeError("pwd: expected bytes, got %s" % type(pwd)) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1130 | if not self.fp: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 1131 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1132 | "Attempt to read ZIP archive that was already closed") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1133 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1134 | # Only open a new file for instances where we were not |
| 1135 | # given a file object in the constructor |
| 1136 | if self._filePassed: |
| 1137 | zef_file = self.fp |
| 1138 | else: |
Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 1139 | zef_file = io.open(self.filename, 'rb') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1140 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1141 | try: |
| 1142 | # Make sure we have an info object |
| 1143 | if isinstance(name, ZipInfo): |
| 1144 | # 'name' is already an info object |
| 1145 | zinfo = name |
| 1146 | else: |
| 1147 | # Get info object for name |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 1148 | zinfo = self.getinfo(name) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1149 | zef_file.seek(zinfo.header_offset, 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1150 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1151 | # Skip the file header: |
| 1152 | fheader = zef_file.read(sizeFileHeader) |
Serhiy Storchaka | d2b1527 | 2013-01-31 15:27:07 +0200 | [diff] [blame] | 1153 | if len(fheader) != sizeFileHeader: |
| 1154 | raise BadZipFile("Truncated file header") |
| 1155 | fheader = struct.unpack(structFileHeader, fheader) |
| 1156 | if fheader[_FH_SIGNATURE] != stringFileHeader: |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1157 | raise BadZipFile("Bad magic number for file header") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1158 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1159 | fname = zef_file.read(fheader[_FH_FILENAME_LENGTH]) |
| 1160 | if fheader[_FH_EXTRA_FIELD_LENGTH]: |
| 1161 | zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | 8572da5 | 2012-11-17 23:52:05 +0100 | [diff] [blame] | 1163 | if zinfo.flag_bits & 0x20: |
| 1164 | # Zip 2.7: compressed patched data |
| 1165 | raise NotImplementedError("compressed patched data (flag bit 5)") |
Martin v. Löwis | 2a2ce32 | 2012-05-01 08:44:08 +0200 | [diff] [blame] | 1166 | |
Antoine Pitrou | 8572da5 | 2012-11-17 23:52:05 +0100 | [diff] [blame] | 1167 | if zinfo.flag_bits & 0x40: |
| 1168 | # strong encryption |
| 1169 | raise NotImplementedError("strong encryption (flag bit 6)") |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1170 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1171 | if zinfo.flag_bits & 0x800: |
| 1172 | # UTF-8 filename |
| 1173 | fname_str = fname.decode("utf-8") |
| 1174 | else: |
| 1175 | fname_str = fname.decode("cp437") |
Georg Brandl | 5ba11de | 2011-01-01 10:09:32 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1177 | if fname_str != zinfo.orig_filename: |
| 1178 | raise BadZipFile( |
| 1179 | 'File name in directory %r and header %r differ.' |
| 1180 | % (zinfo.orig_filename, fname)) |
| 1181 | |
| 1182 | # check for encrypted flag & handle password |
| 1183 | is_encrypted = zinfo.flag_bits & 0x1 |
| 1184 | zd = None |
| 1185 | if is_encrypted: |
| 1186 | if not pwd: |
| 1187 | pwd = self.pwd |
| 1188 | if not pwd: |
| 1189 | raise RuntimeError("File %s is encrypted, password " |
| 1190 | "required for extraction" % name) |
| 1191 | |
| 1192 | zd = _ZipDecrypter(pwd) |
| 1193 | # The first 12 bytes in the cypher stream is an encryption header |
| 1194 | # used to strengthen the algorithm. The first 11 bytes are |
| 1195 | # completely random, while the 12th contains the MSB of the CRC, |
| 1196 | # or the MSB of the file time depending on the header type |
| 1197 | # and is used to check the correctness of the password. |
| 1198 | header = zef_file.read(12) |
| 1199 | h = list(map(zd, header[0:12])) |
| 1200 | if zinfo.flag_bits & 0x8: |
| 1201 | # compare against the file type from extended local headers |
| 1202 | check_byte = (zinfo._raw_time >> 8) & 0xff |
| 1203 | else: |
| 1204 | # compare against the CRC otherwise |
| 1205 | check_byte = (zinfo.CRC >> 24) & 0xff |
| 1206 | if h[11] != check_byte: |
| 1207 | raise RuntimeError("Bad password for file", name) |
| 1208 | |
| 1209 | return ZipExtFile(zef_file, mode, zinfo, zd, |
| 1210 | close_fileobj=not self._filePassed) |
| 1211 | except: |
Łukasz Langa | a9f054b | 2010-11-23 00:15:02 +0000 | [diff] [blame] | 1212 | if not self._filePassed: |
| 1213 | zef_file.close() |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1214 | raise |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1215 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1216 | def extract(self, member, path=None, pwd=None): |
| 1217 | """Extract a member from the archive to the current working directory, |
| 1218 | using its full name. Its file information is extracted as accurately |
| 1219 | as possible. `member' may be a filename or a ZipInfo object. You can |
| 1220 | specify a different directory using `path'. |
| 1221 | """ |
| 1222 | if not isinstance(member, ZipInfo): |
| 1223 | member = self.getinfo(member) |
| 1224 | |
| 1225 | if path is None: |
| 1226 | path = os.getcwd() |
| 1227 | |
| 1228 | return self._extract_member(member, path, pwd) |
| 1229 | |
| 1230 | def extractall(self, path=None, members=None, pwd=None): |
| 1231 | """Extract all members from the archive to the current working |
| 1232 | directory. `path' specifies a different directory to extract to. |
| 1233 | `members' is optional and must be a subset of the list returned |
| 1234 | by namelist(). |
| 1235 | """ |
| 1236 | if members is None: |
| 1237 | members = self.namelist() |
| 1238 | |
| 1239 | for zipinfo in members: |
| 1240 | self.extract(zipinfo, path, pwd) |
| 1241 | |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1242 | @classmethod |
| 1243 | def _sanitize_windows_name(cls, arcname, pathsep): |
| 1244 | """Replace bad characters and remove trailing dots from parts.""" |
| 1245 | table = cls._windows_illegal_name_trans_table |
| 1246 | if not table: |
| 1247 | illegal = ':<>|"?*' |
| 1248 | table = str.maketrans(illegal, '_' * len(illegal)) |
| 1249 | cls._windows_illegal_name_trans_table = table |
| 1250 | arcname = arcname.translate(table) |
| 1251 | # remove trailing dots |
| 1252 | arcname = (x.rstrip('.') for x in arcname.split(pathsep)) |
| 1253 | # rejoin, removing empty parts. |
| 1254 | arcname = pathsep.join(x for x in arcname if x) |
| 1255 | return arcname |
| 1256 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1257 | def _extract_member(self, member, targetpath, pwd): |
| 1258 | """Extract the ZipInfo object 'member' to a physical |
| 1259 | file on the path targetpath. |
| 1260 | """ |
| 1261 | # build the destination pathname, replacing |
| 1262 | # forward slashes to platform specific separators. |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1263 | arcname = member.filename.replace('/', os.path.sep) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1264 | |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1265 | if os.path.altsep: |
| 1266 | arcname = arcname.replace(os.path.altsep, os.path.sep) |
| 1267 | # interpret absolute pathname as relative, remove drive letter or |
| 1268 | # UNC path, redundant separators, "." and ".." components. |
| 1269 | arcname = os.path.splitdrive(arcname)[1] |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1270 | invalid_path_parts = ('', os.path.curdir, os.path.pardir) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1271 | arcname = os.path.sep.join(x for x in arcname.split(os.path.sep) |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1272 | if x not in invalid_path_parts) |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1273 | if os.path.sep == '\\': |
Serhiy Storchaka | e5e6444 | 2013-02-02 19:50:59 +0200 | [diff] [blame] | 1274 | # filter illegal characters on Windows |
Gregory P. Smith | 09aa752 | 2013-02-03 00:36:32 -0800 | [diff] [blame] | 1275 | arcname = self._sanitize_windows_name(arcname, os.path.sep) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1276 | |
Gregory P. Smith | b47acbf | 2013-02-01 11:22:43 -0800 | [diff] [blame] | 1277 | targetpath = os.path.join(targetpath, arcname) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1278 | targetpath = os.path.normpath(targetpath) |
| 1279 | |
| 1280 | # Create all upper directories if necessary. |
| 1281 | upperdirs = os.path.dirname(targetpath) |
| 1282 | if upperdirs and not os.path.exists(upperdirs): |
| 1283 | os.makedirs(upperdirs) |
| 1284 | |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1285 | if member.filename[-1] == '/': |
Martin v. Löwis | 70ccd16 | 2009-05-24 19:47:22 +0000 | [diff] [blame] | 1286 | if not os.path.isdir(targetpath): |
| 1287 | os.mkdir(targetpath) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1288 | return targetpath |
| 1289 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1290 | with self.open(member, pwd=pwd) as source, \ |
| 1291 | open(targetpath, "wb") as target: |
| 1292 | shutil.copyfileobj(source, target) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1293 | |
| 1294 | return targetpath |
| 1295 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1296 | def _writecheck(self, zinfo): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1297 | """Check for errors before writing a file to the archive.""" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1298 | if zinfo.filename in self.NameToInfo: |
Serhiy Storchaka | 9b7a1a1 | 2014-01-20 21:57:40 +0200 | [diff] [blame] | 1299 | import warnings |
| 1300 | warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1301 | if self.mode not in ("w", "a"): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 1302 | raise RuntimeError('write() requires mode "w" or "a"') |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1303 | if not self.fp: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 1304 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1305 | "Attempt to write ZIP archive that was already closed") |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1306 | _check_compression(zinfo.compress_type) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1307 | if zinfo.file_size > ZIP64_LIMIT: |
| 1308 | if not self._allowZip64: |
| 1309 | raise LargeZipFile("Filesize would require ZIP64 extensions") |
| 1310 | if zinfo.header_offset > ZIP64_LIMIT: |
| 1311 | if not self._allowZip64: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 1312 | raise LargeZipFile( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1313 | "Zipfile size would require ZIP64 extensions") |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1314 | |
| 1315 | def write(self, filename, arcname=None, compress_type=None): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1316 | """Put the bytes from filename into the archive under the name |
| 1317 | arcname.""" |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1318 | if not self.fp: |
| 1319 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1320 | "Attempt to write to ZIP archive that was already closed") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1321 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1322 | st = os.stat(filename) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1323 | isdir = stat.S_ISDIR(st.st_mode) |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 1324 | mtime = time.localtime(st.st_mtime) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1325 | date_time = mtime[0:6] |
| 1326 | # Create ZipInfo instance to store file information |
| 1327 | if arcname is None: |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 1328 | arcname = filename |
| 1329 | arcname = os.path.normpath(os.path.splitdrive(arcname)[1]) |
| 1330 | while arcname[0] in (os.sep, os.altsep): |
| 1331 | arcname = arcname[1:] |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1332 | if isdir: |
| 1333 | arcname += '/' |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame] | 1334 | zinfo = ZipInfo(arcname, date_time) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 1335 | zinfo.external_attr = (st[0] & 0xFFFF) << 16 # Unix attributes |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1336 | if compress_type is None: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 1337 | zinfo.compress_type = self.compression |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1338 | else: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 1339 | zinfo.compress_type = compress_type |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1340 | |
| 1341 | zinfo.file_size = st.st_size |
Finn Bock | 03a3bb8 | 2001-09-05 18:40:33 +0000 | [diff] [blame] | 1342 | zinfo.flag_bits = 0x00 |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 1343 | zinfo.header_offset = self.fp.tell() # Start of header bytes |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1344 | if zinfo.compress_type == ZIP_LZMA: |
| 1345 | # Compressed data includes an end-of-stream (EOS) marker |
| 1346 | zinfo.flag_bits |= 0x02 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1347 | |
| 1348 | self._writecheck(zinfo) |
| 1349 | self._didModify = True |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1350 | |
| 1351 | if isdir: |
| 1352 | zinfo.file_size = 0 |
| 1353 | zinfo.compress_size = 0 |
| 1354 | zinfo.CRC = 0 |
| 1355 | self.filelist.append(zinfo) |
| 1356 | self.NameToInfo[zinfo.filename] = zinfo |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1357 | self.fp.write(zinfo.FileHeader(False)) |
Martin v. Löwis | 59e4779 | 2009-01-24 14:10:07 +0000 | [diff] [blame] | 1358 | return |
| 1359 | |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1360 | cmpr = _get_compressor(zinfo.compress_type) |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 1361 | with open(filename, "rb") as fp: |
| 1362 | # Must overwrite CRC and sizes with correct data later |
| 1363 | zinfo.CRC = CRC = 0 |
| 1364 | zinfo.compress_size = compress_size = 0 |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1365 | # Compressed size can be larger than uncompressed size |
| 1366 | zip64 = self._allowZip64 and \ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1367 | zinfo.file_size * 1.05 > ZIP64_LIMIT |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1368 | self.fp.write(zinfo.FileHeader(zip64)) |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1369 | file_size = 0 |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 1370 | while 1: |
| 1371 | buf = fp.read(1024 * 8) |
| 1372 | if not buf: |
| 1373 | break |
| 1374 | file_size = file_size + len(buf) |
| 1375 | CRC = crc32(buf, CRC) & 0xffffffff |
| 1376 | if cmpr: |
| 1377 | buf = cmpr.compress(buf) |
| 1378 | compress_size = compress_size + len(buf) |
| 1379 | self.fp.write(buf) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1380 | if cmpr: |
| 1381 | buf = cmpr.flush() |
| 1382 | compress_size = compress_size + len(buf) |
| 1383 | self.fp.write(buf) |
| 1384 | zinfo.compress_size = compress_size |
| 1385 | else: |
| 1386 | zinfo.compress_size = file_size |
| 1387 | zinfo.CRC = CRC |
| 1388 | zinfo.file_size = file_size |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1389 | if not zip64 and self._allowZip64: |
| 1390 | if file_size > ZIP64_LIMIT: |
| 1391 | raise RuntimeError('File size has increased during compressing') |
| 1392 | if compress_size > ZIP64_LIMIT: |
| 1393 | raise RuntimeError('Compressed size larger than uncompressed size') |
| 1394 | # Seek backwards and write file header (which will now include |
| 1395 | # correct CRC and file sizes) |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 1396 | position = self.fp.tell() # Preserve current position in file |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1397 | self.fp.seek(zinfo.header_offset, 0) |
| 1398 | self.fp.write(zinfo.FileHeader(zip64)) |
Finn Bock | 03a3bb8 | 2001-09-05 18:40:33 +0000 | [diff] [blame] | 1399 | self.fp.seek(position, 0) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1400 | self.filelist.append(zinfo) |
| 1401 | self.NameToInfo[zinfo.filename] = zinfo |
| 1402 | |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 1403 | def writestr(self, zinfo_or_arcname, data, compress_type=None): |
Guido van Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 1404 | """Write a file into the archive. The contents is 'data', which |
| 1405 | may be either a 'str' or a 'bytes' instance; if it is a 'str', |
| 1406 | it is encoded as UTF-8 first. |
| 1407 | 'zinfo_or_arcname' is either a ZipInfo instance or |
Just van Rossum | b083cb3 | 2002-12-12 12:23:32 +0000 | [diff] [blame] | 1408 | the name of the file in the archive.""" |
Guido van Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 1409 | if isinstance(data, str): |
| 1410 | data = data.encode("utf-8") |
Just van Rossum | b083cb3 | 2002-12-12 12:23:32 +0000 | [diff] [blame] | 1411 | if not isinstance(zinfo_or_arcname, ZipInfo): |
| 1412 | zinfo = ZipInfo(filename=zinfo_or_arcname, |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 1413 | date_time=time.localtime(time.time())[:6]) |
Just van Rossum | b083cb3 | 2002-12-12 12:23:32 +0000 | [diff] [blame] | 1414 | zinfo.compress_type = self.compression |
Antoine Pitrou | 6e1df8d | 2008-07-25 19:58:18 +0000 | [diff] [blame] | 1415 | zinfo.external_attr = 0o600 << 16 |
Just van Rossum | b083cb3 | 2002-12-12 12:23:32 +0000 | [diff] [blame] | 1416 | else: |
| 1417 | zinfo = zinfo_or_arcname |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1418 | |
| 1419 | if not self.fp: |
| 1420 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1421 | "Attempt to write to ZIP archive that was already closed") |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1422 | |
Guido van Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 1423 | zinfo.file_size = len(data) # Uncompressed size |
| 1424 | zinfo.header_offset = self.fp.tell() # Start of header data |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 1425 | if compress_type is not None: |
| 1426 | zinfo.compress_type = compress_type |
Martin v. Löwis | 7fb79fc | 2012-05-13 10:06:36 +0200 | [diff] [blame] | 1427 | if zinfo.compress_type == ZIP_LZMA: |
| 1428 | # Compressed data includes an end-of-stream (EOS) marker |
| 1429 | zinfo.flag_bits |= 0x02 |
Ronald Oussoren | ee5c885 | 2010-02-07 20:24:02 +0000 | [diff] [blame] | 1430 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1431 | self._writecheck(zinfo) |
| 1432 | self._didModify = True |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 1433 | zinfo.CRC = crc32(data) & 0xffffffff # CRC-32 checksum |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1434 | co = _get_compressor(zinfo.compress_type) |
| 1435 | if co: |
Guido van Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 1436 | data = co.compress(data) + co.flush() |
| 1437 | zinfo.compress_size = len(data) # Compressed size |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1438 | else: |
| 1439 | zinfo.compress_size = zinfo.file_size |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1440 | zip64 = zinfo.file_size > ZIP64_LIMIT or \ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1441 | zinfo.compress_size > ZIP64_LIMIT |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1442 | if zip64 and not self._allowZip64: |
| 1443 | raise LargeZipFile("Filesize would require ZIP64 extensions") |
| 1444 | self.fp.write(zinfo.FileHeader(zip64)) |
Guido van Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 1445 | self.fp.write(data) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1446 | if zinfo.flag_bits & 0x08: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 1447 | # Write CRC and file sizes after the file data |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1448 | fmt = '<LQQ' if zip64 else '<LLL' |
| 1449 | self.fp.write(struct.pack(fmt, zinfo.CRC, zinfo.compress_size, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1450 | zinfo.file_size)) |
Serhiy Storchaka | 182d7cd | 2013-01-15 00:31:39 +0200 | [diff] [blame] | 1451 | self.fp.flush() |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1452 | self.filelist.append(zinfo) |
| 1453 | self.NameToInfo[zinfo.filename] = zinfo |
| 1454 | |
| 1455 | def __del__(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1456 | """Call the "close()" method in case the user forgot.""" |
Tim Peters | d15f8bb | 2001-11-28 23:16:40 +0000 | [diff] [blame] | 1457 | self.close() |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1458 | |
| 1459 | def close(self): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1460 | """Close the file, and for mode "w" and "a" write the ending |
| 1461 | records.""" |
Tim Peters | d15f8bb | 2001-11-28 23:16:40 +0000 | [diff] [blame] | 1462 | if self.fp is None: |
| 1463 | return |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1465 | try: |
| 1466 | if self.mode in ("w", "a") and self._didModify: # write ending records |
| 1467 | count = 0 |
| 1468 | pos1 = self.fp.tell() |
| 1469 | for zinfo in self.filelist: # write central directory |
| 1470 | count = count + 1 |
| 1471 | dt = zinfo.date_time |
| 1472 | dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2] |
| 1473 | dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2) |
| 1474 | extra = [] |
| 1475 | if zinfo.file_size > ZIP64_LIMIT \ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1476 | or zinfo.compress_size > ZIP64_LIMIT: |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1477 | extra.append(zinfo.file_size) |
| 1478 | extra.append(zinfo.compress_size) |
| 1479 | file_size = 0xffffffff |
| 1480 | compress_size = 0xffffffff |
| 1481 | else: |
| 1482 | file_size = zinfo.file_size |
| 1483 | compress_size = zinfo.compress_size |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1484 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1485 | if zinfo.header_offset > ZIP64_LIMIT: |
| 1486 | extra.append(zinfo.header_offset) |
| 1487 | header_offset = 0xffffffff |
| 1488 | else: |
| 1489 | header_offset = zinfo.header_offset |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1490 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1491 | extra_data = zinfo.extra |
Antoine Pitrou | 8572da5 | 2012-11-17 23:52:05 +0100 | [diff] [blame] | 1492 | min_version = 0 |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1493 | if extra: |
| 1494 | # Append a ZIP64 field to the extra's |
| 1495 | extra_data = struct.pack( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1496 | '<HH' + 'Q'*len(extra), |
| 1497 | 1, 8*len(extra), *extra) + extra_data |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1498 | |
Antoine Pitrou | 8572da5 | 2012-11-17 23:52:05 +0100 | [diff] [blame] | 1499 | min_version = ZIP64_VERSION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1500 | |
Antoine Pitrou | 8572da5 | 2012-11-17 23:52:05 +0100 | [diff] [blame] | 1501 | if zinfo.compress_type == ZIP_BZIP2: |
| 1502 | min_version = max(BZIP2_VERSION, min_version) |
| 1503 | elif zinfo.compress_type == ZIP_LZMA: |
| 1504 | min_version = max(LZMA_VERSION, min_version) |
Martin v. Löwis | f6b16a4 | 2012-05-01 07:58:44 +0200 | [diff] [blame] | 1505 | |
Antoine Pitrou | 8572da5 | 2012-11-17 23:52:05 +0100 | [diff] [blame] | 1506 | extract_version = max(min_version, zinfo.extract_version) |
| 1507 | create_version = max(min_version, zinfo.create_version) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1508 | try: |
| 1509 | filename, flag_bits = zinfo._encodeFilenameFlags() |
| 1510 | centdir = struct.pack(structCentralDir, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1511 | stringCentralDir, create_version, |
| 1512 | zinfo.create_system, extract_version, zinfo.reserved, |
| 1513 | flag_bits, zinfo.compress_type, dostime, dosdate, |
| 1514 | zinfo.CRC, compress_size, file_size, |
| 1515 | len(filename), len(extra_data), len(zinfo.comment), |
| 1516 | 0, zinfo.internal_attr, zinfo.external_attr, |
| 1517 | header_offset) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1518 | except DeprecationWarning: |
| 1519 | print((structCentralDir, stringCentralDir, create_version, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1520 | zinfo.create_system, extract_version, zinfo.reserved, |
| 1521 | zinfo.flag_bits, zinfo.compress_type, dostime, dosdate, |
| 1522 | zinfo.CRC, compress_size, file_size, |
| 1523 | len(zinfo.filename), len(extra_data), len(zinfo.comment), |
| 1524 | 0, zinfo.internal_attr, zinfo.external_attr, |
| 1525 | header_offset), file=sys.stderr) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1526 | raise |
| 1527 | self.fp.write(centdir) |
| 1528 | self.fp.write(filename) |
| 1529 | self.fp.write(extra_data) |
| 1530 | self.fp.write(zinfo.comment) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1531 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1532 | pos2 = self.fp.tell() |
| 1533 | # Write end-of-zip-archive record |
| 1534 | centDirCount = count |
| 1535 | centDirSize = pos2 - pos1 |
| 1536 | centDirOffset = pos1 |
| 1537 | if (centDirCount >= ZIP_FILECOUNT_LIMIT or |
| 1538 | centDirOffset > ZIP64_LIMIT or |
| 1539 | centDirSize > ZIP64_LIMIT): |
| 1540 | # Need to write the ZIP64 end-of-archive records |
| 1541 | zip64endrec = struct.pack( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1542 | structEndArchive64, stringEndArchive64, |
| 1543 | 44, 45, 45, 0, 0, centDirCount, centDirCount, |
| 1544 | centDirSize, centDirOffset) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1545 | self.fp.write(zip64endrec) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1547 | zip64locrec = struct.pack( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1548 | structEndArchive64Locator, |
| 1549 | stringEndArchive64Locator, 0, pos2, 1) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1550 | self.fp.write(zip64locrec) |
| 1551 | centDirCount = min(centDirCount, 0xFFFF) |
| 1552 | centDirSize = min(centDirSize, 0xFFFFFFFF) |
| 1553 | centDirOffset = min(centDirOffset, 0xFFFFFFFF) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1554 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1555 | endrec = struct.pack(structEndArchive, stringEndArchive, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1556 | 0, 0, centDirCount, centDirCount, |
| 1557 | centDirSize, centDirOffset, len(self._comment)) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1558 | self.fp.write(endrec) |
| 1559 | self.fp.write(self._comment) |
| 1560 | self.fp.flush() |
| 1561 | finally: |
| 1562 | fp = self.fp |
| 1563 | self.fp = None |
| 1564 | if not self._filePassed: |
| 1565 | fp.close() |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1566 | |
| 1567 | |
| 1568 | class PyZipFile(ZipFile): |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1569 | """Class to create ZIP archives with Python library files and packages.""" |
| 1570 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1571 | def __init__(self, file, mode="r", compression=ZIP_STORED, |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 1572 | allowZip64=True, optimize=-1): |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1573 | ZipFile.__init__(self, file, mode=mode, compression=compression, |
| 1574 | allowZip64=allowZip64) |
| 1575 | self._optimize = optimize |
| 1576 | |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1577 | def writepy(self, pathname, basename="", filterfunc=None): |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1578 | """Add all files from "pathname" to the ZIP archive. |
| 1579 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1580 | If pathname is a package directory, search the directory and |
| 1581 | all package subdirectories recursively for all *.py and enter |
| 1582 | the modules into the archive. If pathname is a plain |
| 1583 | directory, listdir *.py and enter all modules. Else, pathname |
| 1584 | must be a Python *.py file and the module will be put into the |
| 1585 | archive. Added modules are always module.pyo or module.pyc. |
| 1586 | This method will compile the module.py into module.pyc if |
| 1587 | necessary. |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1588 | If filterfunc(pathname) is given, it is called with every argument. |
| 1589 | When it is False, the file or directory is skipped. |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1590 | """ |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1591 | if filterfunc and not filterfunc(pathname): |
| 1592 | if self.debug: |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1593 | label = 'path' if os.path.isdir(pathname) else 'file' |
| 1594 | print('%s "%s" skipped by filterfunc' % (label, pathname)) |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1595 | return |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1596 | dir, name = os.path.split(pathname) |
| 1597 | if os.path.isdir(pathname): |
| 1598 | initname = os.path.join(pathname, "__init__.py") |
| 1599 | if os.path.isfile(initname): |
| 1600 | # This is a package directory, add it |
| 1601 | if basename: |
| 1602 | basename = "%s/%s" % (basename, name) |
| 1603 | else: |
| 1604 | basename = name |
| 1605 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1606 | print("Adding package in", pathname, "as", basename) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1607 | fname, arcname = self._get_codename(initname[0:-3], basename) |
| 1608 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1609 | print("Adding", arcname) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1610 | self.write(fname, arcname) |
| 1611 | dirlist = os.listdir(pathname) |
| 1612 | dirlist.remove("__init__.py") |
| 1613 | # Add all *.py files and package subdirectories |
| 1614 | for filename in dirlist: |
| 1615 | path = os.path.join(pathname, filename) |
| 1616 | root, ext = os.path.splitext(filename) |
| 1617 | if os.path.isdir(path): |
| 1618 | if os.path.isfile(os.path.join(path, "__init__.py")): |
| 1619 | # This is a package directory, add it |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1620 | self.writepy(path, basename, |
| 1621 | filterfunc=filterfunc) # Recursive call |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1622 | elif ext == ".py": |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1623 | if filterfunc and not filterfunc(path): |
| 1624 | if self.debug: |
| 1625 | print('file "%s" skipped by filterfunc' % path) |
| 1626 | continue |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1627 | fname, arcname = self._get_codename(path[0:-3], |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1628 | basename) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1629 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1630 | print("Adding", arcname) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1631 | self.write(fname, arcname) |
| 1632 | else: |
| 1633 | # This is NOT a package directory, add its files at top level |
| 1634 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1635 | print("Adding files from directory", pathname) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1636 | for filename in os.listdir(pathname): |
| 1637 | path = os.path.join(pathname, filename) |
| 1638 | root, ext = os.path.splitext(filename) |
| 1639 | if ext == ".py": |
Christian Tismer | 410d931 | 2013-10-22 04:09:28 +0200 | [diff] [blame] | 1640 | if filterfunc and not filterfunc(path): |
| 1641 | if self.debug: |
| 1642 | print('file "%s" skipped by filterfunc' % path) |
| 1643 | continue |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1644 | fname, arcname = self._get_codename(path[0:-3], |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1645 | basename) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1646 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1647 | print("Adding", arcname) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1648 | self.write(fname, arcname) |
| 1649 | else: |
| 1650 | if pathname[-3:] != ".py": |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 1651 | raise RuntimeError( |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1652 | 'Files added with writepy() must end with ".py"') |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1653 | fname, arcname = self._get_codename(pathname[0:-3], basename) |
| 1654 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1655 | print("Adding file", arcname) |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1656 | self.write(fname, arcname) |
| 1657 | |
| 1658 | def _get_codename(self, pathname, basename): |
| 1659 | """Return (filename, archivename) for the path. |
| 1660 | |
Fred Drake | 484d735 | 2000-10-02 21:14:52 +0000 | [diff] [blame] | 1661 | Given a module name path, return the correct file path and |
| 1662 | archive name, compiling if necessary. For example, given |
| 1663 | /python/lib/string, return (/python/lib/string.pyc, string). |
| 1664 | """ |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1665 | def _compile(file, optimize=-1): |
| 1666 | import py_compile |
| 1667 | if self.debug: |
| 1668 | print("Compiling", file) |
| 1669 | try: |
| 1670 | py_compile.compile(file, doraise=True, optimize=optimize) |
Serhiy Storchaka | 45c4375 | 2013-01-29 20:10:28 +0200 | [diff] [blame] | 1671 | except py_compile.PyCompileError as err: |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1672 | print(err.msg) |
| 1673 | return False |
| 1674 | return True |
| 1675 | |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1676 | file_py = pathname + ".py" |
| 1677 | file_pyc = pathname + ".pyc" |
| 1678 | file_pyo = pathname + ".pyo" |
Brett Cannon | b57a085 | 2013-06-15 17:32:30 -0400 | [diff] [blame] | 1679 | pycache_pyc = importlib.util.cache_from_source(file_py, True) |
| 1680 | pycache_pyo = importlib.util.cache_from_source(file_py, False) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1681 | if self._optimize == -1: |
| 1682 | # legacy mode: use whatever file is present |
| 1683 | if (os.path.isfile(file_pyo) and |
| 1684 | os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime): |
| 1685 | # Use .pyo file. |
| 1686 | arcname = fname = file_pyo |
| 1687 | elif (os.path.isfile(file_pyc) and |
| 1688 | os.stat(file_pyc).st_mtime >= os.stat(file_py).st_mtime): |
| 1689 | # Use .pyc file. |
| 1690 | arcname = fname = file_pyc |
| 1691 | elif (os.path.isfile(pycache_pyc) and |
| 1692 | os.stat(pycache_pyc).st_mtime >= os.stat(file_py).st_mtime): |
| 1693 | # Use the __pycache__/*.pyc file, but write it to the legacy pyc |
| 1694 | # file name in the archive. |
| 1695 | fname = pycache_pyc |
| 1696 | arcname = file_pyc |
| 1697 | elif (os.path.isfile(pycache_pyo) and |
| 1698 | os.stat(pycache_pyo).st_mtime >= os.stat(file_py).st_mtime): |
| 1699 | # Use the __pycache__/*.pyo file, but write it to the legacy pyo |
| 1700 | # file name in the archive. |
| 1701 | fname = pycache_pyo |
| 1702 | arcname = file_pyo |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1703 | else: |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1704 | # Compile py into PEP 3147 pyc file. |
| 1705 | if _compile(file_py): |
| 1706 | fname = (pycache_pyc if __debug__ else pycache_pyo) |
| 1707 | arcname = (file_pyc if __debug__ else file_pyo) |
| 1708 | else: |
| 1709 | fname = arcname = file_py |
| 1710 | else: |
| 1711 | # new mode: use given optimization level |
| 1712 | if self._optimize == 0: |
| 1713 | fname = pycache_pyc |
| 1714 | arcname = file_pyc |
| 1715 | else: |
| 1716 | fname = pycache_pyo |
| 1717 | arcname = file_pyo |
| 1718 | if not (os.path.isfile(fname) and |
| 1719 | os.stat(fname).st_mtime >= os.stat(file_py).st_mtime): |
| 1720 | if not _compile(file_py, optimize=self._optimize): |
| 1721 | fname = arcname = file_py |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1722 | archivename = os.path.split(arcname)[1] |
Guido van Rossum | 32abe6f | 2000-03-31 17:30:02 +0000 | [diff] [blame] | 1723 | if basename: |
| 1724 | archivename = "%s/%s" % (basename, archivename) |
| 1725 | return (fname, archivename) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1726 | |
| 1727 | |
| 1728 | def main(args = None): |
| 1729 | import textwrap |
| 1730 | USAGE=textwrap.dedent("""\ |
| 1731 | Usage: |
| 1732 | zipfile.py -l zipfile.zip # Show listing of a zipfile |
| 1733 | zipfile.py -t zipfile.zip # Test if a zipfile is valid |
| 1734 | zipfile.py -e zipfile.zip target # Extract zipfile into target dir |
| 1735 | zipfile.py -c zipfile.zip src ... # Create zipfile from sources |
| 1736 | """) |
| 1737 | if args is None: |
| 1738 | args = sys.argv[1:] |
| 1739 | |
| 1740 | if not args or args[0] not in ('-l', '-c', '-e', '-t'): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1741 | print(USAGE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1742 | sys.exit(1) |
| 1743 | |
| 1744 | if args[0] == '-l': |
| 1745 | if len(args) != 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1746 | print(USAGE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1747 | sys.exit(1) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1748 | with ZipFile(args[1], 'r') as zf: |
| 1749 | zf.printdir() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1750 | |
| 1751 | elif args[0] == '-t': |
| 1752 | if len(args) != 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1753 | print(USAGE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1754 | sys.exit(1) |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1755 | with ZipFile(args[1], 'r') as zf: |
| 1756 | badfile = zf.testzip() |
Antoine Pitrou | 7c8bcb6 | 2010-08-12 15:11:50 +0000 | [diff] [blame] | 1757 | if badfile: |
| 1758 | print("The following enclosed file is corrupted: {!r}".format(badfile)) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1759 | print("Done testing") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1760 | |
| 1761 | elif args[0] == '-e': |
| 1762 | if len(args) != 3: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1763 | print(USAGE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1764 | sys.exit(1) |
| 1765 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1766 | with ZipFile(args[1], 'r') as zf: |
| 1767 | out = args[2] |
| 1768 | for path in zf.namelist(): |
| 1769 | if path.startswith('./'): |
| 1770 | tgt = os.path.join(out, path[2:]) |
| 1771 | else: |
| 1772 | tgt = os.path.join(out, path) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1774 | tgtdir = os.path.dirname(tgt) |
| 1775 | if not os.path.exists(tgtdir): |
| 1776 | os.makedirs(tgtdir) |
| 1777 | with open(tgt, 'wb') as fp: |
| 1778 | fp.write(zf.read(path)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1779 | |
| 1780 | elif args[0] == '-c': |
| 1781 | if len(args) < 3: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1782 | print(USAGE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1783 | sys.exit(1) |
| 1784 | |
| 1785 | def addToZip(zf, path, zippath): |
| 1786 | if os.path.isfile(path): |
| 1787 | zf.write(path, zippath, ZIP_DEFLATED) |
| 1788 | elif os.path.isdir(path): |
| 1789 | for nm in os.listdir(path): |
| 1790 | addToZip(zf, |
Christian Tismer | 59202e5 | 2013-10-21 03:59:23 +0200 | [diff] [blame] | 1791 | os.path.join(path, nm), os.path.join(zippath, nm)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1792 | # else: ignore |
| 1793 | |
Serhiy Storchaka | 235c5e0 | 2013-11-23 15:55:38 +0200 | [diff] [blame] | 1794 | with ZipFile(args[1], 'w') as zf: |
Antoine Pitrou | 17babc5 | 2012-11-17 23:50:08 +0100 | [diff] [blame] | 1795 | for src in args[2:]: |
| 1796 | addToZip(zf, src, os.path.basename(src)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1797 | |
| 1798 | if __name__ == "__main__": |
| 1799 | main() |