When you hand someone a ZIP — a client delivery, a game asset pack, a digital product — you believe you know what is in it. But a ZIP is usually "compress this whole folder", and folders collect things you never meant to ship:
- a working
.envor a private key - phone photos that still carry their GPS position in Exif
-
Thumbs.db,.DS_Store,__MACOSX/ - file names in Japanese, Chinese, Cyrillic… that turn into mojibake when the other person extracts them
This post is a short script that checks all of these by reading the ZIP's directory — nothing is extracted to disk — and the results of running it on test archives.
The script
"""Check a ZIP before you hand it to someone, without extracting it. Only the GPS check needs Pillow (pip install pillow). Usage: python zip_precheck.py product.zip (lists what it found and exits with 1 if anything was found) """ import sys import zipfile from pathlib import PurePosixPath SECRET_NAMES = {".env", "id_rsa", "id_ed25519", ".npmrc", ".pypirc", "credentials.json"} JUNK_NAMES = {"thumbs.db", "desktop.ini", ".ds_store"} JUNK_DIRS = {"__macosx", ".git", "__pycache__", "node_modules"} def has_gps(data): """Does the photo's Exif have a GPS block (tag 0x8825)? Needs Pillow.""" from io import BytesIO from PIL import Image try: with Image.open(BytesIO(data)) as im: return bool(im.getexif().get_ifd(0x8825)) except Exception: # not readable as an image: not judged here return False def check(path): problems = [] with zipfile.ZipFile(path) as z: for info in z.infolist(): name = info.filename parts = PurePosixPath(name).parts base = parts[-1].lower() if parts else "" if base in SECRET_NAMES or base.endswith((".pem", ".key")): problems.append(("possible secret", name)) if base in JUNK_NAMES or any(p.lower() in JUNK_DIRS for p in parts[:-1]): problems.append(("junk file", name)) if name.startswith("/") or ".." in parts: problems.append(("unsafe path", name)) if not name.isascii() and not info.flag_bits & 0x800: problems.append(("non-ASCII name without the UTF-8 flag (may be garbled when extracted)", name)) if base.endswith((".jpg", ".jpeg", ".tif", ".tiff", ".png", ".webp")) and has_gps(z.read(info)): problems.append(("location (GPS)", name)) return problems if __name__ == "__main__": found = check(sys.argv[1]) for kind, name in found: print(f"{kind}: {name}") print("nothing found" if not found else f"{len(found)} found") sys.exit(1 if found else 0) It exits with 1 when it finds something, so you can put it in front of an upload step in a build script or CI job.
What it looks at
1. Names alone
ZipFile.infolist() returns the archive's directory without extracting anything. Names like .env, id_rsa or *.pem are reported as possible secrets; Thumbs.db, .DS_Store and anything under __MACOSX/ or .git/ as junk. A name that starts with / or contains .. would be written outside the target folder by a careless extractor (the classic "zip slip"), so it is reported too.
2. The UTF-8 flag on non-ASCII names
Every entry in a ZIP has a general-purpose flag; bit 11 (0x800) says "this name is UTF-8". Without it, the extracting side has to guess the encoding — the spec's default is CP437, and Windows in many locales uses the local code page instead. So a non-ASCII name stored without the flag shows up differently depending on who opens it. Python's zipfile sets the flag automatically for non-ASCII names; older archivers on Japanese Windows, for example, write CP932 bytes with no flag.
3. GPS in photos
Exif keeps GPS data in the IFD under tag 0x8825. If Pillow's getexif().get_ifd(0x8825) is not empty, the photo carries a location. z.read() loads the entry into memory only; nothing is written to disk.
Running it on archives with planted problems
I built test ZIPs with known problems and ran the script on each (Python 3.13.12, Pillow 12.3.0):
| What was in the ZIP | Output |
|---|---|
pack/.env, pack/Thumbs.db, __MACOSX/pack/._a.png, pack/readme.txt | possible secret: pack/.env, junk file: pack/Thumbs.db, junk file: __MACOSX/pack/._a.png — exit 1 |
| a JPEG with a GPS block and one without | only location (GPS): photos/with_gps.jpg — exit 1 |
説明_utf8.txt written by Python (flag set) | nothing found — exit 0 |
説明.txt written as CP932 bytes with no flag | non-ASCII name without the UTF-8 flag …: Éαû╛.txt — exit 1 |
pack/readme.txt only | nothing found — exit 0 |
Éαû╛.txt is how 説明.txt ("readme" in Japanese) looks when its CP932 bytes are read as CP437 — which is what a receiver may see too.
What it does not do
- It does not look inside files. An API key pasted into a
.txtis not found — only names are checked. - It does not open nested ZIPs or password-protected ones.
- PNG and WebP locations are only seen when they are stored as Exif.
- The junk list is my own. Some deliveries legitimately include
.git/; adjust the sets to your case.
The order that has worked for me: rebuild the ZIP, run the check again, and only then upload.
This article was created with the help of AI (Claude Code). The script and every result in the table were run and checked before publishing.