Compression and Archives Modules

zipfile

Reads and creates ZIP archives, lists members and accesses their bytes without extracting to disk. ZIP members can use different compression methods. Inspect names and declared sizes before extraction, limit resource use, and never assume archive paths are safe. CRC checks are not authenticity checks.

Formal Syntax & Structure

python syntaxpython
import zipfile

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from io import BytesIOfrom zipfile import ZipFilebuffer = BytesIO()with ZipFile(buffer, "w") as archive:    archive.writestr("note.txt", "ready")with ZipFile(buffer) as archive:    print(archive.namelist())    print(archive.read("note.txt").decode("utf-8"))
Python Reference