all Python 3zipfileterm
zipfile.ZipInfo
ZIP archives
Describes a single file inside a ZIP archive: its name, sizes before and after compression, date and method.
Syntax
zipfile.ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))
Objects from infolist() or getinfo() tell you sizes without extracting: file_size is the original, compress_size is what the archive stores. A ZipInfo you build yourself carries its own settings: the date defaults to 1980-01-01 and compress_type to ZIP_STORED, so writestr(ZipInfo(...)) stores the file uncompressed even in an archive opened with ZIP_DEFLATED. getinfo() with an unknown name raises KeyError rather than returning None.
Examples
import zipfile
with zipfile.ZipFile('x.zip', 'w', compression=zipfile.ZIP_DEFLATED) as z:
z.writestr('auto.txt', 'a' * 1000)
z.writestr(zipfile.ZipInfo('manual.txt'), 'a' * 1000)
with zipfile.ZipFile('x.zip') as z:
for info in z.infolist():
print(info.filename, info.file_size, info.compress_size < 100)
# → auto.txt 1000 True / manual.txt 1000 False — ZipInfo принёс ZIP_STORED
import zipfile
info = zipfile.ZipInfo('note.txt')
print(info.date_time) # → (1980, 1, 1, 0, 0, 0)
with zipfile.ZipFile('y.zip', 'w') as z:
z.writestr('a.txt', 'x')
try:
z.getinfo('missing.txt')
except KeyError as e:
print(type(e).__name__) # → KeyError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy