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