Why ZIP file names come out garbled between Macs and Windows
Someone sends you a ZIP from a Russian, French, or Japanese Windows PC. You
unzip it on a Mac and get ЏаЁўҐв.txt, or R‚sum‚ caf‚.txt, or unzip
stops with Illegal byte sequence. The files are fine; their names were
decoded with the wrong character set. Here’s why ZIP gets this wrong, what
each tool on a Mac actually does, and the fix that’s already installed.
Two ways to store a name, and one bit to tell them apart
The original ZIP format didn’t say what character set file names were in. Tools used whatever the computer was using: on Windows, the “OEM code page” for the PC’s language. That’s CP437 on a US PC, CP850 in Western Europe, CP866 for Russian, CP932 (Shift-JIS) for Japanese, CP936 for Simplified Chinese, and so on. Nothing in the ZIP records which.
Later, the format added a flag, bit 11 of each entry’s “general purpose” flags, meaning “this name is UTF-8”. (There’s also an Info-ZIP extra field that carries a separate Unicode copy of the name.) A reader that sees the flag decodes UTF-8. A reader that doesn’t has to guess, and every tool guesses differently.
Making test files
To see what each tool does, I needed ZIPs with names in specific code pages
and no UTF-8 flag. Python’s zipfile won’t write one directly (it sets the
flag for any non-ASCII name), but a small subclass will:
import zipfile, sys
out, encoding, name = sys.argv[1], sys.argv[2], sys.argv[3]
raw = name.encode(encoding)
class LegacyName(zipfile.ZipInfo):
def _encodeFilenameFlags(self):
return raw, self.flag_bits & ~0x800 # raw bytes, UTF-8 flag off
with zipfile.ZipFile(out, "w") as z:
z.writestr(LegacyName("x.txt"), "test\n")python3 make.py ru.zip cp866 'Привет.txt' gives a ZIP exactly like one
from an old Russian Windows PC. I made one each for CP866, CP850, CP437,
CP932, CP936, CP950 (Traditional Chinese), and CP949 (Korean).
What each Mac tool does
Tested on macOS 27, on a Mac set to English:
| Tool | What happened |
|---|---|
| Archive Utility (double-click) | Guesses. Japanese, Chinese, and Korean names came out right. Russian came out as ЏаЁўҐв.txt and Western European as R‚sum‚ caf‚.txt. |
unzip | Shows ? and garbage characters when listing, and fails to extract with Illegal byte sequence. |
ditto -x -k | Decodes every name as Mac Roman, the classic Mac character set, so Привет comes out as 臮¢•‚. |
7-Zip (7zz) | Its -mcp= option for choosing a code page has no effect on macOS; names come out garbled. |
tar | Fixes all of them, if you tell it the code page. |
The last row is the useful one. The tar that comes with macOS is bsdtar,
built on libarchive, which reads ZIPs too and accepts a hdrcharset option:
tar -tf file.zip --options zip:hdrcharset=CP866
tar -xf file.zip -C out --options zip:hdrcharset=CP866List first to check you picked the right code page, then extract. No install, and it worked for every code page above.
The same problem in reverse
ZIPs made on a Mac aren’t innocent either. I checked the flags on a ZIP made
with ditto -c -k --sequesterRsrc --keepParent, which makes the same ZIP as
the Finder’s Compress: the names are UTF-8, but bit 11 isn’t set (the
flags are 0x8, which only means “the sizes and CRC come after the data”). A tool that doesn’t guess UTF-8 decodes them with its
local code page, so a Mac-made ZIP can show garbled names on some Windows
tools. 7-Zip (7zz a -tzip) sets the flag (0x800), and bsdtar does when it
knows the names are UTF-8.
What this means for archive apps
Two things, both on Purrchiver’s list:
- When reading, an app should do what Archive Utility does and guess, but
also let you choose the code page when the guess is wrong, since libarchive
can already decode any of them. Purrchiver doesn’t convert legacy names
yet; until it does, the
tarcommand above is the fix. - When writing, set the UTF-8 flag on every non-ASCII name, so nobody has to guess.
If you just need your files back, the short version is in Garbled file names after unzipping on a Mac.