MCPcopy Index your code
hub / github.com/python/cpython / _RealGetContents

Method _RealGetContents

Lib/zipfile/__init__.py:1530–1606  ·  view source on GitHub ↗

Read in the table of contents for the ZIP file.

(self)

Source from the content-addressed store, hash-verified

1528 return ''.join(result)
1529
1530 def _RealGetContents(self):
1531 """Read in the table of contents for the ZIP file."""
1532 fp = self.fp
1533 try:
1534 endrec = _EndRecData(fp)
1535 except OSError:
1536 raise BadZipFile("File is not a zip file")
1537 if not endrec:
1538 raise BadZipFile("File is not a zip file")
1539 if self.debug > 1:
1540 print(endrec)
1541 self._comment = endrec[_ECD_COMMENT] # archive comment
1542
1543 offset_cd, concat = _handle_prepended_data(endrec, self.debug)
1544
1545 # self.start_dir: Position of start of central directory
1546 self.start_dir = offset_cd + concat
1547
1548 if self.start_dir < 0:
1549 raise BadZipFile("Bad offset for central directory")
1550 fp.seek(self.start_dir, 0)
1551 size_cd = endrec[_ECD_SIZE]
1552 data = fp.read(size_cd)
1553 fp = io.BytesIO(data)
1554 total = 0
1555 while total < size_cd:
1556 centdir = fp.read(sizeCentralDir)
1557 if len(centdir) != sizeCentralDir:
1558 raise BadZipFile("Truncated central directory")
1559 centdir = struct.unpack(structCentralDir, centdir)
1560 if centdir[_CD_SIGNATURE] != stringCentralDir:
1561 raise BadZipFile("Bad magic number for central directory")
1562 if self.debug > 2:
1563 print(centdir)
1564 filename = fp.read(centdir[_CD_FILENAME_LENGTH])
1565 orig_filename_crc = crc32(filename)
1566 flags = centdir[_CD_FLAG_BITS]
1567 if flags & _MASK_UTF_FILENAME:
1568 # UTF-8 file names extension
1569 filename = filename.decode('utf-8')
1570 else:
1571 # Historical ZIP filename encoding
1572 filename = filename.decode(self.metadata_encoding or 'cp437')
1573 # Create ZipInfo instance to store file information
1574 x = ZipInfo(filename)
1575 x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
1576 x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
1577 x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET]
1578 (x.create_version, x.create_system, x.extract_version, x.reserved,
1579 x.flag_bits, x.compress_type, t, d,
1580 x.CRC, x.compress_size, x.file_size) = centdir[1:12]
1581 if x.extract_version > MAX_EXTRACT_VERSION:
1582 raise NotImplementedError("zip file version %.1f" %
1583 (x.extract_version / 10))
1584 x.volume, x.internal_attr, x.external_attr = centdir[15:18]
1585 # Convert date/time code to (year, month, day, hour, min, sec)
1586 x._raw_time = t
1587 x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,

Callers 1

__init__Method · 0.95

Calls 11

seekMethod · 0.95
readMethod · 0.95
_decodeExtraMethod · 0.95
_EndRecDataFunction · 0.85
BadZipFileClass · 0.85
_handle_prepended_dataFunction · 0.85
crc32Function · 0.85
ZipInfoClass · 0.85
unpackMethod · 0.80
decodeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected