深入瞭解 EPUB 檔案 | Linux 中國

語言: CN / TW / HK

EPUB 檔案是使用開放格式釋出內容的好方法。

(本文字數:4515,閱讀時長大約:5 分鐘)

EPUB 檔案是使用開放格式釋出內容的好方法。

電子書提供了一種隨時隨地閱讀書籍、雜誌和其他內容的好方法。讀者可以在長途飛行和乘坐火車時享受電子書打發時間。最流行的電子書檔案格式是 EPUB 檔案,它是“ 電子出版物(electronic publication) ”的縮寫。 EPUB 檔案受到各種電子閱讀器的支援,並且是當今電子書出版的有效標準。

EPUB 檔案格式基於 XHTML 內容和 XML 元資料的開放標準,包含在 zip 存檔中。由於一切都基於開放標準,我們可以使用通用工具來建立或檢查 EPUB 檔案。讓我們探索一個 EPUB 檔案以瞭解更多資訊。《C 程式設計技巧和竅門指南》,於今年早些時候在Opensource.com 上釋出,提供 PDF 或 EPUB 格式。

因為 EPUB 檔案是放在 zip 檔案中的 XHTML 內容和 XML 元資料,所以你可以用 unzip 命令在命令列檢查 EPUB:

$ unzip -l osdc_Jim-Hall_C-Programming-Tips.epub 
Archive: osdc_Jim-Hall_C-Programming-Tips.epub
Length Date Time Name
--------- ---------- ----- ----
20 06-23-2022 00:20 mimetype
8259 06-23-2022 00:20 OEBPS/styles/stylesheet.css
1659 06-23-2022 00:20 OEBPS/toc.xhtml
4460 06-23-2022 00:20 OEBPS/content.opf
44157 06-23-2022 00:20 OEBPS/sections/section0018.xhtml
1242 06-23-2022 00:20 OEBPS/sections/section0002.xhtml
22429 06-23-2022 00:20 OEBPS/sections/section0008.xhtml
[...]
9628 06-23-2022 00:20 OEBPS/sections/section0016.xhtml
748 06-23-2022 00:20 OEBPS/sections/section0001.xhtml
3370 06-23-2022 00:20 OEBPS/toc.ncx
8308 06-23-2022 00:21 OEBPS/images/image0011.png
6598 06-23-2022 00:21 OEBPS/images/image0009.png
[...]
14492 06-23-2022 00:21 OEBPS/images/image0005.png
239 06-23-2022 00:20 META-INF/container.xml
--------- -------
959201 41 files

這個 EPUB 包含很多檔案,但其中大部分是內容。要了解 EPUB 檔案是如何組合在一起的,請遵循電子書閱讀器的流程:

1、電子書閱讀器需要驗證 EPUB 檔案是否真的是 EPUB 檔案。他們通過檢查 EPUB 存檔根目錄中的 mimetype 檔案來驗證檔案。該檔案僅包含一行描述 EPUB 檔案的 MIME 型別:

application/epub+zip

2、為了定位內容,電子書閱讀器從 META-INF/container.xml 檔案開始。這是一個簡短的 XML 文件,指示在哪裡可以找到內容。對於此 EPUB 檔案, container.xml 檔案如下所示:

<?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>

為了使 container.xml 檔案更易於閱讀,我將單行拆分為多行,並添加了一些間距來縮排每行。XML 檔案並不關心新行和空格等額外的空白,因此這種額外的間距不會影響 XML 檔案。

3、 container.xml 檔案表示 EPUB 的根從 OEBPS 目錄中的 content.opf 檔案開始。OPF 副檔名是因為 EPUB 基於 “ 開放打包格式(Open Packaging Format) ”,但 content.opf 檔案實際上只是另一個 XML 檔案。

4、 content.opf 檔案包含一個完整的 EPUB 內容清單,以及一個有序的目錄,以及查詢每一章或每一節的引用。這個 EPUB 的 content.opf 檔案很長,因此我將在此僅展示一小部分作為示例。

XML 資料包含在 <package> 塊中,該塊本身具有 <metadata> 塊、 <manifest> 資料和包含電子書目錄的 <spine> 塊:

<?xml version="1.0" encoding="UTF-8"?>
<package unique-identifier="unique-identifier" version="3.0" xmlns="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:opf="http://www.idpf.org/2007/opf">
  <metadata>
    <dc:identifier id="unique-identifier">osdc002</dc:identifier>
    <dc:title>Tips and Tricks for C Programming</dc:title>
    <dc:creator>Jim Hall</dc:creator>
    <dc:language>English</dc:language>
    <meta property="dcterms:modified">2022-06-23T12:09:13Z</meta>
    <meta content="LibreOffice/7.3.0.3$Linux_X86_64 LibreOffice_project/0f246aa12d0eee4a0f7adcefbf7c878fc2238db3 (libepubgen/0.1.1)" name="generator"/>
  </metadata>
  <manifest>
    ...
    <item href="sections/section0001.xhtml" id="section0001" media-type="application/xhtml+xml"/>
    <item href="images/image0003.png" id="image0003" media-type="image/png"/>
    <item href="styles/stylesheet.css" id="stylesheet.css" media-type="text/css"/>
    <item href="toc.ncx" id="toc.ncx" media-type="application/x-dtbncx+xml"/>
    ...
  </manifest>
  <spine toc="toc.ncx">
    <itemref idref="section0001"/>
    <itemref idref="section0002"/>
    <itemref idref="section0003"/>
    ...
  </spine>
</package>

你可以把資料匹配起來,看看在哪裡可以找到每個部分。EPUB 閱讀器就是這樣做的。例如,目錄中的第一項引用了 section0001 ,它在清單中被定義為位於 sections/section0001.xhtml 檔案中。該檔案的名稱不需要與 idref 條目相同,但 LibreOffice Writer 的自動程式就是這樣建立該檔案的。(你可以在元資料中看到,這個 EPUB 是在 Linux 上用 LibreOffice 7.3.0.3 版本建立的,它可以將內容匯出為 EPUB 檔案。)

EPUB 格式

EPUB 檔案是一種使用開放格式釋出內容的好方法。EPUB 檔案格式是 XML 元資料與 XHTML 內容,包含在一個 zip 檔案內。雖然大多數技術作家使用工具來建立 EPUB 檔案,因為 EPUB 是基於開放標準,意味著你可以使用其他方式建立自己的 EPUB 檔案。

via: https:// opensource.com/article/ 22/8/epub-file

作者:Jim Hall 選題: lkxed 譯者: geekpi 校對: wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出