Skip to main content
coreydaley.dev

Recovering what's readable from a damaged archive, and why a cut-off 7z yields nothing

New in Purrchiver 1.1. If you don't see it, update Purrchiver from the App Store.

When an archive is damaged, a normal open is all-or-nothing: the first read error fails it, and you get a message instead of your files. But a download that stopped at 90%, or a file with one bad sector, is usually mostly fine. Everything that was written before the damage is still there.

Purrchiver 1.1 adds recovery: read a damaged archive file by file, keep everything that’s intact, and be honest about the rest. This is how it works, and where the formats themselves set the limits.

Three answers per file, never four

A normal reader stops at the first problem. Recovery keeps going, and gives every file in the archive one of three verdicts:

  • Intact: read to the end without an error, and the size matches what the archive says it should be. For ZIP, that includes the checksum ZIP stores for every file, so even one changed byte is caught.
  • Partly recovered: the damage hit part-way through. The bytes that did come out are kept, but the file is written as photo (incomplete).jpg, so it can never be mistaken for the real thing.
  • Unrecoverable: the archive lists it, but none of its data could be read or written.

There’s no fourth answer that says “probably fine.” A damaged file that looked whole would be worse than a missing one, because you’d trust it.

ZIP keeps two copies of the truth

A ZIP stores each file behind a small local header, and then, at the very end, a central directory: the index that lists every file and where it starts. Normal readers go straight to the end, read the index, and use it.

That’s exactly the part an interrupted download loses. Everything before the cut is there, but the index that points to it isn’t, so a normal reader finds nothing.

So when a ZIP won’t open, or stops part-way, Purrchiver reads it a second time from the front, walking the local headers one after another without the index. In the tests, a ZIP whose central directory has been damaged gets nothing out of the normal reader and every file out of recovery.

The same tests flip a single byte inside one file’s data. Only that file is flagged; every file after it still comes back intact.

Small reads keep more

One detail mattered more than expected. When decompression hits damage, the library underneath fails the whole read call, and it returns none of the data it had already decoded for that call. Reading in 64 KB chunks, a file cut partway through came back with 0 of its 44 KB; reading in 4 KB chunks keeps all but the last few KB before the damage. Recovery reads in small chunks for that reason.

tar is a stream

A compressed tar, like a .tar.gz, is one long stream with files laid end to end, so there’s no index to lose. When a .tar.gz is cut off, every file before the cut is intact. The file the cut landed in comes back as a genuine beginning of the original, minus up to about the last 64 KB that was still being decompressed when the input ran out. Files after the cut are simply gone; they were never downloaded.

7z keeps its index at the end, too, and it’s the only map

A 7z file starts with a tiny header that points to the real index at the end of the file. The index is written last because it can only be filled in once everything has been compressed. Unlike ZIP, there’s no header next to each file, and the data is often compressed as one solid block, so the end of the file is the only place that says where anything starts.

So when a 7z is cut off, there’s no way to find any of its files. Recovery tries, gets nothing, and says so: the result explains that 7z keeps its list of files at the end and that the end is missing. That’s the honest answer, and it’s a good reason to add recovery data when you make a 7z you care about (more on that below).

Update, October 23: a cut-off 7z still yields nothing. The opposite case can now be fixed: a 7z whose start header was never written (7-Zip writes it last, so an interrupted write leaves zeros there) still has its index at the end. Purrchiver finds it and offers Save a Fixed Copy…, leaving the original untouched.

Don’t invent a number

When an archive simply stops, recovery can list the files it got to. It can’t know how many came after the cut: that part of the file never arrived. So the summary says the archive ends early, rather than guessing how many files are missing.

Safe by construction

A damaged archive is an untrusted archive, so recovery gets the same protections as extracting, and a few more:

  • It writes only into a new, empty folder named after the archive, such as Photos (recovered), inside a folder you choose.
  • Every path is checked the same way extraction checks it, so nothing can be written outside that folder. See Zip slip is more than ../.
  • It writes only ordinary files and folders, never links.
  • The same per-file and total size caps as extraction apply, so a damaged archive that claims to hold far more than it should can’t run away. See decompression bombs.

Tested on other people’s broken files

Damage made in a known way only proves so much. So recovery also runs over more than 150 deliberately broken archives from the test suites of libarchive, Go’s archive/zip and archive/tar packages, Apache Commons Compress, yauzl, and py7zr, each with a time limit. Nothing may crash, hang, fail instead of reporting the damage, write outside the folder, or create a link, and every partly recovered file must say so in its name. More on how those sets are used in How Purrchiver is tested.

It found two real bugs on its first runs:

  • A file and a folder with the same name. Go’s dupdir.zip contains both a file called a/b and a folder called a/b holding a/b/c. Recovery hit “file exists” and threw away the whole result. Now it keeps the file and reports a/b/c as unrecoverable.
  • A name macOS won’t accept. A tar from Go’s archive/tar tests holds a file name longer than macOS allows, and it made recovery give up on everything. Now only that one file is reported unrecoverable and the rest come out. A problem with the destination itself, like a full disk, still stops recovery.

Using it

When an archive won’t open or extract and the failure looks like damage, the error offers Recover What’s Readable…. It isn’t offered for a password problem, a safety limit, or a file that isn’t an archive, and only for formats Purrchiver extracts (not disk images or Windows installers). You can also choose Tools ▸ Recover Damaged Archive…, which repairs the archive instead when recovery data sits beside it, or use the Recover Archive action in Shortcuts. Recovery is free.

Update, October 23: since I wrote this, a verify that finds damage (⌘T, or Tools ▸ Verify Archive…) offers recovery too, and the Tools menu has Recover What’s Readable… for the archive in front. A damaged encrypted archive asks for its password and then recovers. A self-extracting .exe is recovered by the archive inside it, and StuffIt files can be recovered as well.

A summary lists every file and what happened to it, with Show in Finder.

Recovery gets back what’s still there. To get back the exact original, including the parts that were lost, you need recovery data saved ahead of time: Purrchiver can add a standard PAR2 file when it creates an archive (part of the unlock), and repair the archive from it later for free. The user-facing details are in Repair a damaged or corrupt ZIP on a Mac and the recovery help page.

Related posts