java解析epub电子书

阅读更多
Epublib主页:http://www.siegmann.nl/epublib

下载地址:https://github.com/psiegman/epublib/downloads

在线API:http://www.siegmann.nl/static/epublib/apidocs/

需要的jar包
epublib-core-latest.jar
kxml2-2.1.8.jar
slf4j-jdk14-1.0-rc5.jar
第一个jar包可以上https://github.com/psiegman/epublib/downloads下载
其它两个可以去http://www.jar114.com/下载

每一个epub格式的电子书其实就是一个zip压缩包,解压之后可以得到里面的内容
/**
* 解压含有多个文件的ZIP包
*
* @param zipFileName
* @param extPlace
*/
private static void extZipFileList(String zipFileName, String extPlace) {
try {

ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));

ZipEntry entry = null;

while ((entry = in.getNextEntry()) != null) {

String entryName = entry.getName();

if (entry.isDirectory()) {
File file = new File(extPlace + entryName);
file.mkdirs();
System.out.println("创建文件夹: " + entryName);
} else {

FileOutputStream os = new FileOutputStream(extPlace
+ entryName);

// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];

int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();

}
}

} catch (IOException e) {
e.printStackTrace();
}
System.out.println("解压文件成功 ");
}

public static void main(String[] args) throws IOException {
Zip zip = new Zip();
// zip.zip("c:\\111.xls ");
zip.extZipFileList("e:\\2009.epub ", "e:\\ ");
}
下面是在java中解析epub电子书 可以将epub电子书中的书名、封面、目录、内容(html格式)读取出来

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try {
EpubReader epubReader = new EpubReader();
EpubWriter epubWriter = new EpubWriter();
Book book = epubReader.readEpub(new FileInputStream("D:\\epub\\yttl.epub"));
//书名
String title = book.getTitle();
//作者
List authorList = book.getMetadata().getAuthors();
for(int i=0;i Author author = authorList.get(i);
}
//封面
Resource resource = book.getCoverImage();
byte[] p = resource.getData();
//将图片输出
   String newFilename="e:\\"+i+".jpg";
   FileImageOutputStream imgout=new FileImageOutputStream(new File(newFilename));
   imgout.write(p,0,p.length);
  imgout.close();

//电子书的内容
List list = book.getContents();
for(int i=0;i Resource  res = list.get(i);
byte[] resdata = res.getData();
String str = new String(resdata,"utf-8");
System.out.println(str);
}

Resource page=book.getCoverPage();
String str = new String(page.getData(),"UTF-8");
System.out.println(str);

//目录
Collection  hrefs = bookResources.getAllHrefs();
Iterator it=hrefs.iterator();
while(it.hasNext()){
   System.out.println(it.next());
   [
}

book.getMetadata().setTitles(new ArrayList()() {{
add("an awesome book");}});

//输出epub电子书
BookProcessor bookProcessor = epubWriter.getBookProcessor();
book=bookProcessor.processBook(book);
epubWriter.write(book, new FileOutputStream("D:\\epub\\new5yttl.epub"));
} catch (FileNotFoundException e) {
TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
TODO Auto-generated catch block
e.printStackTrace();
}
doPost( request, response);
}


你可能感兴趣的:(java解析epub,epub,解析epub,epublib解析epub,epublib)