先上代码
package org.example;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.Resources;
import nl.siegmann.epublib.domain.Spine;
import nl.siegmann.epublib.domain.SpineReference;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.domain.TableOfContents;
import nl.siegmann.epublib.epub.EpubReader;
import org.assertj.core.util.Lists;
/**
* @ProjectName: demo
* @Package: com.example.demo
* @ClassName: MainTool
* @Author: ys
* @Description:
* @Date: 2021/11/24 4:02 下午
* @Version: 1.0
*/
public class App {
public static void main(String[] args) {
try {
// /Users/ys/Desktop/hh7.epub
String dPath = "/Users/ys/Desktop/epub";
// String dPath = args[0];
System.out.println("获取的目录为:" + dPath);
File epubfile = new File(dPath);
File[] epubfileFiles = epubfile.listFiles();
for(File file : epubfileFiles) {
// File file = new File(dPath);
if(!file.getName().contains("epub")){
continue;
}
File file2 = new File(file.getAbsolutePath());
InputStream inputStream = new FileInputStream(file);
InputStream inputStream1 = new FileInputStream(file2);
//从输入流当中读取epub格式文件
EpubReader reader = new EpubReader();
Book book = reader.readEpub(inputStream);
//获取所有资源进行填充,为什么多此一举,在于 在windows环境下,部分中文资源没有加载进来
EpubReader1 reader1 = new EpubReader1();
reader1.readEpubByCompress(book, inputStream1, "UTF-8");
if (book == null) {
return;
}
// 获取电子书目录
TableOfContents tableOfContents = book.getTableOfContents();
Spine spine1 = book.getSpine();
// 电子书章节封装资源
List<SpineReference> spineReferences = book.getSpine().getSpineReferences();
// 电子书 章节Id 集合
List<String> resourceIds = Lists.newArrayList();
// 获取所有资源href
Set<String> hrefs = (Set<String>) book.getResources().getAllHrefs();
// css 资源文件
List<String> hrefCss = Lists.newArrayList();
hrefs.stream().forEach(href -> {
if (href.contains(".css")) {
hrefCss.add(href);
}
});
spineReferences.stream().forEach(spineReference -> {
// 获取章节Id
resourceIds.add(spineReference.getResourceId());
});
//获取到书本的目录资源
// System.out.println("目录资源数量为:" + tableOfContents.size());
//获取到目录对应的资源数据
List<TOCReference> tocReferences = tableOfContents.getTocReferences();
// 写入电子书 包含子目录
EpubWriter1 epubWriter = new EpubWriter1();
//TOCReference tocReference : tocReferences
for (int i = 0; i < tocReferences.size(); i++) {
Resource resource = tocReferences.get(i).getResource();
if (null == resource) {
continue;
}
//最后一个resouceId
String lastResouceId = resource.getId();
// 电子书章节资源
Resources resources = new Resources();
// 电子书章节
Book bookChapter = new Book();
//章节导航
Spine spine = new Spine();
if (hrefCss.size() > 0) {
hrefCss.stream().forEach(href -> {
resources.add(book.getResources().getByHref(href));
});
}
resources.add(resource);
//设置电子书资源
// 设置电子书导航文件(目录文件,包含子目录)
nl.siegmann.epublib.domain.Resource tocResource = book.getResources().getById("ncx");
if (tocResource != null) {
// 发现有的电子书并不存在 toc.ncx 而是以一种toc.xhtml的文件存在
if (tocResource.getHref().contains("toc.ncx")) {
spine.setTocResource(tocResource);
} else {
resources.add(tocResource);
}
}
//添加章节资源 每章内容:1。css文件 2。xhtml文件 3。image文件
bookChapter.setResources(resources);
// 添加该章节骨架
spine.addSpineReference(new SpineReference(book.getResources().getById(resource.getId())));
bookChapter.setSpine(spine);
// 获取图片资源
try {
//这里子目录可能还会包含子目录,这里建议用递归实现字目录数据传输(目前暂定为3级目录)
if (tocReferences.get(i).getChildren().size() > 0) {
//获取子目录的内容
for (TOCReference cTocReference : tocReferences.get(i).getChildren()) {
Resource cResource = cTocReference.getResource();
resources.add(cResource);
// 子目录的图片文件
getImg(cResource, resources, book);
spine.addSpineReference(new SpineReference(cResource));
lastResouceId = cResource.getId();
// 3级目录
if (cTocReference.getChildren().size() > 0) {
for (TOCReference ccTocReference : cTocReference.getChildren()) {
Resource ccResource = ccTocReference.getResource();
resources.add(ccResource);
// 子目录的图片文件
getImg(ccResource, resources, book);
spine.addSpineReference(
new SpineReference(ccResource));
lastResouceId = ccResource.getId();
}
}
}
}
//以及目录图片文件
getImg(resource, resources, book);
//和sine 的线性结构进行id匹配,找到其中缺失项进行补充
/**
* 1.记录最后一个resourceId
* 2.比较sine的resourceid,找到对应记录
* 3.判定sine的下一个resourceid 和 tocreferenence 的第一个resouceid 是相同,如果不同,则加入 sine
*
*/
if (i + 1 < tocReferences.size()) {
Resource resource1 = tocReferences.get(i + 1).