An Overview of EPUB
PS: I added the warning above manually. Since this article was migrated from an old blog, its update date doesn't trigger the blog's built-in warning condition.
What is EPUB
EPUB is a free and open e-book format. It was initially developed under the International Digital Publishing Forum (IDPF), and later the maintenance of the relevant standards was transferred to the W3C.
EPUB can be used to present fixed-layout documents and also supports automatic content reflow. Content reflow enables the e-book to automatically adapt its display to the reading device's screen size, font size, and layout settings. This is one reason why EPUB is more suitable for e-book reading than many fixed-layout document formats.
The development of EPUB can be traced back to the OEBPS (Open eBook Publication Structure), proposed by the Open eBook Forum (OeBF) in 1999. The OeBF later changed its name to IDPF, and OEBPS gradually evolved into the foundation of the EPUB standard.
In September 1999, the OeBF released OEBPS 1.0. OEBPS then went through two updates: 1.0.1 and 1.2. In 2006, the IDPF released OCF 1.0 (Open Container Format) to define the container format for EPUB. In 2007, the IDPF released OPF 2.0 (Open Packaging Format) and OPS 2.0 (Open Publication Structure), which were key components of the EPUB 2.0 standard.
EPUB 2.0.1 arrived in 2010. The following year saw the release of EPUB 3.0, which further enhanced support for HTML5, CSS, audio, video, scripting, and international typography. EPUB 3.0.1 followed in 2014 as a maintenance version of EPUB 3.0. EPUB 3.1 was released in 2017. Later, with the merger of IDPF and W3C, the maintenance of the EPUB standard gradually moved under the W3C umbrella.
Currently, common EPUBs can mainly be divided into two categories: EPUB 2 and EPUB 3. When you create or unpack EPUB files for analysis, you'll often encounter the EPUB 2 structure; newer specifications are primarily based on EPUB 3. This article mainly uses an EPUB 2 e-book file as an example to explain the basic file structure of EPUB.
EPUB File Structure
An EPUB file is essentially a ZIP archive. If you open an EPUB file with WinHex or another hex editor, you can see the common file header for ZIP files, for example:
50 4B 03 04
Therefore, for many EPUB files, you can simply change the extension from .epub to .zip and then use compression software to extract and view the internal structure.
EPUB is typically based on resources such as XML, CSS, XHTML, images, and fonts. Among them:
- XML describes the e-book's metadata, table of contents, file manifest, and other information;
- XHTML holds the body content;
- CSS controls layout and styling;
- Resources like images and fonts provide covers, illustrations, and custom fonts.
Take [乙野四方字].我会呼唤你的名字.epub as an example. Rename the file to [乙野四方字].我会呼唤你的名字.zip, and after extraction, you can see a file structure similar to the one below. Some files have been omitted for easier display:
│ mimetype
│
├─META-INF
│ container.xml
│
└─OEBPS
│ content.opf
│ toc.ncx
│
├─Fonts
│ ziti1-1.ttf
│ ...
│
├─Images
│ cover.jpg
│ note.png
│
├─Styles
│ style.css
│
└─Text
contents.xhtml
...
Among these, mimetype and META-INF/container.xml are two very important files in an EPUB. Generally, they are always present.
It's important to note that the folder name OEBPS is not a mandatory requirement; it's just a common convention. What really determines the location of the e-book's main configuration file is the path declaration in META-INF/container.xml.
The mimetype File
mimetype is a plain text file with the fixed content:
application/epub+zip
It is used to identify this ZIP container as an EPUB file.
In a standard EPUB file, mimetype usually needs to be the first file in the archive and must be stored uncompressed. This allows readers or detection tools to identify the file type more quickly.
The META-INF Folder
The META-INF folder is used to store information related to the EPUB container. By default, this directory contains at least one container.xml file.
container.xml typically looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
Here, the full-path attribute of the rootfile element specifies the location of the OPF file. The reader will use this path to find the EPUB's main configuration file.
Besides container.xml, the following files may also appear in META-INF:
manifest.xml: file list;metadata.xml: metadata;signatures.xml: digital signatures;encryption.xml: encryption information;rights.xml: rights management information.
These files are not required for every EPUB, so you don't need to pay too much attention to them when creating or unpacking a standard EPUB.
The OEBPS Folder
In many EPUB files, the OEBPS folder is used to store the actual book content, including:
content.opf: the e-book's main configuration file;toc.ncx: the table of contents file;- XHTML body files;
- CSS style files;
- Font files;
- Image files.
Common resource folder names are:
Fonts: font files;Images: image files;Styles: CSS style files;Text: XHTML body files.
These folder names are not mandatory; they are just a common convention. As long as the paths are correctly declared in the OPF file, the reader can find the corresponding resources.
The OPF File
The OPF file is one of the most important files in an EPUB. It is essentially an XML file responsible for describing the e-book's metadata, file manifest, reading order, and other information.
The root element of the OPF file is <package>, with common attributes as follows:
xmlns: specifies the XML namespace, which is usuallyhttp://www.idpf.org/2007/opfin EPUB 2;version: specifies the EPUB specification version, e.g.,2.0or3.0;unique-identifier: specifies the e-book's unique identifier, corresponding to theidof a<dc:identifier>in the<metadata>.
For example:
<package version="2.0" unique-identifier="BookId" xmlns="http://www.idpf.org/2007/opf">
Here, BookId is actually an identifier ID that needs to be defined later in the <metadata>.
An OPF file typically contains several sections:
<metadata>: metadata;<manifest>: file manifest;<spine>: reading order;<guide>: special page index, common in EPUB 2;<tour>: reading guide information, rarely used.
metadata: Metadata
<metadata> is used to describe the e-book's basic information, such as title, author, language, subject, publication date, etc.
It typically uses Dublin Core metadata elements, which are the common dc:title, dc:creator, dc:language, and other tags.
Common elements are as follows:
<dc:title>Describes the book title.
<dc:title>Introduction to XML</dc:title><dc:creator>Describes the author.
<dc:creator>John Doe</dc:creator><dc:subject>Describes the subject or keywords.
<dc:subject>XML</dc:subject><dc:description>Describes a summary, abstract, or explanation.
<dc:description>This document provides an introduction to XML.</dc:description><dc:publisher>Describes the publisher or distributor.
<dc:publisher>XYZ Publications</dc:publisher><dc:contributor>Describes contributors, such as editors, translators, proofreaders, etc.
<dc:contributor>Editor: Jane Smith</dc:contributor><dc:date>Describes a date, typically the creation date, publication date, or modification date.
<dc:date>2022-01-21</dc:date><dc:type>Describes the resource type.
<dc:type>Text</dc:type><dc:format>Describes the resource format.
<dc:format>application/epub+zip</dc:format><dc:identifier>Describes a unique identifier for the resource, which can be a UUID, URL, ISBN, etc.
<dc:identifier id="BookId">urn:uuid:5208e6bb-5d25-45b0-a7fd-b97d79a85fd4</dc:identifier><dc:source>Describes the source of the resource.
<dc:source>https://example.com/collection</dc:source><dc:language>Describes the language.
<dc:language>zh</dc:language><dc:relation>Describes a relationship with other resources.
<dc:relation>Is Part Of: XYZ Series</dc:relation><dc:rights>Describes copyright and usage rights.
<dc:rights>Copyright © 2022, All Rights Reserved</dc:rights>
In addition to Dublin Core elements, <meta> tags can also appear in metadata to supplement extended information. For example:
<meta name="cover" content="cover.jpg"/>
Note that cover.jpg here is usually not a file path, but a file ID declared in the <manifest>.
For example:
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>我会呼唤你的名字</dc:title>
<dc:creator opf:file-as="末影服" opf:role="aut">乙野四方字</dc:creator>
<dc:language>zh</dc:language>
<dc:subject>轻小说</dc:subject>
<dc:description>EPUB制作:末影服</dc:description>
<dc:date opf:event="modification">2022-10-07</dc:date>
<meta content="1.7.0" name="Sigil version"/>
<dc:identifier id="BookId" opf:scheme="UUID">urn:uuid:5208e6bb-5d25-45b0-a7fd-b97d79a85fd4</dc:identifier>
<meta name="cover" content="cover.jpg"/>
</metadata>
manifest: File Manifest
<manifest> lists all resource files contained in the e-book, such as XHTML documents, images, stylesheets, font files, etc.
Each file is declared via an <item> element. For example:
<item id="cover.jpg" href="Images/cover.jpg" media-type="image/jpeg"/>
Where:
id: the file ID, used internally by the EPUB to identify this resource;href: the relative path to the file;media-type: the file's media type.
Example:
<manifest>
<item id="Section001.xhtml" href="Text/Section001.xhtml" media-type="application/xhtml+xml"/>
<item id="Section002.xhtml" href="Text/Section002.xhtml" media-type="application/xhtml+xml"/>
<item id="Section003.xhtml" href="Text/Section003.xhtml" media-type="application/xhtml+xml"/>
<item id="Summary.xhtml" href="Text/Summary.xhtml" media-type="application/xhtml+xml"/>
<item id="contents.xhtml" href="Text/contents.xhtml" media-type="application/xhtml+xml"/>
<item id="cover.jpg" href="Images/cover.jpg" media-type="image/jpeg"/>
<item id="cover.xhtml" href="Text/cover.xhtml" media-type="application/xhtml+xml"/>
<item id="message.xhtml" href="Text/message.xhtml" media-type="application/xhtml+xml"/>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="note.png" href="Images/note.png" media-type="image/png"/>
<item id="style.css" href="Styles/style.css" media-type="text/css"/>
<item id="title.xhtml" href="Text/title.xhtml" media-type="application/xhtml+xml"/>
<item id="ziti2.ttf" href="Fonts/ziti2.ttf" media-type="font/ttf"/>
<item id="ziti3.ttf" href="Fonts/ziti3.ttf" media-type="font/ttf"/>
</manifest>
spine: Reading Order
<spine> is used to define the reading order of the e-book's body content. Reading systems display content in the order specified by <spine>.
<spine> consists of multiple <itemref> child elements. For example:
<itemref idref="cover.xhtml"/>
Here, idref corresponds to the id of an <item> in the <manifest>.
Example:
<spine toc="ncx">
<itemref idref="cover.xhtml" properties="duokan-page-fullscreen"/>
<itemref idref="title.xhtml"/>
<itemref idref="message.xhtml"/>
<itemref idref="Summary.xhtml"/>
<itemref idref="contents.xhtml"/>
<itemref idref="Section001.xhtml"/>
<itemref idref="Section002.xhtml"/>
</spine>
In EPUB 2, toc="ncx" is used to specify the ID of the NCX table of contents file. This ID needs to be declared in advance in the <manifest>:
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
guide: Special Page Index
<guide> is used to list special pages in the e-book, such as the cover, table of contents, start of the body text, etc. This part is common in EPUB 2 but is not mandatory.
For example:
<guide>
<reference type="toc" title="Table Of Contents" href="Text/contents.xhtml"/>
<reference type="cover" title="Cover" href="Text/cover.xhtml"/>
</guide>
Where:
type: the page type;title: the page title;href: the corresponding file path.
tour: Reading Guide
<tour> is used to organize some pages in the e-book in a specific order to form a guided reading path. This element is rarely seen and is generally not used in standard EPUB creation.
The NCX File
The NCX file is a common table of contents file in EPUB 2, usually named toc.ncx. It is also essentially an XML file, used to describe the e-book's table of contents structure.
It's worth noting that EPUB 3 primarily uses an XHTML-format navigation document, commonly known as nav.xhtml. However, for compatibility with older readers, some EPUB 3 files still retain toc.ncx.
Below, we'll walk through the toc.ncx obtained after extracting [乙野四方字].我会呼唤你的名字.epub as an example.
head: Table of Contents Header Information
The <head> in toc.ncx usually looks like this:
<head>
<meta name="dtb:uid" content="urn:uuid:5208e6bb-5d25-45b0-a7fd-b97d79a85fd4"/>
<meta name="dtb:depth" content="2"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
</head>
Where:
dtb:uid: the e-book's unique identifier, which should ideally match the<dc:identifier>in the OPF file;dtb:depth: the depth of the table of contents hierarchy;dtb:totalPageCount: the total page count, which is usually0in standard EPUBs;dtb:maxPageNumber: the maximum page number, which is usually0in standard EPUBs.
Strictly speaking, dtb:uid should match the identifier in the OPF file. Even if they don't match, some readers might still open the file, but it's not recommended.
docTitle: Table of Contents Title
<docTitle> is used to describe the book title corresponding to the table of contents file, usually written as:
<docTitle>
<text>作品名</text>
</docTitle>
The book title here should ideally match the <dc:title> in the OPF file. Even if they don't match, readers usually prioritize the title in the OPF.
navMap: Table of Contents Structure
The most important node in the NCX file is <navMap>.
<navMap> is composed of multiple <navPoint> nodes. Each <navPoint> represents a table of contents entry, which can correspond to the cover, production information, chapters, afterword, etc.
Common child nodes within <navPoint> include:
<navLabel>: the display name of the table of contents entry;<content>: the file path the table of contents entry points to.
Here, the playOrder attribute defines the order of the current entry in the table of contents, and the src attribute of the content node defines the location of the chapter file.
<navPoint> elements can also be nested to form a multi-level table of contents structure.
Example:
<navMap>
<navPoint id="navPoint-1" playOrder="1">
<navLabel>
<text>封面</text>
</navLabel>
<content src="Text/cover.xhtml"/>
</navPoint>
<navPoint id="navPoint-2" playOrder="2">
<navLabel>
<text>制作信息</text>
</navLabel>
<content src="Text/message.xhtml"/>
</navPoint>
<!-- Middle part omitted -->
<navPoint id="navPoint-29" playOrder="29">
<navLabel>
<text>终章,又或是在世界的某处</text>
</navLabel>
<content src="Text/Section009.xhtml"/>
</navPoint>
</navMap>
Summary
Simply put, an EPUB can be understood as a ZIP archive organized according to specific rules. Its core structure is roughly as follows:
EPUB File
├─ mimetype
├─ META-INF
│ └─ container.xml
└─ Book Content Directory
├─ content.opf
├─ toc.ncx / nav.xhtml
├─ XHTML Body
├─ CSS Styles
├─ Images
└─ Fonts
Where:
mimetypeis used to declare the file type;container.xmlis used to point to the OPF file;content.opfis used to describe metadata, file manifest, and reading order;toc.ncxornav.xhtmlis used to describe the table of contents;- XHTML, CSS, image, font, and other files together form the e-book's body and resources.
Once you understand the relationships between these files, unpacking, modifying, and creating EPUBs will become much clearer.