An .epub file is a ZIP archive laid out
by the Open Container Format (OCF), part of the EPUB specification. The same rules have
applied, with small changes, since EPUB 2 (OCF 2.0.1, 2010); this page follows EPUB 3.3
(W3C, 2023) and notes where EPUB 2 differs.
ZIP rules
- Each file is stored (method 0) or compressed with Deflate (method 8); no other method is allowed.
- ZIP encryption is not allowed; EPUB has its own, in
encryption.xml. - The archive may not be split or spanned over several files.
- ZIP64 may be used, but only when the archive needs it.
- File names are UTF-8.
- The “version needed to extract” field is 10, 20 or 45 (1.0, 2.0 or 4.5).
The mimetype file
The first entry in the archive must be a file named mimetype, stored
without compression, unencrypted and with no extra field, whose content is exactly the
20 ASCII bytes application/epub+zip, with no line end, padding or byte
order mark. Because its position and length are fixed, the bytes from offset 30 of
every valid EPUB read mimetypeapplication/epub+zip, and a program can
recognise the format from them without unzipping.
00000000 50 4B 03 04 0A 00 00 00 00 00 xx xx xx xx xx xx PK..............
00000010 xx xx 14 00 00 00 14 00 00 00 08 00 00 00 6D 69 ..............mi
00000020 6D 65 74 79 70 65 61 70 70 6C 69 63 61 74 69 6F metypeapplicatio
00000030 6E 2F 65 70 75 62 2B 7A 69 70 50 4B 03 04 … n/epub+zipPK..
| Offset | Bytes | ZIP field | Value in an EPUB |
|---|---|---|---|
0 | 50 4B 03 04 | local file header signature | PK\3\4 |
4 | 0A 00 | version needed | 10 (often 20) |
6 | 00 00 | flags | 0: no encryption and no data descriptor |
8 | 00 00 | method | 0, stored |
10 | 4 bytes | time and date | any |
14 | 4 bytes | CRC-32 | the CRC of the 20 bytes |
18 | 14 00 00 00 | compressed size | 20 |
22 | 14 00 00 00 | uncompressed size | 20 |
26 | 08 00 | name length | 8 |
28 | 00 00 | extra field length | 0, required |
30 | mimetype | file name | |
38 | application/epub+zip | file data | |
58 | 50 4B 03 04 | next entry | usually META-INF/container.xml |
File names and paths
- Paths and names are case-sensitive, and the folder separator is
/. - A file name is at most 255 bytes of UTF-8; a full path at most 65,535 bytes.
- A name must not contain
/"*:<>?\|or DEL (U+007F), C0 or C1 control characters, private-use characters or Unicode non-characters, and must not end with a full stop. - Two names in the same folder must not be the same after Unicode normalisation (NFC) and case folding.
- Names should avoid spaces, which some older reading systems mishandle.
The META-INF folder
The root of the container must hold a folder META-INF. Its files are
reserved; they are never listed in the package manifest and the book’s content must not
link to them.
| File | Status | Content |
|---|---|---|
container.xml | Required | Where the package document is: see below. |
encryption.xml | Optional; required when any file is encrypted or a font is obfuscated | Which files are encrypted and how: see encryption. |
signatures.xml | Optional | XML Digital Signatures over files in the container. |
rights.xml | Optional | Digital rights information, in a format the specification does not define (DRM systems such as Adobe’s put their licence here). |
metadata.xml | Optional | Metadata about the container as a whole, in a format the specification does not define. |
manifest.xml | Optional | A manifest of the container, in any format; kept for OpenDocument compatibility. |
A reading system may also find files such as com.apple.ibooks.display-options.xml
here, added by Apple and other vendors for their own settings; the specification does
not define them.
container.xml
The file uses the namespace
urn:oasis:names:tc:opendocument:xmlns:container.
| Element | Attributes | Content |
|---|---|---|
container | version: required, 1.0 | One rootfiles, then at most one links. |
rootfiles | One or more rootfile. | |
rootfile | full-path: required, the path of a package document from the container root, with no leading /. media-type: required, application/oebps-package+xml. | Empty. |
links | One or more link. | |
link | href: required. rel: required, the relationship. media-type: required when the target is inside the container. | Empty. Links resources about the whole container. |
The first rootfile is the default rendition. Several rootfiles make a
multiple-rendition publication (for example a fixed-layout and a reflowable version of
one book), described by the separate EPUB Multiple-Rendition Publications note; most
reading systems use only the first.
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
encryption.xml
The root element is encryption in the same container namespace. It
holds, in any order, EncryptedKey and EncryptedData elements
from W3C XML Encryption (namespace http://www.w3.org/2001/04/xmlenc#),
and may hold signatures.
| Element | Meaning |
|---|---|
enc:EncryptedData | One per encrypted file. |
enc:EncryptionMethod Algorithm="…" | The algorithm, as a URI: for example http://www.w3.org/2001/04/xmlenc#aes128-cbc, or the font obfuscation URIs below. |
ds:KeyInfo | Which key, often by name or by reference to an EncryptedKey. |
enc:CipherData / enc:CipherReference URI="…" | The encrypted file, by its path in the container. |
enc:EncryptionProperties / enc:EncryptionProperty / Compression | Optional, namespace http://www.idpf.org/2016/encryption#compression: Method 0 or 8 and OriginalLength, when the file was deflated before it was encrypted. |
These files must never be encrypted: mimetype, the
META-INF files listed above, and the package documents. Text-like files
are deflated before they are encrypted; already compressed media (JPEG, MP3 and so on)
should not be.
Font obfuscation
EPUB lets a publisher scramble embedded fonts slightly, so that a font cannot be pulled out of a book and installed as it is. It is not encryption: anyone with the book can undo it.
- Take the publication’s unique identifier: the
dc:identifiernamed by the package’sunique-identifierattribute. - Remove every space (U+0020), tab (U+0009), carriage return (U+000D) and line feed (U+000A).
- Encode it as UTF-8 and take its SHA-1 hash: a 20-byte key.
- XOR the first 1,040 bytes of the font file with the key repeated 52 times; leave the rest unchanged. A font shorter than 1,040 bytes is XORed whole.
- Then deflate the result, if compressing, and store it in the ZIP.
Each obfuscated font has an EncryptedData entry with
Algorithm="http://www.idpf.org/2008/embedding". The key is never written
in the file. Reading systems undo it in the reverse order.
Adobe defined an earlier scheme, still found in many EPUB 2 books, with the algorithm
URI http://ns.adobe.com/pdf/enc#RC: the key is the 16 bytes of the
identifier read as a UUID (its hex digits, without urn:uuid: and hyphens),
and only the first 1,024 bytes are XORed. It is not part of the EPUB
specification.
Differences in EPUB 2
The mimetype file, the META-INF files and
container.xml work the same way in EPUB 2 (OCF 2.0.1); the
links element of container.xml and the compression property
of encryption.xml were added later. An EPUB 2 and an EPUB 3 file are told
apart only by the version attribute of the package document, not by the
container.