Digital publishing
Inside the EPUB Zip Archive: Unzipping an eBook to Fix Broken Code by Hand
The EPUB container specification contains one sentence that explains most failed attempts to repair an ebook by hand: “The mimetype file MUST NOT be compressed or encrypted, and there MUST NOT be an extra field in its ZIP header.”1 The same specification requires that file to be the first entry in the archive and to contain nothing but the string “application/epub+zip.”2 Ordinary ZIP utilities know none of this. An author who decides to unzip an EPUB, correct a typo in a chapter, and compress the folder again with the operating system’s built-in tool can end up with a file that looks identical but that reading systems and validators reject.
This article describes what sits inside the archive, which kinds of errors can sensibly be fixed by hand, and how to rebuild the container so that it conforms to the specification. It draws on the W3C specifications, the documentation of the Info-ZIP zip utility and of EPUBCheck, and the published experience of ebook producers who have done this work for more than a decade.
An EPUB is a ZIP archive with rules that ordinary ZIP tools ignore
EPUB 3.3 became a W3C Recommendation on 13 January 2026, and the W3C now “recommends the wide deployment of this specification as a standard for the Web.”3 The rules for the archive itself are set out in its Open Container Format (OCF) section. The text of that section could not be retrieved in full from the 3.3 Recommendation for this article, so the requirements below are quoted from the EPUB Open Container Format 3.2 specification of 8 May 2019, which the W3C marks as outdated and which points readers to the current standard.4 Readers who need the normative wording should check section 4 of EPUB 3.3 directly.
The 3.2 text sets out several requirements that bear on hand repair. First, the mimetype file must come first in the ZIP archive and must contain the US-ASCII string “application/epub+zip,” with no padding, whitespace, or byte order mark.5 Second, that file must be stored without compression, without encryption, and without an extra field in its ZIP header.6 Third, an OCF ZIP container “MUST include only stored (uncompressed) and Deflate-compressed ZIP entries,” must not use ZIP’s own encryption features, and must not be split across multiple files or storage media.7 Fourth, the container must include a directory named META-INF at its root, and that directory must contain a file called container.xml.8
The container.xml file is the archive’s table of contents in the most literal sense. Each rootfile element in it “MUST identify the location of a Package Document,” and its full-path attribute gives that location as a path relative to the root of the container.9 The Package Document (usually named content.opf, although the name is not fixed) then lists every resource in the book and the reading order. A reading system that opens an EPUB therefore checks the mimetype, reads container.xml, follows the path to the Package Document, and only then reaches the chapters.
File names inside the archive have their own constraints. The OCF 3.2 text requires file names to be UTF-8 encoded and no longer than 255 bytes, forbids a list of characters that includes the slash, colon, asterisk, question mark, angle brackets, backslash, vertical bar, and quotation mark, and forbids a period as the final character.10 All file names in the same directory “MUST be unique following case normalization,” which means that Chapter1.xhtml and chapter1.xhtml cannot sit side by side, even though the specification otherwise treats names as case sensitive.11 These rules matter when an author adds or renames a file by hand, because desktop operating systems permit names that the EPUB container does not.
Unzipping is harmless; rezipping is where books break
Extracting an EPUB does no damage, because nothing is written back to the original file. Leanpub’s support documentation, dated 19 December 2023, describes an EPUB as “a zipped batch of HTML files” and tells authors to make a working folder and run unzip on the file from the macOS Terminal to inspect its contents.12 Any archive tool that understands ZIP will do the same job, and changing the extension from .epub to .zip is a common way to persuade a desktop operating system to open it.
The trouble starts on the return trip. In a December 2017 thread on the Adobe Community forum, a user described renaming an EPUB to .zip, deleting a single div from the HTML, and compressing the folder again, only to find that the result produced an error and would not open on any device.13 In a September 2023 reply in the same thread, the forum member James Gifford explained that the mimetype file has to be stored uncompressed, that the compression built into macOS compresses it anyway, and that the workaround is to write the mimetype file into the archive with a “store uncompressed” command.14 The Leanpub page cited above advises the opposite approach: selecting the unzipped files, choosing Compress in the Finder, and renaming the resulting Archive.zip with an .epub extension.15 The two sources conflict, and the specification’s requirements give no reason to rely on a general-purpose compressor to get the first entry right.
Hidden files are a second hazard. Liz Castro, the author of several EPUB production guides, wrote in July 2011 that macOS places “an invisible file called DS_Store” in folders, that zipping an EPUB folder sweeps it into the archive, and that it “later generates validation errors.”16 The file is present in the container but not declared in the Package Document’s manifest, and validators flag the mismatch.17 A CreativePro article first published in December 2010 records Castro’s view that shareware zipping tools on the Mac had produced EPUBs that sometimes would not validate or open, and that she had “never had any luck” with them.18
The archive usually holds a predictable set of folders
Although the specification fixes only the mimetype file and the META-INF directory, most production tools create a similar layout. A typical EPUB extracted to a folder contains the mimetype file at the top level, the META-INF folder with container.xml (and, in some books, files such as encryption.xml), and a content folder that holds the Package Document, the navigation document, the XHTML chapter files, the stylesheets, the fonts, and the images.19 Leanpub’s guide names that content folder OEBPS and directs authors to the .xhtml files inside it.20 Other tools use names such as OPS or EPUB, so the first step after unzipping is to open container.xml and confirm where the Package Document actually lives.
The chapter files are XHTML, which is stricter than the HTML of an ordinary web page. EPUB 3.3 requires these documents to be conformant XML 1.0 documents.21 A single unclosed tag, an unescaped ampersand, or a mismatched quotation mark around an attribute value makes the file malformed, and a reading system that parses it as XML may refuse to display the chapter at all. This strictness is the reason that hand edits need to be checked by a validator before the book goes anywhere.
The specification also limits what can live outside the archive. EPUB 3.3 permits only certain types of resources to be hosted remotely and states that “EPUB creators MUST store all other resources within the EPUB container.”22 When a hand repair adds an image or a font, the new file belongs inside the container and must be declared in the Package Document’s manifest, or the book fails for the same reason a stray .DS_Store file does.
Hand edits should be small, deliberate, and well-formed
The repairs that suit this method are narrow ones. Typical candidates include a misspelled word that survived proofreading, a broken internal link whose target id was renamed, a stylesheet rule that sets an awkward margin, an image missing its alt attribute, or a stray element left by an export filter. Each of these lives in one identifiable file, and the fix changes a few characters. A repair that touches dozens of files, restructures chapters, or changes the reading order is better done in the source document or in a dedicated editor, because every change to file names or structure has to be mirrored in the Package Document, the navigation document, and any links that point to the changed file.
Renaming is the clearest example of a change that cascades. The calibre editor, which opens EPUB, KEPUB, and AZW3 files and shows “the HTML and CSS that is used internally inside the book files,” handles this automatically: “Renaming a file automatically updates all links and references to it throughout the book.”23 A hand editor working in a plain text program has no such safety net. The author has to search every XHTML file, the Package Document’s manifest and spine, and the navigation document for the old name, and then confirm that the new name satisfies the OCF character and case rules described above.24
Dedicated editors also offer checks that a text editor lacks. The calibre manual describes a Check Book tool that “searches your book for problems that could prevent it working as intended on actual reader devices,” a Fix HTML tool that “converts HTML that cannot be parsed as XML into well-formed XML,” and a Beautify function that reformats files and, according to the manual, “also auto-fixes broken HTML/CSS.”25 Automated fixes of that kind can change markup the author did not intend to touch, so it is prudent to review the result rather than accept it blindly. For a one-character correction, however, a plain text editor and a careful eye remain sufficient, provided the file is saved as UTF-8 and the archive is rebuilt correctly.
Two zip commands rebuild a valid container
The approach most often recommended by ebook producers on macOS and Linux uses the command-line zip program in two passes. Castro’s 2011 post gives the commands as follows, run from inside the unzipped book folder:26
zip -0Xq file.epub mimetype
zip -Xr9Dq file.epub * -x *.DS_Store
The first command creates the archive and adds the mimetype file alone. In the Info-ZIP manual for zip 3.0, -0 “indicates no compression (store all files),” and -X means “Do not save extra file attributes,” so the mimetype entry is stored uncompressed and without the extended attributes that would otherwise appear in an extra field.27 The manual adds that with -X, zip “only includes the Unicode and Zip64 extra fields,” which is a reason to keep the mimetype file name and the archive small and plain.28 Because this command runs first, the mimetype file becomes the first entry in the archive, which satisfies the ordering rule.29
The second command adds everything else. The -r option makes zip “Travel the directory structure recursively,” -9 applies the slowest and most thorough compression, -D tells zip “Do not create entries in the zip archive for directories,” and -x excludes matching files, here every .DS_Store file at any depth.30 Because the archive already exists, zip appends these entries after the mimetype file. The q in both commands only suppresses progress messages. The sketchyTech blog, writing in May 2018, recommends a near-identical single-pass form, zip -Xr9D ../book.epub mimetype META-INF OEBPS -x *.DS_Store, which names mimetype first and relies on zip to order the entries accordingly.31 The zip manual notes that “If zip determines that storing is better than deflation, the entry will be stored instead,” which is likely why the single-pass version usually leaves the tiny mimetype file uncompressed.32 The two-pass version does not depend on that behavior and is the safer choice.
The same sketchyTech post makes a practical suggestion: keep the original .epub and the unzipped folder side by side, and use zip’s update and delete options to change individual entries rather than rebuilding from scratch each time.33 For example, zip -uXr9D ../book.epub . -i OEBPS/chapter.xhtml replaces one changed chapter, and zip -d ../book.epub OEBPS/chapter.xhtml removes an entry.34 Updating an existing archive in place also leaves the original, correctly stored mimetype entry untouched.
Windows users face a different mix of tools. In the 2017 Adobe thread, the answer marked correct describes opening the EPUB in Windows Explorer without extracting it, moving the folder of HTML and CSS files out of the archive to edit them, and then moving it back, with the instruction to “MOVE (don’t copy) the HTML/CSS folder to the desktop.”35 The logic is the same as the update approach: the original mimetype entry is never rewritten. Other forum members in the same thread reported success with eCanCrusher, a drag-and-drop zipping utility, but those are user reports rather than tested claims, and this article does not evaluate the tool.36
EPUBCheck can validate the folder before it is zipped
EPUBCheck, the validator maintained under the W3C, can check an unzipped folder directly. Its command-line documentation lists the option --mode exp, which checks the path “as an expanded EPUB directory,” and gives the example epubcheck --mode exp publication/.37 It also lists --save, which will “Create an EPUB package (.epub) after checking an expanded EPUB directory”; the documentation notes that this option is ignored unless it is combined with --mode exp.38 An author who has edited files in a folder can therefore validate the folder, read the errors, fix them, and let EPUBCheck build the archive, which avoids the zipping problem entirely.
Whichever method builds the archive, the finished .epub should be validated again as a packaged file with the plain epubcheck book.epub form of the command.39 Validation of the folder confirms that the content and the Package Document agree. Validation of the package confirms that the container itself, including the mimetype entry, meets the OCF rules. Leanpub’s guide advises the same final step, telling authors that they “can now open the file as an ebook and check it using an EPUB validator.”40
The error report also guides the hand repair in the first place. Leanpub recommends using “the line numbers and the EPUB validation error report” together to locate a problem inside a specific XHTML file.41 A validator message that names a file and a line is the most efficient map to the broken code, and it gives the author a clear test of whether the fix worked: the message either disappears on the next run or it does not.
Some files inside the archive should be left alone
Not everything in an unzipped EPUB is safe to edit. The OCF specification defines an encryption.xml file in META-INF for resources that have been encrypted or obfuscated, and EPUB 3.3 includes a section on font obfuscation.4243 Obfuscated fonts are deliberately scrambled with a key derived from the publication’s unique identifier,44 so editing that identifier in the Package Document or replacing the font file without regenerating the obfuscation can leave the font unreadable. Books that carry retailer DRM are a separate matter: they are not meant to be opened at all, and this method applies only to files the author produced and has the right to modify.
Hand editing also leaves the source of the problem untouched. If an error came from a word processor style or an export setting, the next export will reproduce it, and every fix made inside the archive will have to be made again. The most durable use of the unzipped EPUB is diagnostic: it shows exactly what the export tool produced, which tells the author what to correct upstream before the next edition, while the hand-repaired file serves as a stopgap until that corrected source exists.
Related articles on selfpublishingtool.com
Platform rules, fees and laws change. Facts in this guide are dated to when they were checked; confirm current terms with the platform or authority before relying on them. Nothing here is legal, tax or financial advice.