Debian Codegraph @

Lib/zipfile/__init__.py

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