Skip to main content

How to Read a Zip File Inside Another Zip File in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some downloads are zips of zips: an outer archive that holds other archives, each with its own files. Python can read a file from the inner archive without extracting anything to disk: read the inner zip as bytes and open those bytes as an archive of their own. This guide builds a small zip of zips and reads a report out of it, with real output from Python 3.15.2.

Syntax​

Syntax
with zipfile.ZipFile(outer_path) as outer:
inner_bytes = outer.read(inner_name)

with zipfile.ZipFile(io.BytesIO(inner_bytes)) as inner:
data = inner.read(name)
PartWhat it means
outer.read(inner_name)The inner zip file's content, as bytes
io.BytesIO(inner_bytes)Wraps the bytes in an object that behaves like a file kept in memory
zipfile.ZipFile(...)Opens that in-memory file as an archive, like any zip on disk
inner.read(name)One file from the inner archive, as bytes

zipfile.ZipFile accepts a path or a file object, and How to Read a File Inside a Zip Archive in Python covers namelist() and read() on their own.

Step 1: Make a Zip of Zips​

The example is bundle.zip, which holds reports.zip, which holds report.txt. Create make_nested_zip.py:

make_nested_zip.py
import io
import zipfile

inner = io.BytesIO()
with zipfile.ZipFile(inner, "w") as archive:
archive.writestr("report.txt", "All tests passed\n")

with zipfile.ZipFile("bundle.zip", "w") as archive:
archive.writestr("reports.zip", inner.getvalue())
Terminal
uv run python make_nested_zip.py

It prints nothing. The inner archive is built in memory, in an io.BytesIO, and stored in bundle.zip as a file named reports.zip.

Step 2: Read the File in the Inner Zip​

Create read_nested_zip.py:

The read_nested_zip.py script in a macOS-style code window: it opens bundle.zip, reads reports.zip from it as bytes, opens those bytes with zipfile.ZipFile and io.BytesIO, then prints the inner archive's namelist and the decoded text of report.txt.

read_nested_zip.py
import io
import zipfile

with zipfile.ZipFile("bundle.zip") as outer:
inner_bytes = outer.read("reports.zip")

with zipfile.ZipFile(io.BytesIO(inner_bytes)) as inner:
print(inner.namelist())
print(inner.read("report.txt").decode("utf-8"), end="")
Terminal
uv run python read_nested_zip.py
Output
['report.txt']
All tests passed

A terminal running uv run python read_nested_zip.py: the inner archive's list, report.txt, then its text, All tests passed.

Nothing was extracted: the inner archive only ever existed in memory.

Common Mistakes​

MistakeWhat happensDo this instead
Asking the outer archive for report.txtKeyError: "There is no item named 'report.txt' in the archive": the outer archive only holds reports.zipOpen the inner archive first, then read from it
Passing the bytes straight to zipfile.ZipFile(inner_bytes)AttributeError: 'bytes' object has no attribute 'seek': it needs a path or a file objectWrap them: zipfile.ZipFile(io.BytesIO(inner_bytes))

Troubleshooting​

SymptomLikely causeFix
KeyError: "There is no item named '...' in the archive"The name is wrong, or the file is in the other archivePrint each archive's namelist()
zipfile.BadZipFile: File is not a zip fileThe inner file isn't a zip, or the outer download was cut shortCheck the names, and download the outer archive again

FAQ​

Can I skip io.BytesIO?​

Yes. outer.open("reports.zip") returns a file object that zipfile.ZipFile accepts directly, because it supports jumping to any position, which ZipFile needs. io.BytesIO reads the inner archive once and keeps it in memory, which keeps the code simple for small archives.

Does this work more than two levels deep?​

Yes: each zipfile.ZipFile(io.BytesIO(...)) opens one more level, so repeat the two steps for every level.

Conclusion​

outer.read("reports.zip") returned the inner archive as bytes, and zipfile.ZipFile(io.BytesIO(inner_bytes)) opened them like a file on disk, so report.txt could be read without extracting anything.

Additional Resources​