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