epub格式解析

温馨提示:本文最后更新于2024-07-07 03:30:00,某些文章具有时效性,若有错误或已失效,请在下方留言

文件组成

将 EPUB 格式的电子书的扩展名修改为 zip 后,可以通过解压软件进行解压。解压后,有几种不同的形式,如下所示

图片[1]-epub格式解析-Stewed Noodles 资源

一个未经加密处理的epub电子书由以下部分或者文件组成:

  • mimetype 文件
  • META-INF 文件夹
  • OEBPS 文件夹

mimetype 文件

每一个epub电子书均包含一个名为mimtype的文件,且内容不变,用以说明epub的文件格式。文件内容为:

application/epub+zip

META-INF 文件夹

META-INF 用于存放容器信息,默认的情况下该目录包含一个 container.xml 文件,文件的内容为:

<?xml version='1.0' encoding='utf-8'?>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
  <rootfiles>
    <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
  </rootfiles>
</container>

主要功能是说明特定文件的存放信息,默认是说明核心文件 OPF 的存放地址以及打开方式。 根据epub电子书的制作不同,还可能包含数字签名和加密信息等文件:

  • manifest.xml 文件列表
  • metadata.xml 元数据
  • signatures.xml 数字签名
  • encryption.xml 加密
  • rights.xml 权限管理

这些目录是可选的

OEBPS 文件夹

OPF 文件

OPF文档是epub的核心文件,且是一个标准的xml文件,依据OPF规范,此文件的根元素为<package>

<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="ePub-UUID">

其内容主要由以下几部分组成:

1. metadata

元数据信息,此信息是书籍的出版信息,由两个子元素组成。

(1)<dc-metadata>,其元素构成的核心元素,包括:

  • <dc-title>:标题
  • <dc-creator>:责任者
  • <dc-subject>:主题词或关键词
  • <dc-descributor>:内容描述
  • <dc-date>:日期
  • <dc-type>:类型
  • <dc-publisher>:出版者
  • <dc-contributor>:发行者
  • <dc-format>:格式
  • <dc-identifier>:标识信息
  • <dc-source>:来源信息
  • <dc-language>:语言
  • <dc-relation>:相关资料
  • <dc-coverage>:覆盖范围
  • <dc-rights>:权限描述

(2)<x-metadata>

扩展元素。如果有些信息在上述元素中无法描述,则在此元素中进行扩展。

例如

<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
  <dc:title>Pro SwiftUI</dc:title>
  <dc:identifier id="ePub-UUID">urn:uuid:70B63C38-2C23-462C-A2C1-D642DCF703B6</dc:identifier>
  <dc:creator id="creator">Paul Hudson</dc:creator>
  <meta refines="#creator" property="role" scheme="marc:relators">aut</meta>
  <dc:language>en</dc:language>
  <dc:date>2024-07-05</dc:date>
  <meta property="dcterms:modified">2024-07-05T03:24:49Z</meta>
  <dc:contributor id="contributor">UlyssesMac 35.1</dc:contributor>
  <meta refines="#contributor" property="role" scheme="marc:relators">bkp</meta>
  <meta name="cover" content="coverImage"/>
</metadata>
2. manifest

文件列表,列出书籍出版的所有文件,但是不包括:mimetype、container.xml、content.opf,由一个子元素构成

<item id="" href="" media-type="">

其中

  • id:文件的id号
  • href:文件的相对路径
  • media-type:文件的媒体类型

例如

<manifest>
    <!-- Navigation -->
    <item id="nav" href="navigation.xhtml" media-type="application/xhtml+xml"/>
    <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>

    <!-- HTML files -->
    <item id="chapter-1" href="chapter-1.xhtml" media-type="application/xhtml+xml"/>
    <item id="chapter-2" href="chapter-2.xhtml" media-type="application/xhtml+xml"/>
    <item id="chapter-3" href="chapter-3.xhtml" media-type="application/xhtml+xml"/>
    <item id="chapter-4" href="chapter-4.xhtml" media-type="application/xhtml+xml"/>
    <item id="coverImageContainer" href="cover.xhtml" media-type="application/xhtml+xml"/>

    <!-- CSS files -->
    <item id="stylesheet" href="css/style.css" media-type="text/css"/>

    <!-- Images -->
    <item id="coverImage" href="cover.png" media-type="image/png"/>

    <!-- Videos -->
    <item id="id" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
    <item id="id1" href="css/nav.css" media-type="text/css"/>
</manifest>
3. spine

其主要功能是提供书籍的线性阅读次序。由一个子元素构成:

<itemref idref=""/>

其中 idref:即参照manifest列出的id

例如

<spine toc="ncx">
  <itemref idref="coverImageContainer"/>
  <itemref idref="id"/>
  <itemref idref="chapter-1"/>
  <itemref idref="chapter-2"/>
  <itemref idref="chapter-3"/>
  <itemref idref="chapter-4"/>
</spine>
4. guide

列出电子书的特定页面,例如封面、目录、序言等,属性值指向文件保存地址。

<guide>
    <reference href="cover.xhtml" type="cover" title="Cover"/>
</guide>
5. tour

导读,可以根据不同的读者水平或阅读目的,按一定的次序,选择电子书中的部分页面组成导读。一般情况下,epub电子书可以不用该元素。

NCX 文件

NCX文件是epub电子书的又一个核心文件,用于制作电子书的目录,其文件的命名通常为 toc.ncx。ncx文件也是一个xml文件,这个文件与目录有直接的关系。

.ncx文件中最主要的节点是navMapnavMap节点是由许多navPoint节点组成的。而navPoint节点则是由navLabelcontent两个子节点组成。

  • navPoint节点中,playOrder属性定义当前项在目录中显示的次序。navLabel子节点中的text节点定义了每个目录的名字。
  • content子节点的src属性定义了对应每个章节的文件的具体位置。

navPoint 节点可以嵌套,就是书籍的目录是层级目录。

例如

<ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">
	<head>
		<meta name="dtb:uid" content="urn:uuid:70B63C38-2C23-462C-A2C1-D642DCF703B6"/>
		<meta name="dtb:depth" content="2"/>
		<meta name="dtb:totalPageCount" content="0"/>
		<meta name="dtb:maxPageNumber" content="0"/>
	</head>

	<docTitle>
		<text>Pro SwiftUI</text>
	</docTitle>
	
	<docAuthor>
		<text>Paul Hudson</text>
	</docAuthor>

	<navMap>
		<navPoint class="h1" id="animations-and-transitions-动画和过渡效果">
			<navLabel>
				<text>Animations and Transitions 动画和过渡效果</text>
			</navLabel>
			<content src="chapter-3.xhtml#animations-and-transitions-动画和过渡效果" />
			<navPoint class="h2" id="animating-the-unanimatable-动画的一致性">
				<navLabel>
					<text>Animating the unanimatable 动画的一致性</text>
				</navLabel>
				<content src="chapter-3.xhtml#animating-the-unanimatable-动画的一致性" />
			</navPoint>
			<navPoint class="h2" id="avoiding-pain-in-ios-156-and-below-避免-ios-156-及以下版本中的痛苦">
				<navLabel>
					<text>Avoiding pain in iOS 15.6 and below 避免 iOS 15.6 及以下版本中的痛苦</text>
				</navLabel>
				<content src="chapter-3.xhtml#avoiding-pain-in-ios-156-and-below-避免-ios-156-及以下版本中的痛苦" />
			</navPoint>
		</navPoint>
	</navMap>
</ncx>

参考

https://vernlium.github.io/2015/06/10/epub%E6%A0%BC%E5%BC%8F%E8%A7%A3%E6%9E%90/

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容