What Is an EPUB File? The Ebook Format Inside a ZIP, and How to Open and Fix One
An EPUB file is an ebook saved as a ZIP of web pages, and EPUB files that open are not always complete. Jump to your question below, or work through it in order.
What Is an EPUB File? One ebook, saved as a ZIP of web pages
Short answer
An EPUB file is an ebook saved as a ZIP file of web pages. It contains XHTML chapters, a style sheet, images and a package file that sets the reading order. The IDPF made the format, and the W3C now keeps it.
We tested 53 good EPUB files and made 30 damaged copies of one EPUB file. Three readers gave the same result on only 17 of the 30. Six damaged EPUB files gave at least one reader a book with missing or changed text and no error.
To open EPUB files, read how to open EPUB files. To check one, read the six step check. If yours will not open, begin with the ten causes.
An EPUB file is an ebook saved as a ZIP file of web pages. It contains chapters in XHTML, a style sheet, images and a package file that lists them in reading order. Apple Books, calibre and Adobe Digital Editions open EPUB files, and Amazon's Send to Kindle accepts EPUB files too.
Picture a reader who downloads EPUB files from a library. On a phone they open at once. On an older PC nothing happens, and the EPUB files look broken. They are not broken. The PC has no reader for them, or the reader dislikes one small detail inside the ZIP.
The Library of Congress says the IDPF standardized EPUB 2 in 2007 and approved version 2.0.1 in 2010. It lists its EPUB 3 entry as approved on 11 October 2011. The IANA record names the W3C as the change controller today.
EPUB files are open to anyone. FileInfo says you can rename an EPUB file to .zip and extract it. We did that to 53 good EPUB files with unzip, and all of them passed. The page also warns that editing the contents can corrupt EPUB files.
EPUB files are easy to build and easy to break. One file must come first, one name must match and the XML must be well formed. The study below shows what happened when we broke 30 copies of one EPUB file.
Who makes EPUB files
Online bookstores, libraries and publishers send EPUB files, and so do friends. Tools write them too. We made 8 test EPUB files with Python code, pandoc 3.1.3, calibre 7.6.0 and EbookLib 0.20, and all 8 passed EPUBCheck.
Three questions start most readers off. Is your EPUB file complete? Which app reads your EPUB file? Is it protected? The first answer comes from the check section. The Library of Congress record says protected EPUB files can need a proprietary reader.
EPUB file facts at a glance
| Property | Value |
|---|---|
| Meaning | An ebook saved as a ZIP file of web pages, styles and images. |
| Ending | .epub |
| Made by | The IDPF, and now the W3C. EPUB 2 came in 2007 and EPUB 3 in 2011. |
| Starts with |
The letters PK, then a file named mimetype at byte 30.
|
| Stores | XHTML chapters, CSS, images, a package file and a navigation file. |
| Media type |
application/, per the IANA record.
|
| Versions | EPUB 2.0.1, EPUB 3.0, 3.0.1, 3.2 and 3.3. The package file states 2.0 or 3.0. |
| Opens in | Apple Books, calibre, Adobe Digital Editions and Google Play Books, per FileInfo. |
| Safe to open | Reading the file is safe. EPUB 3 files can contain scripts, as section 9 explains. |
Wrong page? An archive of any kind is a ZIP file. A fixed page document is a PDF file. A Word document is a DOCX file.
What Is Inside an EPUB File? Five blocks, three rules and 11 files
Every EPUB file is a ZIP with a fixed order. The mimetype file comes first, then META-INF/, which names the package file. The package lists the chapters, images and styles and sets the reading order. Our test book has 11 files and 9,089 bytes.
Our smallest test EPUB file shows every part. The listing shows the files in the ZIP that we wrote with Python. The mimetype file is first, and the package file lives in a folder that the container file names.
Bytes File
20 mimetype
227 META-INF/container.xml
1241 OEBPS/content.opf
493 OEBPS/nav.xhtml
664 OEBPS/toc.ncx
713 OEBPS/ch1.xhtml
651 OEBPS/ch2.xhtml
620 OEBPS/ch3.xhtml
96 OEBPS/style.css
3936 OEBPS/images/cover.png
1422 OEBPS/images/fig1.png
10083 11 files
| Part | What it is and what we saw |
|---|---|
| mimetype |
The first file. It has the 20 characters application/ and nothing else. Ours was stored, not compressed, with no extra field.
|
META-INF/
|
A short XML file that names the package file. Ours was 227 bytes and pointed to OEBPS/.
|
| Package file |
The .opf file. It lists metadata, every file in a manifest and the reading order in a spine. Ours was 1,241 bytes.
|
| Navigation |
A nav.xhtml file for EPUB 3 and a toc.ncx file for EPUB 2. Ours had both, at 493 and 664 bytes.
|
| Chapters | XHTML files with the text. Ours had three, from 620 to 713 bytes. |
| Styles and images | One CSS file of 96 bytes and two PNG images of 3,936 and 1,422 bytes. |
Folder names are not fixed. Our script used OEBPS. Of 45 public EPUB files, 31 used EPUB, 7 used OPS and 7 used OEBPS. The container file points readers to the package file, so a reader never guesses the folder.
What the package file lists
The manifest lists every file with an id and a media type. The spine lists ids in reading order. Metadata gives the title, language and identifier. Its version attribute says 2.0 or 3.0, and PRONOM says that is how a tool separates the versions.
EPUB files never show a chapter that is in the manifest and not in the spine. A chapter that is in the ZIP and not in the manifest is invisible to readers that follow the package. Our tests below show both cases.
Why there are two navigation files
EPUB 2 files used an NCX file for the table of contents. EPUB 3 files added a navigation document. Of 45 public EPUB files, all 45 had one, and 27 also kept an NCX file for older readers. Of our 8 test EPUB files, 5 were EPUB 3 and 3 were EPUB 2.
A folder is not a book. Unzip an EPUB file and you see ordinary files. Zip them again with a normal tool and the EPUB file can break, because the first file must be stored and not compressed. The next section shows how.
What Is the Mimetype File in an EPUB File? First, stored and 20 bytes long
The mimetype file must be first in the ZIP, stored and not compressed, with no extra field and the exact text application/. Apps ignore a breach and still open the book. Checkers and some tools do not. We broke the file in 7 ways, and the file command misnamed 6 of the 7.
The IANA record gives a way to spot an EPUB file. The bytes PK and two more start the file, the word mimetype begins at byte 30 and the text application/ begins at byte 38. The PRONOM record and the Library of Congress list the same positions.
That layout only works when the file is stored and comes first. A tool that scans for those bytes finds nothing in a compressed or moved mimetype file. The EPUB file is intact, and the tool calls it an ordinary ZIP.
| Damage | What we saw |
|---|---|
| Second in the ZIP | Read. EPUBCheck fails it. |
| Compressed | Read. EPUBCheck passes it. |
| Ends with a line break | Read. EPUBCheck fails it. |
| Missing | Read. EPUBCheck fails it. |
| Says application/zip | Read. EPUBCheck fails it. |
| Has a ZIP extra field | Read. EPUBCheck fails it. |
| Starts with a byte order mark | Read. EPUBCheck fails it. |
EbookLib, calibre and pandoc read all 7 books with every chapter. EPUBCheck 5.3.0 failed 6 of the 7 and passed the compressed one. Our 74 line script failed all 7, and unzip passed all 7. Python and the ZIP tools care about the ZIP, not the EPUB rules.
What the file command said
The Linux file command, version 5.45, named 23 of our 30 damaged EPUB files EPUB documents. It missed 7. Six of the 7 mimetype copies came back as ZIP data or a Java archive. The copy cut in half still read as an EPUB document, because file looks only at the first bytes.
One result stands out. The copy with a ZIP extra field in the mimetype entry came back as a Java archive. file reads that extra field as a JAR marker. A broken EPUB file can look like a program, and it is not one.
A basic zip command breaks EPUB files in the same way. We zipped our unpacked test book with zip -r and no other option. It left a 28 byte extra field in the mimetype entry. EPUBCheck reported error PKG-005, and our script failed step 2.
How to write the file correctly
Write mimetype first, with the stored method and no extra field, and write every other file after it. Python's zipfile does this when you set compress_type to ZIP_STORED for that entry. On a command line, zip -X0 book.epub mimetype writes the first file and zip -Xr9D book.epub META-INF OEBPS adds the rest. EPUBCheck passed both EPUB files.
Most readers are forgiving. The three readers we tried opened all 7 damaged mimetype files, and the checkers did not. A store can reject an EPUB file that your reader opens. Fix the file before you upload it.
What Is the Difference Between EPUB 2 and EPUB 3? Open the package file and read one number
Open the package file and read its version. A value of 3.0 means EPUB 3 and 2.0 means EPUB 2. EPUB 3 adds HTML5 and a navigation document, and nearly every modern reader opens both. Of our 53 good EPUB files, 50 were EPUB 3 and 3 were EPUB 2.
Version numbers confuse people, because the file says 3.0 even for 3.2 and 3.3 EPUB files. PRONOM registry says the package attribute is 3.0 for every EPUB 3 release and 2.0 for EPUB 2. The finer release is not in that attribute.
| Version | When and what |
|---|---|
| EPUB 2.0.1 | Approved in 2010, after EPUB 2 in 2007. Reflowable text styled with CSS. Our EPUB 2 test used XHTML 1.1 pages and an NCX file, and EPUBCheck passed it under EPUB 2.0.1 rules. The Library of Congress says the standard is no longer actively maintained. |
| EPUB 3.0 | Approved on 11 October 2011. HTML5 pages, a navigation document, audio, video and optional scripts. Also published as ISO/IEC TS 30135 in 2014. |
| EPUB 3.0.1 | Released in 2014. A maintenance release. The Library of Congress says it became ISO/IEC 23736 in 2020. |
| EPUB 3.2 | Effective on 8 May 2019, per Wikipedia. The release between 3.0.1 and 3.3. We did not compare the specifications line by line. |
| EPUB 3.3 | Announced by the W3C on 25 May 2023, per Wikipedia. The newest version we found. The IANA record cites it as the reference. |
The Library of Congress record for EPUB 2 calls the format open but no longer maintained. It lists the later releases in its EPUB 3 record. For most readers, the number matters less than the structure.
Which version your EPUB file is
The short script reads the package file of a test EPUB file that we saved as EPUB 2. It prints one number. Change the file name and the path to match your EPUB file. The path comes from the container file, and it is not always OEBPS.
import zipfile, re
z = zipfile.ZipFile('g03_epub2.epub')
opf = z.read('OEBPS/content.opf').decode()
print(re.search(r'version="([0-9.]+)"', opf).group(1))
2.0
What EPUB 3 added
EPUB 3 pages are HTML5. The format can also contain audio, video and scripts, and the Library of Congress lists JavaScript as a concern for long term care. Six of our 45 public EPUB files declared scripted pages, and 5 had script files.
Old and new both open. EPUB 3 readers open EPUB 2 files, and 27 of the 45 public EPUB files kept an NCX file for older ones. If an EPUB file will not open, its version is rarely the cause. Start with the check.
What Did We Find in 30 Damaged EPUB Files? Three readers agreed on only 16 of them
We tested 53 good EPUB files and made 30 damaged copies of one EPUB file. Three readers gave the same result on only 17 of the 30. Six copies gave at least one reader a book with missing or changed text and no error. EPUBCheck failed 25, and it still passed 5.
Few damaged EPUB files are public, so we built our own. We made 8 good EPUB files with Python, pandoc, calibre and EbookLib. We also packed 45 public EPUB files from the IDPF set, using the set's own script. Apple Books and Kobo made none of them.
Then we damaged one 9,089 byte EPUB file in 30 ways, in five groups. We changed the mimetype file, damaged the ZIP, changed the container file, broke the package file and broke the content files. We read every copy with 3 readers and checked it with 3 more tools.
A reader counts as right when all three chapters come out intact. It is silent when it gives no error and the book has missing or changed text. We tested everything on Linux, and we did not start Apple Books, Kobo or a Windows viewer.
| Tool | Result |
|---|---|
| EbookLib | Version 0.20 for Python. It read 16 of 30 damaged EPUB files, refused 11 and gave 3 a book with missing or changed text and no error. |
| calibre | Version 7.6.0, converting to text. It read 21 of 30, refused 4 and gave 5 a book with missing or changed text. It is the most forgiving reader we tried. |
| pandoc | Version 3.1.3, converting EPUB files to plain text. It read 16 of 30, refused 11 and gave 3 a book with missing or changed text and no error. |
| unzip | Version 6.00, with the test option. It flagged 5 of 30, the 5 with a damaged ZIP. It passed the other 25, including all 7 mimetype copies. |
| EPUBCheck | Version 5.3.0. It failed 25 of 30 and passed 5. The 5 were a compressed mimetype, bad checksums in chapter 2 and the cover, junk after the end and a chapter that was not UTF-8. |
| file command | Version 5.45. It named 23 of 30 damaged EPUB files EPUB documents and missed 7. It also named the cut copy an EPUB document. |
| Our script | A 74 line Python script. It failed 26 of 30, warned on 3 and passed 1. The pass was the copy with junk after the end of the ZIP. |
Read the grid by column. Green means a reader gave all three chapters, or a checker found no fault. Gray means an error. Red means a book with missing or changed text and no error, which is the worst result, because nothing warns you. Blue means a checker flagged the file.
Where the readers disagree
All three readers opened 13 of the 30 damaged EPUB files, and all three refused 3. All three lost the same chapter in 1. They split on the other 13.
The 13 copies that all three opened include all 7 mimetype copies and the copy with a changed directory byte. They also include junk after the ZIP, no identifier, no title, no modified date and a broken tag in chapter 2.
EPUBCheck failed 11 of those 13. The two it passed were the compressed mimetype and the junk after the ZIP. A reader that opens EPUB files is no proof that stores or other apps will accept them.
Six EPUB files that read without an error
The worst cases are quiet. Copy 11 has one wrong byte in chapter 2. EbookLib refused it. calibre dropped chapter 2 and pandoc printed the heading with the text changed. Neither one raised an error. Copy 25 lost the chapter 2 file, and calibre returned the other two chapters without a word.
Copy 22 has a spine that points to an id that is not in the manifest. EbookLib read all three chapters, calibre dropped chapter 2 and pandoc refused. Copy 24 leaves chapter 3 out of the manifest, and all three readers dropped it. Copy 27 has a non UTF-8 letter, and calibre printed a replacement mark.
What the 45 public EPUB files showed
Good files fail too. EbookLib opened all 45 public EPUB files. In pandoc, all 45 converted, and the longest took 194 seconds. For calibre, 41 converted, and 4 valid EPUB files stopped with the error Spine is empty because their pages are images or SVG.
EPUBCheck 5.3.0 failed 2 of the 45. One declares a Dublin Core prefix, which the rules forbid. The other has invalid characters in the links of its navigation file. Our script passed 43 and warned on 2, one with two renditions and one with a chapter file the package does not list.
Read the study with care. It shows what three readers and four tools do with our files. All 30 damaged EPUB files come from one small book, and we made 8 of the 53 good EPUB files ourselves. We used every tool once per file, and we did not start Apple Books, Kobo or a Windows viewer.
What Does Our Expert Say About EPUB Files? A note from Neeraj Singh on checking before you send
Check the EPUB file before you send it or upload it. Readers forgive most faults, and a store or a stricter tool does not. A check takes a second, and it names the failing step. Converting EPUB files comes after that check and not before it.
Expert insight
“An EPUB file looks like one file, so people trust it. It is a ZIP with rules, and most apps forgive a breach. In our tests three readers opened 13 broken copies without a word, and EPUBCheck failed 11 of them. Check the book before you send it to anyone, and keep the original.”
Why a reader is a poor test
Most readers open what they can and say nothing about the rest. In our tests calibre gave five damaged EPUB files without an error and without all the text. A step by step check names the problem, and the check script does it in six steps.
The check has a limit. A pass proves the ZIP, the container, the package and the pages fit together. It does not prove the book is good, and it does not read the text. The one copy that passed our script had junk after the end of the ZIP.
Copy the file before you touch it. Write down the SHA256 hash of the original and work on a copy. The hash shows later which EPUB file you received, and a repair then cannot damage the evidence.
How Do You Open EPUB Files? Phone, Mac, Windows and Python
Open EPUB files with Apple Books on an iPhone, iPad or Mac, with calibre or Adobe Digital Editions on a PC and with a short Python script anywhere. Vendors also list Windows viewers. We tested three readers on Linux, and EbookLib opened all 53 good EPUB files.
FileInfo's program list names Apple Books, calibre, Adobe Digital Editions and Google Play Books. We tested on Linux, so the table marks what we tested and what we did not. Reading an EPUB file and checking one are different jobs, and the next sections cover both.
| Program | What it does |
|---|---|
| Apple Books | Included with macOS and iOS, per FileInfo. Apple's Books guide says File > Import adds many EPUB books and names ACSM, AZW and ODM files as third party formats. We did not test it. |
| calibre | Free for Windows, Mac and Linux, per FileInfo. Version 7.6.0 converted all 8 of our books and 41 of 45 public EPUB files on Linux. We did not use its viewer. |
| Adobe Digital Editions | Free for Windows, Mac, Android and iOS, per FileInfo. We did not test it. |
| Google Play Books | The Android reader that FileInfo lists. We did not test it. |
| Send to Kindle | Wikipedia says Amazon's service dropped the old Kindle format for EPUB in 2022. We did not test it. |
| SysTools EPUB Viewer | A Windows viewer. The vendor says it previews EPUB files and that saving needs the Pro version. We did not test it. |
| Freeviewer EPUB Viewer | A Windows viewer. The vendor says the free version previews EPUB files of any size. We did not test it. |
| EbookLib | A Python module. Version 0.20 opened all 8 of our books and all 45 public EPUB files, and it read their text and metadata. |
Open EPUB files on an iPhone, iPad or Mac
Apple Books is the built in reader. On a Mac, Apple's Books guide says to choose File > Import or drag the file into your library. It says many EPUB books import this way. We did not test Apple Books with any of our EPUB files.
If EPUB files will not import, do not edit them in place. Copy them first, and use our six step check to see which step fails. Apple's page names ACSM, AZW and ODM files as formats that open only in other apps, so those are not ordinary EPUB files.
Open EPUB files on Windows
A PC needs an app. calibre and Adobe Digital Editions are free, per FileInfo. Two vendors also list Windows viewers on their pages. The cards show what those pages say, and we tested neither program.
SysTools EPUB Viewer
Opens and previews EPUB files on Windows, searches them and shows their images and links
Freeviewer EPUB Viewer
Opens EPUB files on Windows and shows them with images and links in the free version
SysTools says its viewer previews EPUB files, searches them and shows images and links. It says the free version is view only and that saving needs the Pro version. It also says the viewer can recover corrupted EPUB files, which we could not test.
Freeviewer says its viewer opens EPUB files of any size on Windows and previews them in the free version. Its page lists Save As in one place and says the free version only previews in another, so test it on a copy before you rely on it.
Viewers show, they do not repair. A viewer shows EPUB files that read. An EPUB file with a broken package file can still fail in it, so use the six steps before you blame the viewer.
Open EPUB files with Python
Install EbookLib with pip install EbookLib. It reads the metadata, the manifest and every page. The script prints the title and the number of pages in a test book we made. It counts the navigation file as a fourth page.
import ebooklib
from ebooklib import epub
book = epub.read_epub('g02_epub3.epub')
print(book.get_metadata('DC', 'title')[0][0])
print(len(list(book.get_items_of_type(ebooklib.ITEM_DOCUMENT))))
Our Test Book
4
EbookLib refused 11 of our 30 damaged EPUB files, and the error message did not say which step broke. If it raises an error, use our six step script to find the step and then repair the ZIP.
How Do You Check an EPUB File? Six steps, and the first failure names the problem
Check an EPUB file with our 74 line script. It goes through six steps in order, which are the ZIP, the mimetype file, the container file, the package file, the manifest with the spine and the pages. It stops at the first failure and names it.
The script uses only Python's own modules. On a good EPUB file it prints one line. The listing shows the command and the result for the 9,089 byte book from the structure section.
python3 check_epub.py g02_epub3.epub
OK all 6 steps passed
The figure follows the order of the checks. Steps 1 and 2 are about the ZIP. Steps 3 and 4 follow the container and the package. Step 5 compares the lists, and step 6 opens the pages. The damage test lists ten causes behind the numbers.
Check EPUB files with Python
The script fails EPUB files that do not start with a ZIP header. It also fails every mimetype file that is not first and stored, every package with no identifier and every spine that points nowhere. It only warns about a missing modified date and an unlisted chapter.
import sys, zipfile, posixpath, unicodedata
import xml.etree.ElementTree as ET
from urllib.parse import unquote
C = '{urn:oasis:names:tc:opendocument:xmlns:container}'
O = '{http://www.idpf.org/2007/opf}'
DC = '{http://purl.org/dc/elements/1.1/}'
class Stop(Exception): pass
def check(path):
warns = []
def fail(step, msg): raise Stop('FAIL step %d: %s' % (step, msg))
try:
raw = open(path, 'rb').read()
if raw[:4] != b'PK\x03\x04': fail(1, 'the file does not start with a ZIP header')
try: z = zipfile.ZipFile(path)
except Exception as e: fail(1, 'the ZIP cannot be read (%s)' % str(e)[:40])
bad = z.testzip()
if bad: fail(1, 'the ZIP entry %s fails its checksum' % bad)
names = z.namelist()
orig = {unicodedata.normalize('NFC', n): n for n in names}
names = list(orig)
def rd(n):
try: return z.read(orig[n])
except Exception as e: fail(1, '%s cannot be read (%s)' % (n, str(e)[:40]))
if 'mimetype' not in names: fail(2, 'there is no mimetype file')
info = z.getinfo('mimetype')
if names[0] != 'mimetype': fail(2, 'mimetype is not the first file')
if info.compress_type != 0: fail(2, 'mimetype is compressed')
if info.extra: fail(2, 'mimetype has a ZIP extra field')
if rd('mimetype') != b'application/epub+zip': fail(2, 'mimetype does not say application/epub+zip')
if 'META-INF/container.xml' not in names: fail(3, 'there is no META-INF/container.xml')
try: opf_path = ET.fromstring(rd('META-INF/container.xml')).find('.//%srootfile' % C).get('full-path')
except Exception as e: fail(3, 'container.xml cannot be parsed (%s)' % str(e)[:40])
if opf_path not in names: fail(3, 'container.xml names %s, which is not in the ZIP' % opf_path)
try: pkg = ET.fromstring(rd(opf_path))
except Exception as e: fail(4, '%s is not well formed (%s)' % (opf_path, str(e)[:40]))
md = pkg.find(O + 'metadata')
for tag in ('identifier', 'title', 'language'):
if md is None or md.find(DC + tag) is None: fail(4, 'the package has no dc:' + tag)
if (pkg.get('version') or '').startswith('3') and not any(m.get('property') == 'dcterms:modified' for m in md.iter(O + 'meta')):
warns.append('WARN the EPUB 3 package has no dcterms:modified')
base, man = posixpath.dirname(opf_path), {}
for it in pkg.iter(O + 'item'):
man[it.get('id')] = it
p = unicodedata.normalize('NFC', posixpath.normpath(posixpath.join(base, unquote(it.get('href')))))
if p not in names: fail(5, 'the manifest lists %s, which is not in the ZIP' % p)
spine = [r.get('idref') for r in pkg.iter(O + 'itemref')]
if not spine: fail(5, 'the spine is empty')
for r in spine:
if r not in man: fail(5, 'the spine points to %s, which is not in the manifest' % r)
for r in spine:
it = man[r]
p = unicodedata.normalize('NFC', posixpath.normpath(posixpath.join(base, unquote(it.get('href')))))
if it.get('media-type') != 'application/xhtml+xml':
if it.get('media-type') == 'text/html': warns.append('WARN %s has the media type text/html' % p)
continue
try:
data = rd(p); ET.fromstring(data); data.decode('utf-8')
except Stop: raise
except Exception as e: fail(6, '%s is not well formed UTF-8 XML (%s)' % (p, str(e)[:40]))
listed = {unicodedata.normalize('NFC', posixpath.normpath(posixpath.join(base, unquote(i.get('href'))))) for i in man.values()}
for n in names:
if n.endswith('.xhtml') and n not in listed: warns.append('WARN %s is in the ZIP but not in the manifest' % n)
except Stop as s:
return [str(s)]
return warns
if __name__ == '__main__':
r = check(sys.argv[1])
if not r: print('OK all 6 steps passed')
for l in r: print(l)
sys.exit(1 if any(l.startswith('FAIL') for l in r) else 0)
Here is what it printed for four damaged EPUB files. The first has the mimetype file second. The second has a spine that points to an unknown id. The third has one page with a broken tag. The fourth lacks chapter 3 in the manifest.
python3 check_epub.py c01.epub
FAIL step 2: mimetype is not the first file
python3 check_epub.py c22.epub
FAIL step 5: the spine points to nochapter, which is not in the manifest
python3 check_epub.py c26.epub
FAIL step 6: OEBPS/ch2.xhtml is not well formed UTF-8 XML (mismatched tag: line 11, column 2)
python3 check_epub.py c24.epub
WARN OEBPS/ch3.xhtml is in the ZIP but not in the manifest
The first line of every answer names the step and the cause. That is the fact a repair needs. The last answer is only a warning, because the file is valid, and readers still lose chapter 3 as the study shows.
What the script found in 30 damaged EPUB files
| Result | Cases |
|---|---|
| Step 1, the ZIP (6) | The ZIP is cut in half, loses its last 100 bytes, starts with junk or has a changed byte in its directory, a page or the cover. |
| Step 2, the mimetype file (7) | The file is second, compressed, missing, wrong, has a line break, has a byte order mark or has an extra field. |
| Step 3, the container file (3) | The container file is missing, names a package file that is not there or the package file itself is gone. |
| Step 4, the package file (3) | The package file is not well formed, has no identifier or has no title. |
| Step 5, manifest and spine (5) | The spine points to an unknown id, is empty or the manifest lists a page, the cover or the navigation file that is gone. |
| Step 6, the pages (2) | A page has a broken tag or a letter that is not UTF-8. |
| Warned, passed (3) | No modified date, a chapter that is not in the manifest or a chapter with the media type text/html. |
| Passed (1) | Junk after the end of the ZIP. All six steps accept it, and so do the three readers. |
The script checks structure and does not scan for malware. It cannot judge the writing, the images or the layout. It reads only the first package file, so it warns about the second rendition in one public book. EPUBCheck is stricter on style rules.
Check an EPUB file by hand
You can do the first two steps in a terminal. The first command lists the files with their storage method, and the first row must be mimetype with the method Stored. The second prints the text of that file. The third tests the ZIP.
unzip -lv g02_epub3.epub | head -4
unzip -p g02_epub3.epub mimetype
unzip -tq g02_epub3.epub
Archive: g02_epub3.epub
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
20 Stored 20 0% 2026-10-22 10:00 2cab616f mimetype
application/epub+zip
No errors detected in compressed data of g02_epub3.epub.
The first row must be mimetype with the method Stored. We tested these commands on Linux with unzip 6.00. We did not try them on a Mac. unzip passed all 7 mimetype copies, so it checks the ZIP and not the EPUB rules.
A pass is not a promise. A good check means the ZIP, the container, the package and the pages fit together. It does not mean the EPUB file is the one that was sent. Our copy with junk after the ZIP passed.
Are EPUB Files Safe to Open? Reading is safe, and the book inside can still contain scripts
Reading an EPUB file is safe. Opening the EPUB file in an app brings the risk of any web page, so judge the sender first. EPUB 3 can contain scripts. Six of 45 public EPUB files declared scripted pages, and none asked for remote resources.
The ending says nothing about safety. An EPUB file is a ZIP of pages, and its safety depends on who wrote it and what it contains. The table lists what to check, and every row ends with its source.
| Check | What to do |
|---|---|
| The sender | An EPUB file is a package of web pages. Links in it work like links on any page, so open nothing from a sender you do not know. Source: Our advice. |
| Reading | EbookLib, calibre, pandoc, unzip, EPUBCheck, file and our script only read bytes. We used them on 83 files, and every SHA256 hash was the same afterward. Source: Our tests. |
| Scripts | EPUB 3 allows scripted pages. Six of 45 public EPUB files declared them and 5 had script files. The Library of Congress lists JavaScript as a care concern. Source: Our tests and Library of Congress. |
| Remote resources | None of the 45 public EPUB files declared remote resources. A book that loads images from the internet lets the sender see that you opened it. Switch remote content off if the app has the option. Source: Our tests. |
| Protection | Protected EPUB files can need a proprietary reader, per the Library of Congress. We tested no protected file and we do not cover how to remove protection. Source: Library of Congress. |
| Unpacking | Unpack a file with a tool you trust, into an empty folder. Our largest public book unpacks to about 36 MB, so check the size first. Source: Our tests. |
| Viewers | Vendors say their viewers open EPUB files. We tested none. Try a viewer on a copy and read the limits of its free version first. Source: Vendor pages. |
Why a reader is safer than an app
A reader prints what it finds. An ebook app builds the pages on screen, with fonts, images and scripts. We checked 83 EPUB files before and after every tool and script. The SHA256 hash of every file was the same.
Use a reader first when you are not sure what a file is. List the files with unzip. Read the package file as text. Look at the title and the identifier. Then decide, and keep the original untouched.
Why you should read the first bytes
A file that ends in .epub can be anything. A good EPUB file starts with the letters PK and has the word mimetype at byte 30. The Linux file command checks that, and our script checks it too. It named 23 of our 30 damaged EPUB files correctly.
A name is not a promise. Files called invoice.epub or scan.epub are only names. Check where one came from, hash it and list its contents before you open it in an app.
Why Will Your EPUB File Not Open? Ten causes, and what to do about every one
Most EPUB files that will not open fail for one of ten reasons. Check the ZIP and the mimetype file first. Apps forgive many faults, so EPUB files that open can still fail stores or checkers, and EPUB files that fail are not always lost. Our script found the failing step in 26 of 30 copies.
We damaged one EPUB file in 30 ways. All three readers opened 13 of the damaged copies and refused 3. All three lost the same chapter in 1, and they split on the other 13. Our script failed 26 copies, warned on 3 and passed 1. The table groups the 30 copies into ten causes.
Read the table slowly. The number in brackets matches the cause. Every row says what the readers did and what to try. The fixes work on a copy. A reader that says nothing is no sign that an EPUB file is good.
| Cause | Result and fix |
|---|---|
| The wrong app (1) | Phones and PCs with no EPUB reader show nothing useful on a double click. Fix: Use an app from the open section on a copy. |
| A bad mimetype file (2) | All three readers opened all 7 copies. EPUBCheck failed 6 and our script failed 7. Fix: Rewrite the ZIP with the repair script, which fixed all 7. |
| A cut ZIP (3) | Cutting a copy in half stopped all three readers. Losing the last 100 bytes stopped EbookLib and pandoc, and calibre read it. Fix: Get a new copy. No tool brings back cut text. |
| Junk around the ZIP (4) | 4,096 junk bytes before the start stopped pandoc and failed our script. The same bytes after the end harmed nothing. Fix: Rewrite the ZIP with the repair script, which trims the front. |
| A bad checksum (5) | A changed byte in the directory stopped no reader. Changed bytes in chapter 2 or the cover stopped EbookLib, and calibre dropped chapter 2. Fix: Get a new copy, because a damaged page is gone. |
| A bad container (6) |
A missing container file, or one that names the wrong path, stopped EbookLib and pandoc and not calibre. Missing package files stopped all three. Fix: Rebuild container, or get a new copy.
|
| A damaged package file (7) | A broken tag stopped all three readers. A missing identifier, title or modified date stopped none, and EPUBCheck failed all three. Fix: Close the tag and add the missing metadata. |
| A wrong spine or manifest (8) | A spine that points nowhere, an empty spine, a chapter left out of the manifest or a text/html media type cost some readers a chapter or the entire book. Fix: List every chapter id in the spine. |
| A missing file (9) | A missing chapter stopped EbookLib and pandoc, and calibre returned two chapters. A missing cover or navigation file stopped EbookLib only. Fix: Put the file back in the same path, or get a new copy. |
| A broken page (10) | A missing closing tag stopped no reader. A letter that is not UTF-8 changed chapter 1 in EbookLib and calibre and stopped pandoc, and EPUBCheck passed it. Fix: Close the tag and save the page as UTF-8. |
Repair the ZIP with a script
Bad mimetype files are the cause that a script fixes well. The script reads every file it can, skips any that fail and writes new ZIP files with the mimetype file first and stored. It never changes the original. The listing shows it in full, with the result for a copy whose mimetype file was second.
import io, sys, zipfile
def repair(src, dst):
raw = open(src, 'rb').read()
start = raw.find(b'PK\x03\x04')
z = zipfile.ZipFile(io.BytesIO(raw[start:]))
keep = []
for name in z.namelist():
if name == 'mimetype' or name.endswith('/'):
continue
try:
keep.append((name, z.read(name)))
except Exception as e:
print('skipped', name, type(e).__name__)
with zipfile.ZipFile(dst, 'w') as out:
info = zipfile.ZipInfo('mimetype')
out.writestr(info, b'application/epub+zip', compress_type=zipfile.ZIP_STORED)
for name, data in keep:
out.writestr(name, data, compress_type=zipfile.ZIP_DEFLATED)
print('wrote', len(keep) + 1, 'files to', dst)
if __name__ == '__main__':
repair(sys.argv[1], sys.argv[2])
python3 repair_epub.py c01.epub fixed.epub
wrote 11 files to fixed.epub
OK all 6 steps passed
Of the 26 damaged copies that failed our check, the script fixed 9, with all three chapters. They were the 7 mimetype copies, the copy with a changed directory byte and the copy with junk before the ZIP. The other 17 need a different fix.
Rebuild the book with calibre
calibre can read damaged EPUB files and write new ones. The two commands write a rebuilt file and test it. On the 26 failing copies, calibre wrote EPUB files that passed our check with all three chapters for 18. It wrote EPUB files with two chapters for 4 and no file for 4.
ebook-convert broken.epub rebuilt.epub
python3 check_epub.py rebuilt.epub
Those four short files are the danger. calibre gave no error. The new file passes the check, and one chapter is gone. Count the chapters after every rebuild. A rebuild also writes new ids and styles, so it changes the EPUB file and does not repair the original.
Repair steps in the order we would try them
We tested the steps on Python 3.13 and calibre 7.6.0 on Linux. We did not test them on Macs or phones, and we tested no EPUB file from a store.
- Copy the EPUB file, write its SHA256 hash and work on the copy.
- Test the copy with our script and read the first failure. It names the step and the cause.
- If step 1 reports junk before the ZIP, or step 2 fails, rewrite the ZIP with the repair script.
- If step 3, 4, 5 or 6 fails, open the named file in an editor that saves UTF-8 and fix the line it names.
- If the ZIP is cut or a checksum fails, get a new copy from the sender or the store.
- Test the result again until the script prints OK. Then open it in your app.
- Keep the original and the repaired copy side by side.
What to send the sender or a helper
Short messages get faster answers. Include the details below. Send the output of the check script too, because its first line names the failing step.
Subject: EPUB file will not open
File: the name, the size in bytes and the SHA256 hash
Check: the output of check_epub.py
System: Mac, Windows or phone, and the name of the app
What happens: the exact words of the error
Source: where the EPUB file came from and when it was copied
What no repair can fix
Do not try random repair sites. A site that offers to fix any EPUB file for free can keep the upload. The EPUB file can be one you paid for. Try the steps above first, and ask the sender or the store for a new copy.
How Do You Convert an EPUB File? PDF, DOCX, MOBI and plain text
Convert EPUB files with calibre or pandoc. calibre wrote PDF, DOCX, MOBI, AZW3, TXT and HTMLZ from our books, and pandoc wrote DOCX, ODT, HTML and Markdown. All 50 conversions finished, and every output of our test book kept all three chapters.
Two kinds of work belong here. One writes the book in another format. The other unpacks the book into files. Choose by what you need next. The table lists what we found for the test book, and every row names its source.
| Type | Finding and source |
|---|---|
| EPUB to PDF | calibre wrote a 4 page PDF of about 78 KB from our test book, with all three chapters and both images. On our headless Linux server it needed a display setting first. Source: Our tests. |
| EPUB to DOCX | calibre wrote about 9 KB and pandoc about 15 KB. Both kept all three chapters and both images. Source: Our tests. |
| EPUB to MOBI or AZW3 | calibre wrote both formats for all 5 books we tried. Wikipedia says Amazon's Send to Kindle took EPUB from 2022, so check your device first. Source: Our tests and Wikipedia. |
| EPUB to TXT | calibre wrote 875 bytes of text from our test book. The text is complete and the two images are dropped. Source: Our tests. |
| EPUB to HTML or Markdown | pandoc wrote HTML of 1,193 bytes and Markdown of 1,005 bytes, and it saved the two images in a media folder. Source: Our tests. |
| EPUB to a folder | FileInfo says to rename the file to .zip and extract it. unzip opened our .epub files with no rename at all. Source: FileInfo and our tests. |
Convert EPUB files with calibre
calibre reads the book and writes the format that the file ending asks for. We converted 5 books to 6 formats with it, which makes 30 conversions, and all 30 finished. The listing shows the commands we used.
ebook-convert book.epub book.pdf
ebook-convert book.epub book.docx
ebook-convert book.epub book.mobi
ebook-convert book.epub book.azw3
ebook-convert book.epub book.txt
pandoc book.epub -o book.docx
pandoc book.epub -o book.md --extract-media=media
unzip book.epub -d book_folder
The commands are the ones we used, with our file names changed to book. We tested them on Linux with calibre 7.6.0 and pandoc 3.1.3. In our reader test, pandoc needed 194 seconds for the longest public book, so wait before you stop a job.
Convert EPUB files with pandoc
pandoc reads an EPUB file and writes DOCX, ODT, HTML or Markdown. We converted the same 5 books to 4 formats, which makes 20 conversions, and all finished. For HTML and Markdown, ask for a media folder, or the images stay inside the book.
Text books come out well. Books made of images come out nearly empty, because they contain pictures and no text. Three of our public EPUB files are built that way, and pandoc wrote 3 to 39 characters for them. That is right, and not a failure.
Unpack EPUB files
Unpacking gives you the pages, styles and images as files. The command in the listing writes them into a folder. Editing them and zipping the folder again can break the book, as the mimetype section explains.
Protected books are different. We tested no protected EPUB file. The Library of Congress says protected EPUB files can need a proprietary reader, so a converter can fail on one. Do not remove protection from EPUB files you do not own.
How Do EPUB Files Differ From PDF Files? Also MOBI, DOCX and ZIP
An EPUB file is an ebook whose text reflows to fit the screen. PDF files keep their pages fixed, DOCX files are documents you edit and ZIP files are any archive. An EPUB file is a ZIP with rules, and readers need those rules.
The names are easy to mix up, and the files do different jobs. The table lists near neighbors of EPUB files. Every row ends with its source.
| Format | What it is |
|---|---|
| EPUB | Reflowable text in a ZIP of web pages. Text reflows to fit the screen. The Library of Congress lists reflowable text as a main function. Source: Library of Congress. |
| A fixed page document. calibre made a 4 page PDF from our 3 chapter book. Source: Our tests. | |
| MOBI | An older Amazon ebook format. calibre wrote it from all 5 books. Wikipedia says Send to Kindle took EPUB from 2022. Source: Our tests and Wikipedia. |
| DOCX | A Word document. calibre and pandoc both wrote it from all 5 books. Source: Our tests. |
| ZIP | An ordinary archive. An EPUB file is a ZIP with rules, and FileInfo says you can rename one to .zip. Source: FileInfo. |
Which one do you need
Other pages on this site cover ODT files for OpenDocument text. An EPUB file differs from all of them because its pages are XHTML and CSS, the same web pages that a browser reads.
Why a renamed file fails
A tip that circulates online says to rename files to change their type. Renaming .epub to .zip is fine, because EPUB files are ZIP files. Renaming .epub to .pdf or .mobi changes the ending and nothing inside. Convert the EPUB file instead.
We did not test a rename to PDF or MOBI. A reader that checks the first bytes would see PK and the word mimetype, and it would know the file is still an EPUB file.
What Is the EPUB File Format Made Of? The facts we could check, and where they came from
EPUB files are an open standard. The facts come from the Library of Congress, PRONOM, the IANA record, FileInfo, Apple's Books guide, Wikipedia and our checks of 53 good EPUB files and 30 damaged ones.
| Part | What we could check |
|---|---|
| Maker | The IDPF made EPUB 2 in 2007 and EPUB 3 in 2011. The W3C now keeps the format, per the IANA record. |
| Kind |
A ZIP archive. The mimetype file comes first, stored, with the text application/.
|
| Container |
META-INF/ names the package file. Of 45 public EPUB files, 31 used a folder called EPUB, 7 OPS and 7 OEBPS.
|
| Package file |
An .opf file with metadata, a manifest and a spine. Its version attribute says 2.0 or 3.0.
|
| Navigation | A navigation document for EPUB 3 and an NCX file for EPUB 2. All 45 public EPUB files had the first, and 27 kept the second. |
| Specification | The W3C publishes EPUB 3.3, and the IANA record cites it. The IDPF published the earlier versions. |
| Ending | .epub |
| Media type |
application/, registered with IANA. The Library of Congress and PRONOM list the same type.
|
| Detection |
The letters PK at byte 0, mimetype at byte 30 and the media type at byte 38. PRONOM lists it as fmt/483 and does not separate the versions.
|
| Related formats | ZIP, XHTML, CSS and the OPF package format, per the Library of Congress. |
| Our tests | 53 good EPUB files, 30 damaged EPUB files, three readers, three checkers, the file command and a 74 line script on Linux. |
About the values we list
Facts about structure come from the IANA record, the Library of Congress and our files. Dates for EPUB 3.2 and 3.3 come from Wikipedia. Where a source is thin, as with the vendor viewers, the table says whose claim it is.
Where sources differ, we say so. The Freeviewer page lists Save As in one place and says the free version previews in another. We list both views and mark which one we tested, which was neither.
What we left out
We did not start Apple Books, Kobo, Kindles or Windows viewers. We tested no protected EPUB file and no fixed layout EPUB file in any reader. We made 8 of our 53 good EPUB files ourselves, and all 30 damaged EPUB files come from one small book.
The damaged copies show what can happen and not how common it is. We tested no route from EPUB to the old Kindle formats on a device. We name no price, and the vendor pages name none we could use.
Sources checked
For the format, see the Library of Congress records for the EPUB 2 format description and the EPUB 3 format description. For identification, see the IANA registration and the PRONOM entry. For programs, see the FileInfo page for EPUB.
For importing books on a Mac, see Apple's Books import guide. For release dates, see the Wikipedia article on EPUB. Every count and label came from the files and scripts in our research folder.
Frequently Asked Questions About EPUB Files
What is an EPUB file?
An EPUB file is an ebook saved as a ZIP file of web pages. It contains XHTML chapters, a style sheet, images and a package file that sets the reading order. The IDPF made the format and the W3C now keeps it.
How do I open an EPUB file?
Apple Books opens EPUB files on iPhones, iPads and Macs. On Windows PCs, use calibre or Adobe Digital Editions, which are free. Short Python code with EbookLib also reads EPUB files, and a text editor shows nothing useful because the file is a ZIP.
Can I open an EPUB file on Windows?
Windows needs an app for EPUB files. calibre and Adobe Digital Editions are free, per FileInfo. SysTools and Freeviewer also list Windows viewers on their pages, and we tested neither one.
What is inside an EPUB file?
A ZIP with a mimetype file first, a container file that names the package file, the package file itself, a navigation file, XHTML chapters, a style sheet and images. Our test book had 11 files and 9,089 bytes. Folder names vary, and 31 of 45 public EPUB files used one called EPUB.
Can I unzip an EPUB file?
Yes. An EPUB file is a ZIP, and unzip opened all 53 of our good files. FileInfo says you can rename the file to .zip first. Edit nothing inside and zip it again, because the mimetype file must stay first and stored.
What is the mimetype file in an EPUB file?
It is the first file in the ZIP and has the 20 characters application/epub+zip. It must be stored and not compressed, with no extra field. Apps ignore a breach, and EPUBCheck failed 6 of our 7 broken copies.
What is the difference between EPUB 2 and EPUB 3?
EPUB 3 uses HTML5 pages and a navigation document, and it can contain audio, video and scripts. EPUB 2 uses an NCX table of contents. The package file says 2.0 or 3.0. Of our 53 good EPUB files, 50 were EPUB 3 and 3 were EPUB 2.
Why does my EPUB file say invalid or will not open?
The usual causes are bad mimetype files, cut ZIP files, wrong container or package files and spines that point nowhere. In our tests, the readers split on 13 of 30 damaged EPUB files. Use our check script to find the failing step.
How do I fix a corrupted EPUB file?
Copy the file, then check it to find the failing step. Our rewrite script fixed 9 of the 26 copies that failed. All 9 had bad mimetype files, a changed directory byte or junk before the ZIP. Cut ZIP files and bad checksums need new copies.
How do I convert an EPUB file to PDF?
Use calibre to convert it. It wrote a 4 page PDF of our test EPUB file with all three chapters and both images. pandoc writes DOCX, ODT, HTML and Markdown from the same file.
Can I read EPUB files on a Kindle?
Wikipedia says Amazon's Send to Kindle accepted EPUB files from 2022 and that Kindle readers support EPUB. We did not test a Kindle. If your device refuses a file, convert it with calibre to MOBI or AZW3.
Is an EPUB file safe to open?
Reading one with a tool is safe, and our readers changed none of the 83 files we tested. The book inside can still contain links and, in EPUB 3, scripts. Six of 45 public EPUB files declared scripted pages. Judge the source and work on a copy.
Still Stuck With an EPUB File? Seven routes out, depending on what went wrong
Check the ZIP and the mimetype file first. The ten causes list what to try in order.
Test the file with our script, and read the first failure. The repair steps show the order.
Is it an archive of any kind? The page on ZIP files shows how to open the container around every EPUB file.
Is it a fixed page document? The page on PDF files covers pages that do not reflow and the programs that read them.
Is it a Word document? The page on DOCX files explains Word's ZIP based document format.
Is it an OpenDocument text file? The page on ODT files covers the open format that LibreOffice writes.
Is it a file type you do not know? Browse the file extension resource library, or send it to us on the submit a file type page.