Settings

Theme

Recovering Corrupt Zip Files

construct.net

68 points by AshleysBrain · 30 comments

Reader

7 threads
dotancohen

  > recently we had a particularly bad case: a customer sent us an approximately 1 GB project file which was corrupt, for a game already published to Steam, which they'd spent months working on. They told us all their backups were corrupt too... Predictably, WinRAR's repair tool produced a 1 GB ZIP file that contained nothing.
Thirty years of creating and storing zip files on every major and some minor OSes, filesystems, sketchy transfer protocols, and unreliable media make me think that the huge number of issues this company has with corrupted ZIP files has nothing to do with the format or with its customers' bad practices. I think their software might just produce bad zip files.
  • matja

    The more popular your software is, the more likely you'll get bug reports from people with just bad hardware. I know a few applications now that perform a simple memory/CPU test when sending a bug report and reject those that are basically impossible.

    E.g.: 10% of Firefox crashes are caused by bitflips : https://news.ycombinator.com/item?id=47252971

    • ranger_danger

      Firefox was the only program that was crashing on my "Vmin Shift Instability" bugged CPU. Before I even knew what the culprit was, I filed a bug report with Firefox and the first question they asked me was if my CPU was a Raptor Lake (it was).

      • exe34

        It's mind blowing for me whenever the hardware abstraction leaks like this. You (ie Firefox) had to call the instructions in a specific order to cause the hardware to fail.

    • akoboldfrying

      This is right. There's a reason people squat domain names that differ from well-known ones by a single bit flip. Think of how many old, shitty phones there are in the world, built using the cheapest available components and running in someone's crumb-filled, humid pocket right next to their magnetic key fob thing.

  • toast0

    There's tons of marginal hardware out there, and game developers sometimes push hardware pretty hard and may run early drivers, etc.

  • BatFastard

    As the old saying goes, an untested backup is a non backup.

  • shuwix

    Only time .zip failed me was like in early 90's because floppy discs failed, not the .zip part.

  • drfloyd51

    They said as much in their intro.

    Due to a rare defect in our software, sometimes the zip is corrupted.

    We blame the user for not backing up, or backing up corrupted files.

    That is when I stopped reading.

CrendKing

I just tested. 7-Zip is already doing exactly what the author's recovery tool does: if I delete the metadata at the end of the file, it still can open and extract files, while complaining about the harmless "Unexepcet end of data". Further, even if I delete the actual content of the last file, it still can show the directory structure, it just can't extract data of the last file.

cmovq

> Perhaps surprisingly, the ZIP file puts the central directory - which lists the content of the ZIP file - at the end of the file

This is very common for archive files. It lets you easily append a file to the end of the archive (overwriting the directory) followed by the updated directory. If it were at the start, you’d have to rewrite the entire contents of the archive to grow the directory.

  • flomo

    > This is very common for archive files.

    Maybe. But Unix types have always been all-in on tar. They love tar. They continue to use some form of tar to this day.

    This was actually a 'cultural issue' back in the day, because Sun Java JAR files were just ZIPs. For good reason. But that wasn't the blessed holy unix archive format.

  • yjftsjthsd-h

    I always assumed you could work around this by pre-allocating a large enough header space to hold a reasonable sized directory tree. You could even then append a supplementary directory if you run out of space and just stick a pointer in the first one. I'm pretty sure that's just reinventing an actual filesystem, but if it works...

    • bell-cot

      > ... pre-allocating a large enough header space to hold ...

      Isn't the point of compression is to eliminate such wastes of space?

    • akoboldfrying

      Sure, this would also work. But it seems more complicated -- what would be the benefit?

      • yjftsjthsd-h

        Losing the back half of the achieve doesn't lose your directory list. (Truth be told, I prefer the approach of sticking a copy on the front and back for redundancy, but I appreciate that that's a very specific tradeoff)

    • LoganDark

      Isn't this basically FAT without the actual allocation table?

  • ahefner

    Do you really need a central directory? The tar and cpio file formats basically append files serially with a small header preceeding each one, and for most use cases this is sufficient.

    • toast0

      You don't need one, but it's convenient for listing the files in the archive without having to scan through it.

  • LoganDark

    I'm curious, is there a reason why you can't incrementally prepend to files? I haven't seen a filesystem that allows prepending to a file without rewriting the entire file. Is it just because it's not traditional, or is there any particular technical reason why it would be too expensive, or etc?

    • bell-cot

      In a world long accustomed to "append only" files, use cases would be relatively few.

      Vs. the folks creating and testing filesystems would have to do a whole lot of work to add that feature. Followed by the folks doing higher-level software, most of which which need to handle files being prepended to.

      In short - interesting idea, not worth the effort.

    • pestatije

      its a legacy from the tape-storage times

    • gjvc

      the size of the prepended data would rarely be an exact number of blocks such that it became purely a metadata update (see also: inode small file optimisation), and thus you would have a small "hole" in the file between the new data and the existing data, which would have to be bookkept safely.

Dan42

It's an interesting tale of software spelunking and edge cases, but I was wondering about this:

> Things like the uncompressed size and CRC-32 can be disregarded.

If you compute a running crc32 then you could check for that value in addition to the uncompressed size, giving you 8 bytes of precision. And if you decompress data as you go, you could check for the uncompressed size as well, giving you 12 bytes of precision. Surely that's enough to make sure you always find the real boundary of a file in the zip.

shortercode

Zip files are an example of something that works, are used extensively, and have a number of weird design decisions. Some of this just history, other not so much. But good luck replacing them for customers or friends.

They have some cool advantages. Like being able to read directly from the archive without decompressing the whole thing, per file checksum, and skipping compression on parts which won’t benefit. You can also wedge large amounts of arbitrary data into the file without changing it which… is kinda weird and sometimes useful?

Nasty bits are they used 32 bit ints all over the place, so rely on hacks to support larger files. Can’t support true streaming decompression. Implementations can vary quite a lot, as can compatibility. They also have a number of old and weird features that people don’t really use. Like being able to split a zip file into multiple parts, some ancient compression techniques etc.

mrtx01

Why do they use zip at all with all these design flaws? Wouldn't there be better alternatives?

andrewshadura

It seems technically possible to create an append-only ZIP writer that would only add files at the end of a ZIP archive behind the existing central directory, and then write a new central directory including the new files (and excluding the deleted files). That might make it more difficult to irrecoverably damage the archive as most of it contents will likely be preserved after e.g. an abrupt loss of power.

  • tyingq

      -g
      --grow
    
    Grow (append to) the specified zip archive, instead of creating a new one. If this operation fails, zip attempts to restore the archive to its original state. If the restoration fails, the archive might become corrupted. This option is ignored when there's no existing archive or when at least one archive member must be updated or deleted.
    • andrewshadura

      Not the same. Grow overwrites the old central directory in place. My approach would be to keep it where it was and appending a new central directory.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection