epub文件解压

<dependency>
<groupId>com.positiondev.epublib</groupId>
<artifactId>epublib-core</artifactId>
<version>3.1</version>
</dependency>
<!--html解析 -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>

===================================
package com.example.demo.ebook;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.MediaType;
import nl.siegmann.epublib.domain.Metadata;
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;

public class EbookMain {
public static void main(String[] args) {

File file = new File("/Users/xiejianwu/Desktop/ebook/ebook.epub");
InputStream in = null;
try {
//从输入流当中读取epub格式文件
EpubReader reader = new EpubReader();
in = new FileInputStream(file);
Book book = reader.readEpub(in);

book.getTableOfContents()

book.getContents();

//获取到书本的头部信息
Metadata metadata = book.getMetadata();
System.out.println("FirstTitle为:"+metadata.getFirstTitle());
//获取到书本的全部资源
Resources resources = book.getResources();
System.out.println("所有资源数量为:"+resources.size());
//获取所有的资源数据
Collection<String> allHrefs = resources.getAllHrefs();
for (String href : allHrefs) {
Resource resource = resources.getByHref(href);
//data就是资源的内容数据,可能是css,html,图片等等
byte[] data = resource.getData();
// 获取到内容的类型 css,html,还是图片
MediaType mediaType = resource.getMediaType();

String title = resource.getTitle();






}
//获取到书本的内容资源
List<Resource> contents = book.getContents();
System.out.println("内容资源数量为:"+contents.size());
//获取到书本的spine资源 线性排序
Spine spine = book.getSpine();
System.out.println("spine资源数量为:"+spine.size());
//通过spine获取所有的数据
List<SpineReference> spineReferences = spine.getSpineReferences();
for (SpineReference spineReference : spineReferences) {
Resource resource = spineReference.getResource();
//data就是资源的内容数据,可能是css,html,图片等等
byte[] data = resource.getData();
// 获取到内容的类型 css,html,还是图片
MediaType mediaType = resource.getMediaType();

System.out.println("mediaType1 = "+mediaType+"--"+resource.getTitle()+"--"+resource.getId());


FileOutputStream output = new FileOutputStream(new File("/Users/xiejianwu/Desktop/ebook/test/"+resource.getId()));

BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);

InputStream input = resource.getInputStream();


try {
// 读数据
int len = 0;
byte[] bytes = new byte[1024];
while ((len=input.read(bytes))!=-1){
// 写数据
bufferedOutput.write(bytes,0,len);
}
}finally {
bufferedOutput.close();
input.close();
}


}
//获取到书本的目录资源
TableOfContents tableOfContents = book.getTableOfContents();
System.out.println("目录资源数量为:"+tableOfContents.size());
//获取到目录对应的资源数据
List<TOCReference> tocReferences = tableOfContents.getTocReferences();
for (TOCReference tocReference : tocReferences) {
Resource resource = tocReference.getResource();
//data就是资源的内容数据,可能是css,html,图片等等
byte[] data = resource.getData();
// 获取到内容的类型 css,html,还是图片
MediaType mediaType = resource.getMediaType();
if(tocReference.getChildren().size()>0){
//获取子目录的内容
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//一定要关闭资源
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}



public static void wordCopy(File file){
FileInputStream fis = null;
FileOutputStream fos = null;

// 准备好复制过来的文件的新名字
String[] names = file.getPath().split("\\.");
String name = names[0]+"Copy."+names[1]; // 重命名
try {
// 文件字节输出、输出流对象
fis = new FileInputStream(file);
fos = new FileOutputStream(name);
// 读数据
int len = 0;
byte[] bytes = new byte[1024];
while ((len=fis.read(bytes))!=-1){
// 写数据
fos.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
// 释放资源
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}

}



}



}
posted @ 2023-06-28 23:29  ENU  阅读(105)  评论(0编辑  收藏  举报