import zipfile
from io import BytesIO
# Create zip in memory
buf = BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr("hello.txt", "Hello, World!")
zf.writestr("data.txt", "Some data here")
# Read zip
buf.seek(0)
with zipfile.ZipFile(buf, "r") as zf:
print(zf.namelist())
print(zf.read("hello.txt"))
Output
Click "Run" to execute your code
The zipfile module is part of Python's standard library. Read and write ZIP archives.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?