Can you repair a corrupt EPUB file?
Often, yes, but it depends on what kind of corrupt it is. An EPUB is a ZIP archive with strict rules about how it's packed. If the archive itself is sound and only the packing or the list of files is wrong, you can unzip it, correct it and zip it up again, and the book comes back whole. If bytes of the file are damaged or missing, as with a download that stopped early, a tool can rescue the chapters that survived, but nothing rebuilds the ones that didn't. For those, get a fresh copy.
First, find out which kind you have
Work on a copy, and test it in Terminal before you change anything:
unzip -t book.epub
UnZip's manual says -t extracts each file in memory and compares its CRC, a checksum, with the one stored in the archive. If every file passes, the archive's bytes are intact, and the fault is in how the book is packed or in its list of files. Both can be repaired. A CRC error on a file means its bytes are damaged. If unzip reports "End-of-central-directory signature not found", it can't find the index at the end of the archive: the file is cut short, or isn't a ZIP at all.
EPUBCheck tells the two cases apart in its own words. EPUBCheck prints the codes with a hyphen (PKG-003); the reference writes them with an underscore. Its messages reference has PKG_003, "Unable to read EPUB file header. This is likely a corrupted EPUB file.", and the fatal PKG_004, "Corrupted EPUB ZIP header." Those are the damaged kind. The packaging faults have codes of their own, and they're the ones you can repair.
Packaging faults, and how to repack
The EPUB 3.3 specification puts tight rules on a 20-byte file called mimetype. It must be the first file in the archive, it must hold application/epub+zip with no whitespace before or after, it must not be compressed, and its ZIP header must have no extra field. EPUBCheck reports three of these faults by code: PKG_006 (missing or not first), PKG_007 (wrong contents) and PKG_005 (an extra field). The ZIP rules of the same specification allow only two ways of storing the other files, stored or Deflate, and no ZIP encryption.
The usual cause is a book that someone unzipped, edited and zipped again with an ordinary archiver, which puts the files in any order and compresses all of them. The cure is to pack it again by hand. Unzip the book into a folder, open Terminal in that folder, and run:
zip -X0 ../fixed.epub mimetype
zip -rX9 ../fixed.epub . -x mimetype
The first line stores mimetype on its own, uncompressed (-0) and without the extra file attributes (-X) that Info-ZIP's zip otherwise adds on Unix, so it becomes entry number one. The second adds everything else to the same archive with full Deflate compression (-9) and leaves out mimetype (-x). Adding to an archive doesn't move what's already in it, so mimetype stays first; zipinfo fixed.epub lists the entries in order if you want to check. The options are all in the zip manual. Type zip -v to see whether you have Info-ZIP's tools.
EPUBCheck can do the packing too. Its command line reference says --mode exp checks an unzipped book folder, and --save creates the .epub package after the check:
epubcheck --mode exp book-folder/ --save
Errors in the list of files
Inside the archive, the package file (the OPF) has a manifest that lists every file of the book (what is inside an EPUB walks through it). When the list and the files disagree, EPUBCheck reports RSC_001 ("File … could not be found"), RSC_007 (a page refers to a file that isn't in the EPUB), RSC_008 (a page refers to a file that the manifest doesn't declare) or, if you add -u to show the minor usage notes, OPF_003 (a file in the EPUB that the manifest doesn't declare). A missing META-INF/container.xml, the file that points to the OPF, is the fatal RSC_002.
These are repairable when the cause is a typo or a stale entry. Open the OPF in a text editor, correct the href or add the missing item, then repack as above. If the manifest names a chapter that truly isn't in the archive, though, the text of that chapter is gone, and no edit brings it back.
Damaged or truncated files
Here Info-ZIP has two salvage modes, and both write a new file rather than touching the old one. The manual says plain -F "requires a reasonably intact central directory", the index of the entries, and copies the good entries to the new archive. If the archive "is too damaged or the end has been truncated, you must use -FF", which scans the file from the start for the markers between entries. The same page warns that -FF "may create an inconsistent archive".
zip -F book.epub --out salvaged.epub
zip -FF book.epub --out salvaged.epub
Try -F first. If it answers "Can't use -F to fix (try -FF)", run the second line, and answer y when it asks whether this is a single-disk archive. Whatever comes out is a ZIP of what could be found, not yet a working book. Run unzip -t salvaged.epub on it first: the entry that the cut went through is often copied too, and fails the test. Unzip the rest, check which chapters and images are missing against the manifest, and repack it with mimetype first. Run EPUBCheck on the result. A file whose own bytes are damaged stays damaged, so if a chapter you need is among the losses, the honest repair is to download the book again from the shop or the library you got it from.
In Readwyn
Readwyn's “Library health” panel has a check called “Broken EPUB” under “Files”. It reads each EPUB in the library and lists the books whose manifest names a file that isn't in the EPUB, whose EPUB holds a file the manifest doesn't list, or whose table of contents points to a missing file, as well as books that don't open at all (“The EPUB does not open: …”) or have no META-INF/container.xml. It finds them; it doesn't repair them. The check has no fix button, and the file stays as it was, so the repair is the work above, on a copy of the book.