1080ad2755adebb174ad7a3e04f4053c9e0b35f4
[fanfix.git] / src / be / nikiroo / fanfix / supported / Cbz.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.List;
10 import java.util.zip.ZipEntry;
11 import java.util.zip.ZipInputStream;
12
13 import be.nikiroo.fanfix.Instance;
14 import be.nikiroo.fanfix.data.Chapter;
15 import be.nikiroo.fanfix.data.Paragraph;
16 import be.nikiroo.fanfix.data.Story;
17
18 /**
19 * Support class for CBZ files (works better with CBZ created with this program,
20 * as they have some metadata available).
21 *
22 * @author niki
23 */
24 class Cbz extends Epub {
25 @Override
26 protected boolean supports(URL url) {
27 return url.toString().toLowerCase().endsWith(".cbz");
28 }
29
30 @Override
31 public String getSourceName() {
32 return "cbz";
33 }
34
35 @Override
36 protected String getDataPrefix() {
37 return "";
38 }
39
40 @Override
41 protected boolean requireInfo() {
42 return false;
43 }
44
45 @Override
46 protected boolean getCover() {
47 return false;
48 }
49
50 @Override
51 protected void preprocess(URL source, InputStream in) throws IOException {
52 super.preprocess(source, in);
53 meta.setImageDocument(true);
54 }
55
56 @Override
57 public Story process(URL url) throws IOException {
58 Story story = processMeta(url, false, true);
59 story.setChapters(new ArrayList<Chapter>());
60 Chapter chap = new Chapter(1, null);
61 story.getChapters().add(chap);
62
63 ZipInputStream zipIn = new ZipInputStream(getInput());
64
65 List<String> images = new ArrayList<String>();
66 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
67 .getNextEntry()) {
68 if (!entry.isDirectory()
69 && entry.getName().startsWith(getDataPrefix())) {
70 String entryLName = entry.getName().toLowerCase();
71 boolean imageEntry = false;
72 for (String ext : getImageExt(false)) {
73 if (entryLName.endsWith(ext)) {
74 imageEntry = true;
75 }
76 }
77
78 if (imageEntry) {
79 String uuid = meta.getUuid() + "_" + entry.getName();
80 images.add(uuid);
81 try {
82 Instance.getCache().addToCache(zipIn, uuid);
83 } catch (Exception e) {
84 Instance.syserr(e);
85 }
86 }
87 }
88 }
89
90 // ZIP order is not sure
91 Collections.sort(images);
92
93 for (String uuid : images) {
94 try {
95 chap.getParagraphs().add(
96 new Paragraph(new File(uuid).toURI().toURL()));
97 } catch (Exception e) {
98 Instance.syserr(e);
99 }
100 }
101
102 return story;
103 }
104 }