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 try:
570 return self.filename.encode('ascii'), self.flag_bits
571 except UnicodeEncodeError:
572 return self.filename.encode('utf-8'), self.flag_bits | _MASK_UTF_FILENAME
573
574 def _decodeExtra(self, filename_crc):
575 # Try to decode the extra field.
576 extra = self.extra
577 unpack = struct.unpack
578 while len(extra) >= 4:
579 tp, ln = unpack('<HH', extra[:4])
580 if ln+4 > len(extra):
581 raise BadZipFile("Corrupt extra field %04x (size=%d)" % (tp, ln))
582 if tp == 0x0001:
583 data = extra[4:ln+4]
584 # ZIP64 extension (large files and/or large archives)
585 try:
586 if self.file_size in (0xFFFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF):
587 field = "File size"
588 self.file_size, = unpack('<Q', data[:8])
589 data = data[8:]
590 if self.compress_size == 0xFFFF_FFFF:
591 field = "Compress size"
592 self.compress_size, = unpack('<Q', data[:8])
593 data = data[8:]
594 if self.header_offset == 0xFFFF_FFFF:
595 field = "Header offset"
596 self.header_offset, = unpack('<Q', data[:8])
597 except struct.error:
598 raise BadZipFile(f"Corrupt zip64 extra field. "
599 f"{field} not found.") from None
600 elif tp == 0x7075:
601 data = extra[4:ln+4]
602 # Unicode Path Extra Field
603 try:
604 up_version, up_name_crc = unpack('<BL', data[:5])
605 if up_version == 1 and up_name_crc == filename_crc:
606 up_unicode_name = data[5:].decode('utf-8')
607 if up_unicode_name:
608 self.filename = _sanitize_filename(up_unicode_name)
609 else:
610 import warnings
611 warnings.warn("Empty unicode path extra field (0x7075)", stacklevel=2)
612 except struct.error as e:
613 raise BadZipFile("Corrupt unicode path extra field (0x7075)") from e
614 except UnicodeDecodeError as e:
615 raise BadZipFile('Corrupt unicode path extra field (0x7075): invalid utf-8 bytes') from e
616
617 extra = extra[ln+4:]
618
619 @classmethod
620 def from_file(cls, filename, arcname=None, *, strict_timestamps=True):
621 """Construct an appropriate ZipInfo for a file on the filesystem.
622
623 filename should be the path to a file or directory on the filesystem.
624
625 arcname is the name which it will have within the archive (by default,
626 this will be the same as filename, but without a drive letter and with
627 leading path separators removed).
628 """
629 if isinstance(filename, os.PathLike):
630 filename = os.fspath(filename)
631 st = os.stat(filename)
632 isdir = stat.S_ISDIR(st.st_mode)
633 mtime = time.localtime(st.st_mtime)
634 date_time = mtime[0:6]
635 if not strict_timestamps and date_time[0] < 1980:
636 date_time = (1980, 1, 1, 0, 0, 0)
637 elif not strict_timestamps and date_time[0] > 2107:
638 date_time = (2107, 12, 31, 23, 59, 59)
639 # Create ZipInfo instance to store file information
640 if arcname is None:
641 arcname = filename
642 arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
643 while arcname[0] in (os.sep, os.altsep):
644 arcname = arcname[1:]
645 if isdir:
646 arcname += '/'
647 zinfo = cls(arcname, date_time)
648 zinfo.external_attr = (st.st_mode & 0xFFFF) << 16 # Unix attributes
649 if isdir:
650 zinfo.file_size = 0
651 zinfo.external_attr |= 0x10 # MS-DOS directory flag
652 else:
653 zinfo.file_size = st.st_size
654
655 return zinfo
656
657 def _for_archive(self, archive):
658 """Resolve suitable defaults from the archive.
659
660 Resolve the date_time, compression attributes, and external attributes
661 to suitable defaults as used by :method:`ZipFile.writestr`.
662
663 Return self.
664 """
665 # gh-91279: Set the SOURCE_DATE_EPOCH to a specific timestamp
666 epoch = os.environ.get('SOURCE_DATE_EPOCH')
667 get_time = int(epoch) if epoch else time.time()
668 self.date_time = time.localtime(get_time)[:6]
669
670 self.compress_type = archive.compression
671 self.compress_level = archive.compresslevel
672 if self.filename.endswith('/'): # pragma: no cover
673 self.external_attr = 0o40775 << 16 # drwxrwxr-x
674 self.external_attr |= 0x10 # MS-DOS directory flag
675 else:
676 self.external_attr = 0o600 << 16 # ?rw-------
677 return self
678
679 def is_dir(self):
680 """Return True if this archive member is a directory."""
681 if self.filename.endswith('/'):
682 return True
683 # The ZIP format specification requires to use forward slashes
684 # as the directory separator, but in practice some ZIP files
685 # created on Windows can use backward slashes. For compatibility
686 # with the extraction code which already handles this:
687 if os.path.altsep:
688 return self.filename.endswith((os.path.sep, os.path.altsep))
689 return False
690
691
692 # ZIP encryption uses the CRC32 one-byte primitive for scrambling some
693 # internal keys. We noticed that a direct implementation is faster than
694 # relying on binascii.crc32().
695
696 _crctable = None
697 def _gen_crc(crc):
698 for j in range(8):
699 if crc & 1:
700 crc = (crc >> 1) ^ 0xEDB88320
701 else:
702 crc >>= 1
703 return crc
704
705 # ZIP supports a password-based form of encryption. Even though known
706 # plaintext attacks have been found against it, it is still useful
707 # to be able to get data out of such a file.
708 #
709 # Usage:
710 # zd = _ZipDecrypter(mypwd)
711 # plain_bytes = zd(cypher_bytes)
712
713 def _ZipDecrypter(pwd):
714 key0 = 305419896
715 key1 = 591751049
716 key2 = 878082192
717
718 global _crctable
719 if _crctable is None:
720 _crctable = list(map(_gen_crc, range(256)))
721 crctable = _crctable
722
723 def crc32(ch, crc):
724 """Compute the CRC32 primitive on one byte."""
725 return (crc >> 8) ^ crctable[(crc ^ ch) & 0xFF]
726
727 def update_keys(c):
728 nonlocal key0, key1, key2
729 key0 = crc32(c, key0)
730 key1 = (key1 + (key0 & 0xFF)) & 0xFFFFFFFF
731 key1 = (key1 * 134775813 + 1) & 0xFFFFFFFF
732 key2 = crc32(key1 >> 24, key2)
733
734 for p in pwd:
735 update_keys(p)
736
737 def decrypter(data):
738 """Decrypt a bytes object."""
739 result = bytearray()
740 append = result.append
741 for c in data:
742 k = key2 | 2
743 c ^= ((k * (k^1)) >> 8) & 0xFF
744 update_keys(c)
745 append(c)
746 return bytes(result)
747
748 return decrypter
749
750
751 class LZMACompressor:
752
753 def __init__(self):
754 self._comp = None
755
756 def _init(self):
757 props = lzma._encode_filter_properties({'id': lzma.FILTER_LZMA1})
758 self._comp = lzma.LZMACompressor(lzma.FORMAT_RAW, filters=[
759 lzma._decode_filter_properties(lzma.FILTER_LZMA1, props)
760 ])
761 return struct.pack('<BBH', 9, 4, len(props)) + props
762
763 def compress(self, data):
764 if self._comp is None:
765 return self._init() + self._comp.compress(data)
766 return self._comp.compress(data)
767
768 def flush(self):
769 if self._comp is None:
770 return self._init() + self._comp.flush()
771 return self._comp.flush()
772
773
774 class LZMADecompressor:
775
776 def __init__(self):
777 self._decomp = None
778 self._unconsumed = b''
779 self.eof = False
780
781 def decompress(self, data):
782 if self._decomp is None:
783 self._unconsumed += data
784 if len(self._unconsumed) <= 4:
785 return b''
786 psize, = struct.unpack('<H', self._unconsumed[2:4])
787 if len(self._unconsumed) <= 4 + psize:
788 return b''
789
790 self._decomp = lzma.LZMADecompressor(lzma.FORMAT_RAW, filters=[
791 lzma._decode_filter_properties(lzma.FILTER_LZMA1,
792 self._unconsumed[4:4 + psize])
793 ])
794 data = self._unconsumed[4 + psize:]
795 del self._unconsumed
796
797 result = self._decomp.decompress(data)
798 self.eof = self._decomp.eof
799 return result
800
801
802 compressor_names = {
803 0: 'store',
804 1: 'shrink',
805 2: 'reduce',
806 3: 'reduce',
807 4: 'reduce',
808 5: 'reduce',
809 6: 'implode',
810 7: 'tokenize',
811 8: 'deflate',
812 9: 'deflate64',
813 10: 'implode',
814 12: 'bzip2',
815 14: 'lzma',
816 18: 'terse',
817 19: 'lz77',
818 93: 'zstd',
819 97: 'wavpack',
820 98: 'ppmd',
821 }
822
823 def _check_compression(compression):
824 if compression == ZIP_STORED:
825 pass
826 elif compression == ZIP_DEFLATED:
827 if not zlib:
828 raise RuntimeError(
829 "Compression requires the (missing) zlib module")
830 elif compression == ZIP_BZIP2:
831 if not bz2:
832 raise RuntimeError(
833 "Compression requires the (missing) bz2 module")
834 elif compression == ZIP_LZMA:
835 if not lzma:
836 raise RuntimeError(
837 "Compression requires the (missing) lzma module")
838 elif compression == ZIP_ZSTANDARD:
839 if not zstd:
840 raise RuntimeError(
841 "Compression requires the (missing) compression.zstd module")
842 else:
843 raise NotImplementedError("That compression method is not supported")
844
845
846 def _get_compressor(compress_type, compresslevel=None):
847 if compress_type == ZIP_DEFLATED:
848 if compresslevel is not None:
849 return zlib.compressobj(compresslevel, zlib.DEFLATED, -15)
850 return zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15)
851 elif compress_type == ZIP_BZIP2:
852 if compresslevel is not None:
853 return bz2.BZ2Compressor(compresslevel)
854 return bz2.BZ2Compressor()
855 # compresslevel is ignored for ZIP_LZMA
856 elif compress_type == ZIP_LZMA:
857 return LZMACompressor()
858 elif compress_type == ZIP_ZSTANDARD:
859 return zstd.ZstdCompressor(level=compresslevel)
860 else:
861 return None
862
863
864 def _get_decompressor(compress_type):
865 _check_compression(compress_type)
866 if compress_type == ZIP_STORED:
867 return None
868 elif compress_type == ZIP_DEFLATED:
869 return zlib.decompressobj(-15)
870 elif compress_type == ZIP_BZIP2:
871 return bz2.BZ2Decompressor()
872 elif compress_type == ZIP_LZMA:
873 return LZMADecompressor()
874 elif compress_type == ZIP_ZSTANDARD:
875 return zstd.ZstdDecompressor()
876 else:
877 descr = compressor_names.get(compress_type)
878 if descr:
879 raise NotImplementedError("compression type %d (%s)" % (compress_type, descr))
880 else:
881 raise NotImplementedError("compression type %d" % (compress_type,))
882
883
884 class _SharedFile:
885 def __init__(self, file, pos, close, lock, writing):
886 self._file = file
887 self._pos = pos
888 self._close = close
889 self._lock = lock
890 self._writing = writing
891 self.seekable = file.seekable
892
893 def tell(self):
894 return self._pos
895
896 def seek(self, offset, whence=0):
897 with self._lock:
898 if self._writing():
899 raise ValueError("Can't reposition in the ZIP file while "
900 "there is an open writing handle on it. "
901 "Close the writing handle before trying to read.")
902 if whence == os.SEEK_CUR:
903 self._file.seek(self._pos + offset)
904 else:
905 self._file.seek(offset, whence)
906 self._pos = self._file.tell()
907 return self._pos
908
909 def read(self, n=-1):
910 with self._lock:
911 if self._writing():
912 raise ValueError("Can't read from the ZIP file while there "
913 "is an open writing handle on it. "
914 "Close the writing handle before trying to read.")
915 self._file.seek(self._pos)
916 data = self._file.read(n)
917 self._pos = self._file.tell()
918 return data
919
920 def close(self):
921 if self._file is not None:
922 fileobj = self._file
923 self._file = None
924 self._close(fileobj)
925
926 # Provide the tell method for unseekable stream
927 class _Tellable:
928 def __init__(self, fp):
929 self.fp = fp
930 self.offset = 0
931
932 def write(self, data):
933 n = self.fp.write(data)
934 self.offset += n
935 return n
936
937 def tell(self):
938 return self.offset
939
940 def flush(self):
941 self.fp.flush()
942
943 def close(self):
944 self.fp.close()
945
946
947 class ZipExtFile(io.BufferedIOBase):
948 """File-like object for reading an archive member.
949 Is returned by ZipFile.open().
950 """
951
952 # Max size supported by decompressor.
953 MAX_N = (1 << 31) - 1
954
955 # Read from compressed files in 4k blocks.
956 MIN_READ_SIZE = 4096
957
958 # Chunk size to read during seek
959 MAX_SEEK_READ = 1 << 24
960
961 def __init__(self, fileobj, mode, zipinfo, pwd=None,
962 close_fileobj=False):
963 self._fileobj = fileobj
964 self._pwd = pwd
965 self._close_fileobj = close_fileobj
966
967 self._compress_type = zipinfo.compress_type
968 self._compress_left = zipinfo.compress_size
969 self._left = zipinfo.file_size
970
971 self._decompressor = _get_decompressor(self._compress_type)
972
973 self._eof = False
974 self._readbuffer = b''
975 self._offset = 0
976
977 self.newlines = None
978
979 self.mode = mode
980 self.name = zipinfo.filename
981
982 if hasattr(zipinfo, 'CRC'):
983 self._expected_crc = zipinfo.CRC
984 self._running_crc = crc32(b'')
985 else:
986 self._expected_crc = None
987
988 self._seekable = False
989 try:
990 if fileobj.seekable():
991 self._orig_compress_start = fileobj.tell()
992 self._orig_compress_size = zipinfo.compress_size
993 self._orig_file_size = zipinfo.file_size
994 self._orig_start_crc = self._running_crc
995 self._orig_crc = self._expected_crc
996 self._seekable = True
997 except AttributeError:
998 pass
999
1000 self._decrypter = None
1001 if pwd:
1002 if zipinfo.flag_bits & _MASK_USE_DATA_DESCRIPTOR:
1003 # compare against the file type from extended local headers
1004 check_byte = (zipinfo._raw_time >> 8) & 0xff
1005 else:
1006 # compare against the CRC otherwise
1007 check_byte = (zipinfo.CRC >> 24) & 0xff
1008 h = self._init_decrypter()
1009 if h != check_byte:
1010 raise RuntimeError("Bad password for file %r" % zipinfo.orig_filename)
1011
1012
1013 def _init_decrypter(self):
1014 self._decrypter = _ZipDecrypter(self._pwd)
1015 # The first 12 bytes in the cypher stream is an encryption header
1016 # used to strengthen the algorithm. The first 11 bytes are
1017 # completely random, while the 12th contains the MSB of the CRC,
1018 # or the MSB of the file time depending on the header type
1019 # and is used to check the correctness of the password.
1020 header = self._fileobj.read(12)
1021 self._compress_left -= 12
1022 return self._decrypter(header)[11]
1023
1024 def __repr__(self):
1025 result = ['<%s.%s' % (self.__class__.__module__,
1026 self.__class__.__qualname__)]
1027 if not self.closed:
1028 result.append(' name=%r' % (self.name,))
1029 if self._compress_type != ZIP_STORED:
1030 result.append(' compress_type=%s' %
1031 compressor_names.get(self._compress_type,
1032 self._compress_type))
1033 else:
1034 result.append(' [closed]')
1035 result.append('>')
1036 return ''.join(result)
1037
1038 def readline(self, limit=-1):
1039 """Read and return a line from the stream.
1040
1041 If limit is specified, at most limit bytes will be read.
1042 """
1043
1044 if limit < 0:
1045 # Shortcut common case - newline found in buffer.
1046 i = self._readbuffer.find(b'\n', self._offset) + 1
1047 if i > 0:
1048 line = self._readbuffer[self._offset: i]
1049 self._offset = i
1050 return line
1051
1052 return io.BufferedIOBase.readline(self, limit)
1053
1054 def peek(self, n=1):
1055 """Returns buffered bytes without advancing the position."""
1056 if n > len(self._readbuffer) - self._offset:
1057 chunk = self.read(n)
1058 if len(chunk) > self._offset:
1059 self._readbuffer = chunk + self._readbuffer[self._offset:]
1060 self._offset = 0
1061 else:
1062 self._offset -= len(chunk)
1063
1064 # Return up to 512 bytes to reduce allocation overhead for tight loops.
1065 return self._readbuffer[self._offset: self._offset + 512]
1066
1067 def readable(self):
1068 if self.closed:
1069 raise ValueError("I/O operation on closed file.")
1070 return True
1071
1072 def read(self, n=-1):
1073 """Read and return up to n bytes.
1074 If the argument is omitted, None, or negative, data is read and returned until EOF is reached.
1075 """
1076 if self.closed:
1077 raise ValueError("read from closed file.")
1078 if n is None or n < 0:
1079 buf = self._readbuffer[self._offset:]
1080 self._readbuffer = b''
1081 self._offset = 0
1082 while not self._eof:
1083 buf += self._read1(self.MAX_N)
1084 return buf
1085
1086 end = n + self._offset
1087 if end < len(self._readbuffer):
1088 buf = self._readbuffer[self._offset:end]
1089 self._offset = end
1090 return buf
1091
1092 n = end - len(self._readbuffer)
1093 buf = self._readbuffer[self._offset:]
1094 self._readbuffer = b''
1095 self._offset = 0
1096 while n > 0 and not self._eof:
1097 data = self._read1(n)
1098 if n < len(data):
1099 self._readbuffer = data
1100 self._offset = n
1101 buf += data[:n]
1102 break
1103 buf += data
1104 n -= len(data)
1105 return buf
1106
1107 def _update_crc(self, newdata):
1108 # Update the CRC using the given data.
1109 if self._expected_crc is None:
1110 # No need to compute the CRC if we don't have a reference value
1111 return
1112 self._running_crc = crc32(newdata, self._running_crc)
1113 # Check the CRC if we're at the end of the file
1114 if self._eof and self._running_crc != self._expected_crc:
1115 raise BadZipFile("Bad CRC-32 for file %r" % self.name)
1116
1117 def read1(self, n):
1118 """Read up to n bytes with at most one read() system call."""
1119
1120 if n is None or n < 0:
1121 buf = self._readbuffer[self._offset:]
1122 self._readbuffer = b''
1123 self._offset = 0
1124 while not self._eof:
1125 data = self._read1(self.MAX_N)
1126 if data:
1127 buf += data
1128 break
1129 return buf
1130
1131 end = n + self._offset
1132 if end < len(self._readbuffer):
1133 buf = self._readbuffer[self._offset:end]
1134 self._offset = end
1135 return buf
1136
1137 n = end - len(self._readbuffer)
1138 buf = self._readbuffer[self._offset:]
1139 self._readbuffer = b''
1140 self._offset = 0
1141 if n > 0:
1142 while not self._eof:
1143 data = self._read1(n)
1144 if n < len(data):
1145 self._readbuffer = data
1146 self._offset = n
1147 buf += data[:n]
1148 break
1149 if data:
1150 buf += data
1151 break
1152 return buf
1153
1154 def _read1(self, n):
1155 # Read up to n compressed bytes with at most one read() system call,
1156 # decrypt and decompress them.
1157 if self._eof or n <= 0:
1158 return b''
1159
1160 # Read from file.
1161 if self._compress_type == ZIP_DEFLATED:
1162 ## Handle unconsumed data.
1163 data = self._decompressor.unconsumed_tail
1164 if n > len(data):
1165 data += self._read2(n - len(data))
1166 else:
1167 data = self._read2(n)
1168
1169 if self._compress_type == ZIP_STORED:
1170 self._eof = self._compress_left <= 0
1171 elif self._compress_type == ZIP_DEFLATED:
1172 n = max(n, self.MIN_READ_SIZE)
1173 data = self._decompressor.decompress(data, n)
1174 self._eof = (self._decompressor.eof or
1175 self._compress_left <= 0 and
1176 not self._decompressor.unconsumed_tail)
1177 if self._eof:
1178 data += self._decompressor.flush()
1179 else:
1180 data = self._decompressor.decompress(data)
1181 self._eof = self._decompressor.eof or self._compress_left <= 0
1182
1183 data = data[:self._left]
1184 self._left -= len(data)
1185 if self._left <= 0:
1186 self._eof = True
1187 self._update_crc(data)
1188 return data
1189
1190 def _read2(self, n):
1191 if self._compress_left <= 0:
1192 return b''
1193
1194 n = max(n, self.MIN_READ_SIZE)
1195 n = min(n, self._compress_left)
1196
1197 data = self._fileobj.read(n)
1198 self._compress_left -= len(data)
1199 if not data:
1200 raise EOFError
1201
1202 if self._decrypter is not None:
1203 data = self._decrypter(data)
1204 return data
1205
1206 def close(self):
1207 try:
1208 if self._close_fileobj:
1209 self._fileobj.close()
1210 finally:
1211 super().close()
1212
1213 def seekable(self):
1214 if self.closed:
1215 raise ValueError("I/O operation on closed file.")
1216 return self._seekable
1217
1218 def seek(self, offset, whence=os.SEEK_SET):
1219 if self.closed:
1220 raise ValueError("seek on closed file.")
1221 if not self._seekable:
1222 raise io.UnsupportedOperation("underlying stream is not seekable")
1223 curr_pos = self.tell()
1224 if whence == os.SEEK_SET:
1225 new_pos = offset
1226 elif whence == os.SEEK_CUR:
1227 new_pos = curr_pos + offset
1228 elif whence == os.SEEK_END:
1229 new_pos = self._orig_file_size + offset
1230 else:
1231 raise ValueError("whence must be os.SEEK_SET (0), "
1232 "os.SEEK_CUR (1), or os.SEEK_END (2)")
1233
1234 if new_pos > self._orig_file_size:
1235 new_pos = self._orig_file_size
1236
1237 if new_pos < 0:
1238 new_pos = 0
1239
1240 read_offset = new_pos - curr_pos
1241 buff_offset = read_offset + self._offset
1242
1243 if buff_offset >= 0 and buff_offset < len(self._readbuffer):
1244 # Just move the _offset index if the new position is in the _readbuffer
1245 self._offset = buff_offset
1246 read_offset = 0
1247 # Fast seek uncompressed unencrypted file
1248 elif self._compress_type == ZIP_STORED and self._decrypter is None and read_offset != 0:
1249 # disable CRC checking after first seeking - it would be invalid
1250 self._expected_crc = None
1251 # seek actual file taking already buffered data into account
1252 read_offset -= len(self._readbuffer) - self._offset
1253 self._fileobj.seek(read_offset, os.SEEK_CUR)
1254 self._left -= read_offset
1255 self._compress_left -= read_offset
1256 self._eof = self._left <= 0
1257 read_offset = 0
1258 # flush read buffer
1259 self._readbuffer = b''
1260 self._offset = 0
1261 elif read_offset < 0:
1262 # Position is before the current position. Reset the ZipExtFile
1263 self._fileobj.seek(self._orig_compress_start)
1264 self._running_crc = self._orig_start_crc
1265 self._expected_crc = self._orig_crc
1266 self._compress_left = self._orig_compress_size
1267 self._left = self._orig_file_size
1268 self._readbuffer = b''
1269 self._offset = 0
1270 self._decompressor = _get_decompressor(self._compress_type)
1271 self._eof = False
1272 read_offset = new_pos
1273 if self._decrypter is not None:
1274 self._init_decrypter()
1275
1276 while read_offset > 0:
1277 read_len = min(self.MAX_SEEK_READ, read_offset)
1278 self.read(read_len)
1279 read_offset -= read_len
1280
1281 return self.tell()
1282
1283 def tell(self):
1284 if self.closed:
1285 raise ValueError("tell on closed file.")
1286 if not self._seekable:
1287 raise io.UnsupportedOperation("underlying stream is not seekable")
1288 filepos = self._orig_file_size - self._left - len(self._readbuffer) + self._offset
1289 return filepos
1290
1291
1292 class _ZipWriteFile(io.BufferedIOBase):
1293 def __init__(self, zf, zinfo, zip64):
1294 self._zinfo = zinfo
1295 self._zip64 = zip64
1296 self._zipfile = zf
1297 self._compressor = _get_compressor(zinfo.compress_type,
1298 zinfo.compress_level)
1299 self._file_size = 0
1300 self._compress_size = 0
1301 self._crc = 0
1302
1303 @property
1304 def _fileobj(self):
1305 return self._zipfile.fp
1306
1307 @property
1308 def name(self):
1309 return self._zinfo.filename
1310
1311 @property
1312 def mode(self):
1313 return 'wb'
1314
1315 def writable(self):
1316 return True
1317
1318 def write(self, data):
1319 if self.closed:
1320 raise ValueError('I/O operation on closed file.')
1321
1322 # Accept any data that supports the buffer protocol
1323 if isinstance(data, (bytes, bytearray)):
1324 nbytes = len(data)
1325 else:
1326 data = memoryview(data)
1327 nbytes = data.nbytes
1328 self._file_size += nbytes
1329
1330 self._crc = crc32(data, self._crc)
1331 if self._compressor:
1332 data = self._compressor.compress(data)
1333 self._compress_size += len(data)
1334 self._fileobj.write(data)
1335 return nbytes
1336
1337 def close(self):
1338 if self.closed:
1339 return
1340 try:
1341 super().close()
1342 # Flush any data from the compressor, and update header info
1343 if self._compressor:
1344 buf = self._compressor.flush()
1345 self._compress_size += len(buf)
1346 self._fileobj.write(buf)
1347 self._zinfo.compress_size = self._compress_size
1348 else:
1349 self._zinfo.compress_size = self._file_size
1350 self._zinfo.CRC = self._crc
1351 self._zinfo.file_size = self._file_size
1352
1353 if not self._zip64:
1354 if self._file_size > ZIP64_LIMIT:
1355 raise RuntimeError("File size too large, try using force_zip64")
1356 if self._compress_size > ZIP64_LIMIT:
1357 raise RuntimeError("Compressed size too large, try using force_zip64")
1358
1359 # Write updated header info
1360 if self._zinfo.flag_bits & _MASK_USE_DATA_DESCRIPTOR:
1361 # Write CRC and file sizes after the file data
1362 fmt = '<LLQQ' if self._zip64 else '<LLLL'
1363 self._fileobj.write(struct.pack(fmt, _DD_SIGNATURE, self._zinfo.CRC,
1364 self._zinfo.compress_size, self._zinfo.file_size))
1365 self._zipfile.start_dir = self._fileobj.tell()
1366 else:
1367 # Seek backwards and write file header (which will now include
1368 # correct CRC and file sizes)
1369
1370 # Preserve current position in file
1371 self._zipfile.start_dir = self._fileobj.tell()
1372 self._fileobj.seek(self._zinfo.header_offset)
1373 self._fileobj.write(self._zinfo.FileHeader(self._zip64))
1374 self._fileobj.seek(self._zipfile.start_dir)
1375
1376 # Successfully written: Add file to our caches
1377 self._zipfile.filelist.append(self._zinfo)
1378 self._zipfile.NameToInfo[self._zinfo.filename] = self._zinfo
1379 finally:
1380 self._zipfile._writing = False
1381
1382
1383
1384 class ZipFile:
1385 """ Class with methods to open, read, write, close, list zip files.
1386
1387 z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True,
1388 compresslevel=None)
1389
1390 file: Either the path to the file, or a file-like object.
1391 If it is a path, the file will be opened and closed by ZipFile.
1392 mode: The mode can be either read 'r', write 'w', exclusive create 'x',
1393 or append 'a'.
1394 compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
1395 ZIP_BZIP2 (requires bz2), ZIP_LZMA (requires lzma), or
1396 ZIP_ZSTANDARD (requires compression.zstd).
1397 allowZip64: if True ZipFile will create files with ZIP64 extensions when
1398 needed, otherwise it will raise an exception when this would
1399 be necessary.
1400 compresslevel: None (default for the given compression type) or an integer
1401 specifying the level to pass to the compressor.
1402 When using ZIP_STORED or ZIP_LZMA this keyword has no effect.
1403 When using ZIP_DEFLATED integers 0 through 9 are accepted.
1404 When using ZIP_BZIP2 integers 1 through 9 are accepted.
1405 When using ZIP_ZSTANDARD integers -7 though 22 are common,
1406 see the CompressionParameter enum in compression.zstd for
1407 details.
1408
1409 """
1410
1411 fp = None # Set here since __del__ checks it
1412 _windows_illegal_name_trans_table = None
1413 _ignore_invalid_names = False
1414
1415 def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
1416 compresslevel=None, *, strict_timestamps=True, metadata_encoding=None):
1417 """Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
1418 or append 'a'."""
1419 if mode not in ('r', 'w', 'x', 'a'):
1420 raise ValueError("ZipFile requires mode 'r', 'w', 'x', or 'a'")
1421
1422 _check_compression(compression)
1423
1424 self._allowZip64 = allowZip64
1425 self._didModify = False
1426 self.debug = 0 # Level of printing: 0 through 3
1427 self.NameToInfo = {} # Find file info given name
1428 self.filelist = [] # List of ZipInfo instances for archive
1429 self.compression = compression # Method of compression
1430 self.compresslevel = compresslevel
1431 self.mode = mode
1432 self.pwd = None
1433 self._comment = b''
1434 self._strict_timestamps = strict_timestamps
1435 self.metadata_encoding = metadata_encoding
1436
1437 # Check that we don't try to write with nonconforming codecs
1438 if self.metadata_encoding and mode != 'r':
1439 raise ValueError(
1440 "metadata_encoding is only supported for reading files")
1441
1442 # Check if we were passed a file-like object
1443 if isinstance(file, os.PathLike):
1444 file = os.fspath(file)
1445 if isinstance(file, str):
1446 # No, it's a filename
1447 self._filePassed = 0
1448 self.filename = file
1449 modeDict = {'r' : 'rb', 'w': 'w+b', 'x': 'x+b', 'a' : 'r+b',
1450 'r+b': 'w+b', 'w+b': 'wb', 'x+b': 'xb'}
1451 filemode = modeDict[mode]
1452 while True:
1453 try:
1454 self.fp = io.open(file, filemode)
1455 except OSError:
1456 if filemode in modeDict:
1457 filemode = modeDict[filemode]
1458 continue
1459 raise
1460 break
1461 else:
1462 self._filePassed = 1
1463 self.fp = file
1464 self.filename = getattr(file, 'name', None)
1465 self._fileRefCnt = 1
1466 self._lock = threading.RLock()
1467 self._seekable = True
1468 self._writing = False
1469
1470 try:
1471 if mode == 'r':
1472 self._RealGetContents()
1473 elif mode in ('w', 'x'):
1474 # set the modified flag so central directory gets written
1475 # even if no files are added to the archive
1476 self._didModify = True
1477 try:
1478 self.start_dir = self.fp.tell()
1479 except (AttributeError, OSError):
1480 self.fp = _Tellable(self.fp)
1481 self.start_dir = 0
1482 self._seekable = False
1483 else:
1484 # Some file-like objects can provide tell() but not seek()
1485 try:
1486 self.fp.seek(self.start_dir)
1487 except (AttributeError, OSError):
1488 self._seekable = False
1489 elif mode == 'a':
1490 try:
1491 # See if file is a zip file
1492 self._RealGetContents()
1493 # seek to start of directory and overwrite
1494 self.fp.seek(self.start_dir)
1495 except BadZipFile:
1496 # file is not a zip file, just append
1497 self.fp.seek(0, 2)
1498
1499 # set the modified flag so central directory gets written
1500 # even if no files are added to the archive
1501 self._didModify = True
1502 self.start_dir = self.fp.tell()
1503 else:
1504 raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
1505 except:
1506 fp = self.fp
1507 self.fp = None
1508 self._fpclose(fp)
1509 raise
1510
1511 def __enter__(self):
1512 return self
1513
1514 def __exit__(self, type, value, traceback):
1515 self.close()
1516
1517 def __repr__(self):
1518 result = ['<%s.%s' % (self.__class__.__module__,
1519 self.__class__.__qualname__)]
1520 if self.fp is not None:
1521 if self._filePassed:
1522 result.append(' file=%r' % self.fp)
1523 elif self.filename is not None:
1524 result.append(' filename=%r' % self.filename)
1525 result.append(' mode=%r' % self.mode)
1526 else:
1527 result.append(' [closed]')
1528 result.append('>')
1529 return ''.join(result)
1530
1531 def _RealGetContents(self):
1532 """Read in the table of contents for the ZIP file."""
1533 fp = self.fp
1534 try:
1535 endrec = _EndRecData(fp)
1536 except OSError:
1537 raise BadZipFile("File is not a zip file")
1538 if not endrec:
1539 raise BadZipFile("File is not a zip file")
1540 if self.debug > 1:
1541 print(endrec)
1542 self._comment = endrec[_ECD_COMMENT] # archive comment
1543
1544 offset_cd, concat = _handle_prepended_data(endrec, self.debug)
1545
1546 # self.start_dir: Position of start of central directory
1547 self.start_dir = offset_cd + concat
1548
1549 if self.start_dir < 0:
1550 raise BadZipFile("Bad offset for central directory")
1551 fp.seek(self.start_dir, 0)
1552 size_cd = endrec[_ECD_SIZE]
1553 data = fp.read(size_cd)
1554 fp = io.BytesIO(data)
1555 total = 0
1556 while total < size_cd:
1557 centdir = fp.read(sizeCentralDir)
1558 if len(centdir) != sizeCentralDir:
1559 raise BadZipFile("Truncated central directory")
1560 centdir = struct.unpack(structCentralDir, centdir)
1561 if centdir[_CD_SIGNATURE] != stringCentralDir:
1562 raise BadZipFile("Bad magic number for central directory")
1563 if self.debug > 2:
1564 print(centdir)
1565 filename = fp.read(centdir[_CD_FILENAME_LENGTH])
1566 orig_filename_crc = crc32(filename)
1567 flags = centdir[_CD_FLAG_BITS]
1568 if flags & _MASK_UTF_FILENAME:
1569 # UTF-8 file names extension
1570 filename = filename.decode('utf-8')
1571 else:
1572 # Historical ZIP filename encoding
1573 filename = filename.decode(self.metadata_encoding or 'cp437')
1574 # Create ZipInfo instance to store file information
1575 x = ZipInfo(filename)
1576 x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
1577 x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
1578 x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET]
1579 (x.create_version, x.create_system, x.extract_version, x.reserved,
1580 x.flag_bits, x.compress_type, t, d,
1581 x.CRC, x.compress_size, x.file_size) = centdir[1:12]
1582 if x.extract_version > MAX_EXTRACT_VERSION:
1583 raise NotImplementedError("zip file version %.1f" %
1584 (x.extract_version / 10))
1585 x.volume, x.internal_attr, x.external_attr = centdir[15:18]
1586 # Convert date/time code to (year, month, day, hour, min, sec)
1587 x._raw_time = t
1588 x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
1589 t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
1590 x._decodeExtra(orig_filename_crc)
1591 x.header_offset = x.header_offset + concat
1592 self.filelist.append(x)
1593 self.NameToInfo[x.filename] = x
1594
1595 # update total bytes read from central directory
1596 total = (total + sizeCentralDir + centdir[_CD_FILENAME_LENGTH]
1597 + centdir[_CD_EXTRA_FIELD_LENGTH]
1598 + centdir[_CD_COMMENT_LENGTH])
1599
1600 if self.debug > 2:
1601 print("total", total)
1602
1603 end_offset = self.start_dir
1604 for zinfo in reversed(sorted(self.filelist,
1605 key=lambda zinfo: zinfo.header_offset)):
1606 zinfo._end_offset = end_offset
1607 end_offset = zinfo.header_offset
1608
1609 def namelist(self):
1610 """Return a list of file names in the archive."""
1611 return [data.filename for data in self.filelist]
1612
1613 def infolist(self):
1614 """Return a list of class ZipInfo instances for files in the
1615 archive."""
1616 return self.filelist
1617
1618 def printdir(self, file=None):
1619 """Print a table of contents for the zip file."""
1620 print("%-46s %19s %12s" % ("File Name", "Modified ", "Size"),
1621 file=file)
1622 for zinfo in self.filelist:
1623 date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
1624 print("%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size),
1625 file=file)
1626
1627 def testzip(self):
1628 """Read all the files and check the CRC.
1629
1630 Return None if all files could be read successfully, or the name
1631 of the offending file otherwise."""
1632 chunk_size = 2 ** 20
1633 for zinfo in self.filelist:
1634 try:
1635 # Read by chunks, to avoid an OverflowError or a
1636 # MemoryError with very large embedded files.
1637 with self.open(zinfo.filename, "r") as f:
1638 while f.read(chunk_size): # Check CRC-32
1639 pass
1640 except BadZipFile:
1641 return zinfo.filename
1642
1643 def getinfo(self, name):
1644 """Return the instance of ZipInfo given 'name'."""
1645 info = self.NameToInfo.get(name)
1646 if info is None:
1647 raise KeyError(
1648 'There is no item named %r in the archive' % name)
1649
1650 return info
1651
1652 def setpassword(self, pwd):
1653 """Set default password for encrypted files."""
1654 if pwd and not isinstance(pwd, bytes):
1655 raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__)
1656 if pwd:
1657 self.pwd = pwd
1658 else:
1659 self.pwd = None
1660
1661 @property
1662 def comment(self):
1663 """The comment text associated with the ZIP file."""
1664 return self._comment
1665
1666 @comment.setter
1667 def comment(self, comment):
1668 if not isinstance(comment, bytes):
1669 raise TypeError("comment: expected bytes, got %s" % type(comment).__name__)
1670 # check for valid comment length
1671 if len(comment) > ZIP_MAX_COMMENT:
1672 import warnings
1673 warnings.warn('Archive comment is too long; truncating to %d bytes'
1674 % ZIP_MAX_COMMENT, stacklevel=2)
1675 comment = comment[:ZIP_MAX_COMMENT]
1676 self._comment = comment
1677 self._didModify = True
1678
1679 def read(self, name, pwd=None):
1680 """Return file bytes for name. 'pwd' is the password to decrypt
1681 encrypted files."""
1682 with self.open(name, "r", pwd) as fp:
1683 return fp.read()
1684
1685 def open(self, name, mode="r", pwd=None, *, force_zip64=False):
1686 """Return file-like object for 'name'.
1687
1688 name is a string for the file name within the ZIP file, or a ZipInfo
1689 object.
1690
1691 mode should be 'r' to read a file already in the ZIP file, or 'w' to
1692 write to a file newly added to the archive.
1693
1694 pwd is the password to decrypt files (only used for reading).
1695
1696 When writing, if the file size is not known in advance but may exceed
1697 2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large
1698 files. If the size is known in advance, it is best to pass a ZipInfo
1699 instance for name, with zinfo.file_size set.
1700 """
1701 if mode not in {"r", "w"}:
1702 raise ValueError('open() requires mode "r" or "w"')
1703 if pwd and (mode == "w"):
1704 raise ValueError("pwd is only supported for reading files")
1705 if not self.fp:
1706 raise ValueError(
1707 "Attempt to use ZIP archive that was already closed")
1708
1709 # Make sure we have an info object
1710 if isinstance(name, ZipInfo):
1711 # 'name' is already an info object
1712 zinfo = name
1713 elif mode == 'w':
1714 zinfo = ZipInfo(name)
1715 zinfo.compress_type = self.compression
1716 zinfo.compress_level = self.compresslevel
1717 else:
1718 # Get info object for name
1719 zinfo = self.getinfo(name)
1720
1721 if mode == 'w':
1722 return self._open_to_write(zinfo, force_zip64=force_zip64)
1723
1724 if self._writing:
1725 raise ValueError("Can't read from the ZIP file while there "
1726 "is an open writing handle on it. "
1727 "Close the writing handle before trying to read.")
1728
1729 # Open for reading:
1730 self._fileRefCnt += 1
1731 zef_file = _SharedFile(self.fp, zinfo.header_offset,
1732 self._fpclose, self._lock, lambda: self._writing)
1733 try:
1734 # Skip the file header:
1735 fheader = zef_file.read(sizeFileHeader)
1736 if len(fheader) != sizeFileHeader:
1737 raise BadZipFile("Truncated file header")
1738 fheader = struct.unpack(structFileHeader, fheader)
1739 if fheader[_FH_SIGNATURE] != stringFileHeader:
1740 raise BadZipFile("Bad magic number for file header")
1741
1742 fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
1743 if fheader[_FH_EXTRA_FIELD_LENGTH]:
1744 zef_file.seek(fheader[_FH_EXTRA_FIELD_LENGTH], whence=1)
1745
1746 if zinfo.flag_bits & _MASK_COMPRESSED_PATCH:
1747 # Zip 2.7: compressed patched data
1748 raise NotImplementedError("compressed patched data (flag bit 5)")
1749
1750 if zinfo.flag_bits & _MASK_STRONG_ENCRYPTION:
1751 # strong encryption
1752 raise NotImplementedError("strong encryption (flag bit 6)")
1753
1754 if fheader[_FH_GENERAL_PURPOSE_FLAG_BITS] & _MASK_UTF_FILENAME:
1755 # UTF-8 filename
1756 fname_str = fname.decode("utf-8")
1757 else:
1758 fname_str = fname.decode(self.metadata_encoding or "cp437")
1759
1760 if fname_str != zinfo.orig_filename:
1761 raise BadZipFile(
1762 'File name in directory %r and header %r differ.'
1763 % (zinfo.orig_filename, fname))
1764
1765 if (zinfo._end_offset is not None and
1766 zef_file.tell() + zinfo.compress_size > zinfo._end_offset):
1767 if zinfo._end_offset == zinfo.header_offset:
1768 import warnings
1769 warnings.warn(
1770 f"Overlapped entries: {zinfo.orig_filename!r} "
1771 f"(possible zip bomb)",
1772 skip_file_prefixes=(os.path.dirname(__file__),))
1773 else:
1774 raise BadZipFile(
1775 f"Overlapped entries: {zinfo.orig_filename!r} "
1776 f"(possible zip bomb)")
1777
1778 # check for encrypted flag & handle password
1779 is_encrypted = zinfo.flag_bits & _MASK_ENCRYPTED
1780 if is_encrypted:
1781 if not pwd:
1782 pwd = self.pwd
1783 if pwd and not isinstance(pwd, bytes):
1784 raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__)
1785 if not pwd:
1786 raise RuntimeError("File %r is encrypted, password "
1787 "required for extraction" % name)
1788 else:
1789 pwd = None
1790
1791 return ZipExtFile(zef_file, mode + 'b', zinfo, pwd, True)
1792 except:
1793 zef_file.close()
1794 raise
1795
1796 def _open_to_write(self, zinfo, force_zip64=False):
1797 if force_zip64 and not self._allowZip64:
1798 raise ValueError(
1799 "force_zip64 is True, but allowZip64 was False when opening "
1800 "the ZIP file."
1801 )
1802 if self._writing:
1803 raise ValueError("Can't write to the ZIP file while there is "
1804 "another write handle open on it. "
1805 "Close the first handle before opening another.")
1806
1807 # Size and CRC are overwritten with correct data after processing the file
1808 zinfo.compress_size = 0
1809 zinfo.CRC = 0
1810
1811 zinfo.flag_bits = 0x00
1812 if zinfo.compress_type == ZIP_LZMA:
1813 # Compressed data includes an end-of-stream (EOS) marker
1814 zinfo.flag_bits |= _MASK_COMPRESS_OPTION_1
1815 if not self._seekable:
1816 zinfo.flag_bits |= _MASK_USE_DATA_DESCRIPTOR
1817
1818 if not zinfo.external_attr:
1819 zinfo.external_attr = 0o600 << 16 # permissions: ?rw-------
1820
1821 # Compressed size can be larger than uncompressed size
1822 zip64 = force_zip64 or (zinfo.file_size * 1.05 > ZIP64_LIMIT)
1823 if not self._allowZip64 and zip64:
1824 raise LargeZipFile("Filesize would require ZIP64 extensions")
1825
1826 if self._seekable:
1827 self.fp.seek(self.start_dir)
1828 zinfo.header_offset = self.fp.tell()
1829
1830 self._writecheck(zinfo)
1831 self._didModify = True
1832
1833 self.fp.write(zinfo.FileHeader(zip64))
1834
1835 self._writing = True
1836 return _ZipWriteFile(self, zinfo, zip64)
1837
1838 def extract(self, member, path=None, pwd=None):
1839 """Extract a member from the archive to the current working directory,
1840 using its full name. Its file information is extracted as accurately
1841 as possible. 'member' may be a filename or a ZipInfo object. You can
1842 specify a different directory using 'path'. You can specify the
1843 password to decrypt the file using 'pwd'.
1844 """
1845 if path is None:
1846 path = os.getcwd()
1847 else:
1848 path = os.fspath(path)
1849
1850 return self._extract_member(member, path, pwd)
1851
1852 def extractall(self, path=None, members=None, pwd=None):
1853 """Extract all members from the archive to the current working
1854 directory. 'path' specifies a different directory to extract to.
1855 'members' is optional and must be a subset of the list returned
1856 by namelist(). You can specify the password to decrypt all files
1857 using 'pwd'.
1858 """
1859 if members is None:
1860 members = self.namelist()
1861
1862 if path is None:
1863 path = os.getcwd()
1864 else:
1865 path = os.fspath(path)
1866
1867 for zipinfo in members:
1868 self._extract_member(zipinfo, path, pwd)
1869
1870 @classmethod
1871 def _sanitize_windows_name(cls, arcname, pathsep):
1872 """Replace bad characters and remove trailing dots from parts."""
1873 table = cls._windows_illegal_name_trans_table
1874 if not table:
1875 illegal = ':<>|"?*'
1876 table = str.maketrans(illegal, '_' * len(illegal))
1877 cls._windows_illegal_name_trans_table = table
1878 arcname = arcname.translate(table)
1879 # remove trailing dots and spaces
1880 arcname = (x.rstrip(' .') for x in arcname.split(pathsep))
1881 # rejoin, removing empty parts.
1882 arcname = pathsep.join(x for x in arcname if x)
1883 return arcname
1884
1885 def _extract_member(self, member, targetpath, pwd):
1886 """Extract the ZipInfo object 'member' to a physical
1887 file on the path targetpath.
1888 """
1889 if not isinstance(member, ZipInfo):
1890 member = self.getinfo(member)
1891
1892 # build the destination pathname, replacing
1893 # forward slashes to platform specific separators.
1894 arcname = member.filename
1895 if os.path.sep != '/':
1896 arcname = arcname.replace('/', os.path.sep)
1897 if os.path.altsep and os.path.altsep != '/':
1898 arcname = arcname.replace(os.path.altsep, os.path.sep)
1899 # interpret absolute pathname as relative, remove drive letter or
1900 # UNC path, redundant separators, "." and ".." components.
1901 drive, root, arcname = os.path.splitroot(arcname)
1902 if self._ignore_invalid_names and (drive or root):
1903 return None
1904 if self._ignore_invalid_names and os.path.pardir in arcname.split(os.path.sep):
1905 return None
1906 invalid_path_parts = ('', os.path.curdir, os.path.pardir)
1907 arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
1908 if x not in invalid_path_parts)
1909 if os.path.sep == '\\':
1910 # filter illegal characters on Windows
1911 arcname2 = self._sanitize_windows_name(arcname, os.path.sep)
1912 if self._ignore_invalid_names and arcname2 != arcname:
1913 return None
1914 arcname = arcname2
1915
1916 if not arcname and not member.is_dir():
1917 if self._ignore_invalid_names:
1918 return None
1919 raise ValueError("Empty filename.")
1920
1921 targetpath = os.path.join(targetpath, arcname)
1922 targetpath = os.path.normpath(targetpath)
1923
1924 # Create all upper directories if necessary.
1925 upperdirs = os.path.dirname(targetpath)
1926 if upperdirs and not os.path.exists(upperdirs):
1927 os.makedirs(upperdirs, exist_ok=True)
1928
1929 if member.is_dir():
1930 if not os.path.isdir(targetpath):
1931 try:
1932 os.mkdir(targetpath)
1933 except FileExistsError:
1934 if not os.path.isdir(targetpath):
1935 raise
1936 return targetpath
1937
1938 with self.open(member, pwd=pwd) as source, \
1939 open(targetpath, "wb") as target:
1940 shutil.copyfileobj(source, target)
1941
1942 return targetpath
1943
1944 def _writecheck(self, zinfo):
1945 """Check for errors before writing a file to the archive."""
1946 if zinfo.filename in self.NameToInfo:
1947 import warnings
1948 warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3)
1949 if self.mode not in ('w', 'x', 'a'):
1950 raise ValueError("write() requires mode 'w', 'x', or 'a'")
1951 if not self.fp:
1952 raise ValueError(
1953 "Attempt to write ZIP archive that was already closed")
1954 _check_compression(zinfo.compress_type)
1955 if not self._allowZip64:
1956 requires_zip64 = None
1957 if len(self.filelist) >= ZIP_FILECOUNT_LIMIT:
1958 requires_zip64 = "Files count"
1959 elif zinfo.file_size > ZIP64_LIMIT:
1960 requires_zip64 = "Filesize"
1961 elif zinfo.header_offset > ZIP64_LIMIT:
1962 requires_zip64 = "Zipfile size"
1963 if requires_zip64:
1964 raise LargeZipFile(requires_zip64 +
1965 " would require ZIP64 extensions")
1966
1967 def write(self, filename, arcname=None,
1968 compress_type=None, compresslevel=None):
1969 """Put the bytes from filename into the archive under the name
1970 arcname."""
1971 if not self.fp:
1972 raise ValueError(
1973 "Attempt to write to ZIP archive that was already closed")
1974 if self._writing:
1975 raise ValueError(
1976 "Can't write to ZIP archive while an open writing handle exists"
1977 )
1978
1979 zinfo = ZipInfo.from_file(filename, arcname,
1980 strict_timestamps=self._strict_timestamps)
1981
1982 if zinfo.is_dir():
1983 zinfo.compress_size = 0
1984 zinfo.CRC = 0
1985 self.mkdir(zinfo)
1986 else:
1987 if compress_type is not None:
1988 zinfo.compress_type = compress_type
1989 else:
1990 zinfo.compress_type = self.compression
1991
1992 if compresslevel is not None:
1993 zinfo.compress_level = compresslevel
1994 else:
1995 zinfo.compress_level = self.compresslevel
1996
1997 with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
1998 shutil.copyfileobj(src, dest, 1024*8)
1999
2000 def writestr(self, zinfo_or_arcname, data,
2001 compress_type=None, compresslevel=None):
2002 """Write a file into the archive. The contents is 'data', which
2003 may be either a 'str' or a 'bytes' instance; if it is a 'str',
2004 it is encoded as UTF-8 first.
2005 'zinfo_or_arcname' is either a ZipInfo instance or
2006 the name of the file in the archive."""
2007 if isinstance(data, str):
2008 data = data.encode("utf-8")
2009 if isinstance(zinfo_or_arcname, ZipInfo):
2010 zinfo = zinfo_or_arcname
2011 else:
2012 zinfo = ZipInfo(zinfo_or_arcname)._for_archive(self)
2013
2014 if not self.fp:
2015 raise ValueError(
2016 "Attempt to write to ZIP archive that was already closed")
2017 if self._writing:
2018 raise ValueError(
2019 "Can't write to ZIP archive while an open writing handle exists."
2020 )
2021
2022 if compress_type is not None:
2023 zinfo.compress_type = compress_type
2024
2025 if compresslevel is not None:
2026 zinfo.compress_level = compresslevel
2027
2028 zinfo.file_size = len(data) # Uncompressed size
2029 with self._lock:
2030 with self.open(zinfo, mode='w') as dest:
2031 dest.write(data)
2032
2033 def mkdir(self, zinfo_or_directory_name, mode=511):
2034 """Creates a directory inside the zip archive."""
2035 if isinstance(zinfo_or_directory_name, ZipInfo):
2036 zinfo = zinfo_or_directory_name
2037 if not zinfo.is_dir():
2038 raise ValueError("The given ZipInfo does not describe a directory")
2039 elif isinstance(zinfo_or_directory_name, str):
2040 directory_name = zinfo_or_directory_name
2041 if not directory_name.endswith("/"):
2042 directory_name += "/"
2043 zinfo = ZipInfo(directory_name)
2044 zinfo.compress_size = 0
2045 zinfo.CRC = 0
2046 zinfo.external_attr = ((0o40000 | mode) & 0xFFFF) << 16
2047 zinfo.file_size = 0
2048 zinfo.external_attr |= 0x10
2049 else:
2050 raise TypeError("Expected type str or ZipInfo")
2051
2052 with self._lock:
2053 if self._seekable:
2054 self.fp.seek(self.start_dir)
2055 zinfo.header_offset = self.fp.tell() # Start of header bytes
2056 if zinfo.compress_type == ZIP_LZMA:
2057 # Compressed data includes an end-of-stream (EOS) marker
2058 zinfo.flag_bits |= _MASK_COMPRESS_OPTION_1
2059
2060 self._writecheck(zinfo)
2061 self._didModify = True
2062
2063 self.filelist.append(zinfo)
2064 self.NameToInfo[zinfo.filename] = zinfo
2065 self.fp.write(zinfo.FileHeader(False))
2066 self.start_dir = self.fp.tell()
2067
2068 def __del__(self):
2069 """Call the "close()" method in case the user forgot."""
2070 self.close()
2071
2072 def close(self):
2073 """Close the file, and for mode 'w', 'x' and 'a' write the ending
2074 records."""
2075 if self.fp is None:
2076 return
2077
2078 if self._writing:
2079 raise ValueError("Can't close the ZIP file while there is "
2080 "an open writing handle on it. "
2081 "Close the writing handle before closing the zip.")
2082
2083 try:
2084 if self.mode in ('w', 'x', 'a') and self._didModify: # write ending records
2085 with self._lock:
2086 if self._seekable:
2087 self.fp.seek(self.start_dir)
2088 self._write_end_record()
2089 finally:
2090 fp = self.fp
2091 self.fp = None
2092 self._fpclose(fp)
2093
2094 def _write_end_record(self):
2095 for zinfo in self.filelist: # write central directory
2096 dt = zinfo.date_time
2097 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
2098 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
2099 extra = []
2100 if zinfo.file_size > ZIP64_LIMIT \
2101 or zinfo.compress_size > ZIP64_LIMIT:
2102 extra.append(zinfo.file_size)
2103 extra.append(zinfo.compress_size)
2104 file_size = 0xffffffff
2105 compress_size = 0xffffffff
2106 else:
2107 file_size = zinfo.file_size
2108 compress_size = zinfo.compress_size
2109
2110 if zinfo.header_offset > ZIP64_LIMIT:
2111 extra.append(zinfo.header_offset)
2112 header_offset = 0xffffffff
2113 else:
2114 header_offset = zinfo.header_offset
2115
2116 extra_data = zinfo.extra
2117 min_version = 0
2118 if extra:
2119 # Append a ZIP64 field to the extra's
2120 extra_data = _Extra.strip(extra_data, (1,))
2121 extra_data = struct.pack(
2122 '<HH' + 'Q'*len(extra),
2123 1, 8*len(extra), *extra) + extra_data
2124
2125 min_version = ZIP64_VERSION
2126
2127 if zinfo.compress_type == ZIP_BZIP2:
2128 min_version = max(BZIP2_VERSION, min_version)
2129 elif zinfo.compress_type == ZIP_LZMA:
2130 min_version = max(LZMA_VERSION, min_version)
2131 elif zinfo.compress_type == ZIP_ZSTANDARD:
2132 min_version = max(ZSTANDARD_VERSION, min_version)
2133
2134 extract_version = max(min_version, zinfo.extract_version)
2135 create_version = max(min_version, zinfo.create_version)
2136 filename, flag_bits = zinfo._encodeFilenameFlags()
2137 centdir = struct.pack(structCentralDir,
2138 stringCentralDir, create_version,
2139 zinfo.create_system, extract_version, zinfo.reserved,
2140 flag_bits, zinfo.compress_type, dostime, dosdate,
2141 zinfo.CRC, compress_size, file_size,
2142 len(filename), len(extra_data), len(zinfo.comment),
2143 0, zinfo.internal_attr, zinfo.external_attr,
2144 header_offset)
2145 self.fp.write(centdir)
2146 self.fp.write(filename)
2147 self.fp.write(extra_data)
2148 self.fp.write(zinfo.comment)
2149
2150 pos2 = self.fp.tell()
2151 # Write end-of-zip-archive record
2152 centDirCount = len(self.filelist)
2153 centDirSize = pos2 - self.start_dir
2154 centDirOffset = self.start_dir
2155 requires_zip64 = None
2156 if centDirCount > ZIP_FILECOUNT_LIMIT:
2157 requires_zip64 = "Files count"
2158 elif centDirOffset > ZIP64_LIMIT:
2159 requires_zip64 = "Central directory offset"
2160 elif centDirSize > ZIP64_LIMIT:
2161 requires_zip64 = "Central directory size"
2162 if requires_zip64:
2163 # Need to write the ZIP64 end-of-archive records
2164 if not self._allowZip64:
2165 raise LargeZipFile(requires_zip64 +
2166 " would require ZIP64 extensions")
2167 zip64endrec = struct.pack(
2168 structEndArchive64, stringEndArchive64,
2169 sizeEndCentDir64 - 12, 45, 45, 0, 0, centDirCount, centDirCount,
2170 centDirSize, centDirOffset)
2171 self.fp.write(zip64endrec)
2172
2173 zip64locrec = struct.pack(
2174 structEndArchive64Locator,
2175 stringEndArchive64Locator, 0, pos2, 1)
2176 self.fp.write(zip64locrec)
2177 centDirCount = min(centDirCount, 0xFFFF)
2178 centDirSize = min(centDirSize, 0xFFFFFFFF)
2179 centDirOffset = min(centDirOffset, 0xFFFFFFFF)
2180
2181 endrec = struct.pack(structEndArchive, stringEndArchive,
2182 0, 0, centDirCount, centDirCount,
2183 centDirSize, centDirOffset, len(self._comment))
2184 self.fp.write(endrec)
2185 self.fp.write(self._comment)
2186 if self.mode == "a":
2187 self.fp.truncate()
2188 self.fp.flush()
2189
2190 def _fpclose(self, fp):
2191 assert self._fileRefCnt > 0
2192 self._fileRefCnt -= 1
2193 if not self._fileRefCnt and not self._filePassed:
2194 fp.close()
2195
2196
2197 class PyZipFile(ZipFile):
2198 """Class to create ZIP archives with Python library files and packages."""
2199
2200 def __init__(self, file, mode="r", compression=ZIP_STORED,
2201 allowZip64=True, optimize=-1):
2202 ZipFile.__init__(self, file, mode=mode, compression=compression,
2203 allowZip64=allowZip64)
2204 self._optimize = optimize
2205
2206 def writepy(self, pathname, basename="", filterfunc=None):
2207 """Add all files from "pathname" to the ZIP archive.
2208
2209 If pathname is a package directory, search the directory and
2210 all package subdirectories recursively for all *.py and enter
2211 the modules into the archive. If pathname is a plain
2212 directory, listdir *.py and enter all modules. Else, pathname
2213 must be a Python *.py file and the module will be put into the
2214 archive. Added modules are always module.pyc.
2215 This method will compile the module.py into module.pyc if
2216 necessary.
2217 If filterfunc(pathname) is given, it is called with every argument.
2218 When it is False, the file or directory is skipped.
2219 """
2220 pathname = os.fspath(pathname)
2221 if filterfunc and not filterfunc(pathname):
2222 if self.debug:
2223 label = 'path' if os.path.isdir(pathname) else 'file'
2224 print('%s %r skipped by filterfunc' % (label, pathname))
2225 return
2226 dir, name = os.path.split(pathname)
2227 if os.path.isdir(pathname):
2228 initname = os.path.join(pathname, "__init__.py")
2229 if os.path.isfile(initname):
2230 # This is a package directory, add it
2231 if basename:
2232 basename = "%s/%s" % (basename, name)
2233 else:
2234 basename = name
2235 if self.debug:
2236 print("Adding package in", pathname, "as", basename)
2237 fname, arcname = self._get_codename(initname[0:-3], basename)
2238 if self.debug:
2239 print("Adding", arcname)
2240 self.write(fname, arcname)
2241 dirlist = sorted(os.listdir(pathname))
2242 dirlist.remove("__init__.py")
2243 # Add all *.py files and package subdirectories
2244 for filename in dirlist:
2245 path = os.path.join(pathname, filename)
2246 root, ext = os.path.splitext(filename)
2247 if os.path.isdir(path):
2248 if os.path.isfile(os.path.join(path, "__init__.py")):
2249 # This is a package directory, add it
2250 self.writepy(path, basename,
2251 filterfunc=filterfunc) # Recursive call
2252 elif ext == ".py":
2253 if filterfunc and not filterfunc(path):
2254 if self.debug:
2255 print('file %r skipped by filterfunc' % path)
2256 continue
2257 fname, arcname = self._get_codename(path[0:-3],
2258 basename)
2259 if self.debug:
2260 print("Adding", arcname)
2261 self.write(fname, arcname)
2262 else:
2263 # This is NOT a package directory, add its files at top level
2264 if self.debug:
2265 print("Adding files from directory", pathname)
2266 for filename in sorted(os.listdir(pathname)):
2267 path = os.path.join(pathname, filename)
2268 root, ext = os.path.splitext(filename)
2269 if ext == ".py":
2270 if filterfunc and not filterfunc(path):
2271 if self.debug:
2272 print('file %r skipped by filterfunc' % path)
2273 continue
2274 fname, arcname = self._get_codename(path[0:-3],
2275 basename)
2276 if self.debug:
2277 print("Adding", arcname)
2278 self.write(fname, arcname)
2279 else:
2280 if pathname[-3:] != ".py":
2281 raise RuntimeError(
2282 'Files added with writepy() must end with ".py"')
2283 fname, arcname = self._get_codename(pathname[0:-3], basename)
2284 if self.debug:
2285 print("Adding file", arcname)
2286 self.write(fname, arcname)
2287
2288 def _get_codename(self, pathname, basename):
2289 """Return (filename, archivename) for the path.
2290
2291 Given a module name path, return the correct file path and
2292 archive name, compiling if necessary. For example, given
2293 /python/lib/string, return (/python/lib/string.pyc, string).
2294 """
2295 def _compile(file, optimize=-1):
2296 import py_compile
2297 if self.debug:
2298 print("Compiling", file)
2299 try:
2300 py_compile.compile(file, doraise=True, optimize=optimize)
2301 except py_compile.PyCompileError as err:
2302 print(err.msg)
2303 return False
2304 return True
2305
2306 file_py = pathname + ".py"
2307 file_pyc = pathname + ".pyc"
2308 pycache_opt0 = importlib.util.cache_from_source(file_py, optimization='')
2309 pycache_opt1 = importlib.util.cache_from_source(file_py, optimization=1)
2310 pycache_opt2 = importlib.util.cache_from_source(file_py, optimization=2)
2311 if self._optimize == -1:
2312 # legacy mode: use whatever file is present
2313 if (os.path.isfile(file_pyc) and
2314 os.stat(file_pyc).st_mtime >= os.stat(file_py).st_mtime):
2315 # Use .pyc file.
2316 arcname = fname = file_pyc
2317 elif (os.path.isfile(pycache_opt0) and
2318 os.stat(pycache_opt0).st_mtime >= os.stat(file_py).st_mtime):
2319 # Use the __pycache__/*.pyc file, but write it to the legacy pyc
2320 # file name in the archive.
2321 fname = pycache_opt0
2322 arcname = file_pyc
2323 elif (os.path.isfile(pycache_opt1) and
2324 os.stat(pycache_opt1).st_mtime >= os.stat(file_py).st_mtime):
2325 # Use the __pycache__/*.pyc file, but write it to the legacy pyc
2326 # file name in the archive.
2327 fname = pycache_opt1
2328 arcname = file_pyc
2329 elif (os.path.isfile(pycache_opt2) and
2330 os.stat(pycache_opt2).st_mtime >= os.stat(file_py).st_mtime):
2331 # Use the __pycache__/*.pyc file, but write it to the legacy pyc
2332 # file name in the archive.
2333 fname = pycache_opt2
2334 arcname = file_pyc
2335 else:
2336 # Compile py into PEP 3147 pyc file.
2337 if _compile(file_py):
2338 if sys.flags.optimize == 0:
2339 fname = pycache_opt0
2340 elif sys.flags.optimize == 1:
2341 fname = pycache_opt1
2342 else:
2343 fname = pycache_opt2
2344 arcname = file_pyc
2345 else:
2346 fname = arcname = file_py
2347 else:
2348 # new mode: use given optimization level
2349 if self._optimize == 0:
2350 fname = pycache_opt0
2351 arcname = file_pyc
2352 else:
2353 arcname = file_pyc
2354 if self._optimize == 1:
2355 fname = pycache_opt1
2356 elif self._optimize == 2:
2357 fname = pycache_opt2
2358 else:
2359 msg = "invalid value for 'optimize': {!r}".format(self._optimize)
2360 raise ValueError(msg)
2361 if not (os.path.isfile(fname) and
2362 os.stat(fname).st_mtime >= os.stat(file_py).st_mtime):
2363 if not _compile(file_py, optimize=self._optimize):
2364 fname = arcname = file_py
2365 archivename = os.path.split(arcname)[1]
2366 if basename:
2367 archivename = "%s/%s" % (basename, archivename)
2368 return (fname, archivename)
2369
2370
2371 def main(args=None):
2372 import argparse
2373
2374 description = 'A simple command-line interface for zipfile module.'
2375 parser = argparse.ArgumentParser(description=description, color=True)
2376 group = parser.add_mutually_exclusive_group(required=True)
2377 group.add_argument('-l', '--list', metavar='<zipfile>',
2378 help='Show listing of a zipfile')
2379 group.add_argument('-e', '--extract', nargs=2,
2380 metavar=('<zipfile>', '<output_dir>'),
2381 help='Extract zipfile into target dir')
2382 group.add_argument('-c', '--create', nargs='+',
2383 metavar=('<name>', '<file>'),
2384 help='Create zipfile from sources')
2385 group.add_argument('-t', '--test', metavar='<zipfile>',
2386 help='Test if a zipfile is valid')
2387 parser.add_argument('--metadata-encoding', metavar='<encoding>',
2388 help='Specify encoding of member names for -l, -e and -t')
2389 args = parser.parse_args(args)
2390
2391 encoding = args.metadata_encoding
2392
2393 if args.test is not None:
2394 src = args.test
2395 with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
2396 badfile = zf.testzip()
2397 if badfile:
2398 print("The following enclosed file is corrupted: {!r}".format(badfile))
2399 print("Done testing")
2400
2401 elif args.list is not None:
2402 src = args.list
2403 with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
2404 zf.printdir()
2405
2406 elif args.extract is not None:
2407 src, curdir = args.extract
2408 with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
2409 zf.extractall(curdir)
2410
2411 elif args.create is not None:
2412 if encoding:
2413 print("Non-conforming encodings not supported with -c.",
2414 file=sys.stderr)
2415 sys.exit(1)
2416
2417 zip_name = args.create.pop(0)
2418 files = args.create
2419
2420 def addToZip(zf, path, zippath):
2421 if os.path.isfile(path):
2422 zf.write(path, zippath, ZIP_DEFLATED)
2423 elif os.path.isdir(path):
2424 if zippath:
2425 zf.write(path, zippath)
2426 for nm in sorted(os.listdir(path)):
2427 addToZip(zf,
2428 os.path.join(path, nm), os.path.join(zippath, nm))
2429 # else: ignore
2430
2431 with ZipFile(zip_name, 'w') as zf:
2432 for path in files:
2433 zippath = os.path.basename(path)
2434 if not zippath:
2435 zippath = os.path.basename(os.path.dirname(path))
2436 if zippath in ('', os.curdir, os.pardir):
2437 zippath = ''
2438 addToZip(zf, path, zippath)
2439
2440
2441 from ._path import ( # noqa: E402
2442 Path,
2443
2444 # used privately for tests
2445 CompleteDirs, # noqa: F401
2446 )