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

Function _check_zipfile

Lib/zipfile/__init__.py:235–252  ·  view source on GitHub ↗
(fp)

Source from the content-addressed store, hash-verified

233
234
235def _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
254def is_zipfile(filename):
255 """Quickly see if a file is a ZIP file by checking the magic number.

Callers 1

is_zipfileFunction · 0.85

Calls 5

_EndRecDataFunction · 0.85
_handle_prepended_dataFunction · 0.85
unpackMethod · 0.80
seekMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…