Version 1.1.0
[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 import be.nikiroo.utils.ui.Progress;
18
19 /**
20 * Support class for CBZ files (works better with CBZ created with this program,
21 * as they have some metadata available).
22 *
23 * @author niki
24 */
25 class Cbz extends Epub {
26 @Override
27 protected boolean supports(URL url) {
28 return url.toString().toLowerCase().endsWith(".cbz");
29 }
30
31 @Override
32 public String getSourceName() {
33 return "cbz";
34 }
35
36 @Override
37 protected String getDataPrefix() {
38 return "";
39 }
40
41 @Override
42 protected boolean requireInfo() {
43 return false;
44 }
45
46 @Override
47 protected boolean getCover() {
48 return false;
49 }
50
51 @Override
52 protected void preprocess(URL source, InputStream in) throws IOException {
53 super.preprocess(source, in);
54 meta.setImageDocument(true);
55 }
56
57 @Override
58 public Story process(URL url, Progress pg) throws IOException {
59 if (pg == null) {
60 pg = new Progress();
61 } else {
62 pg.setMinMax(0, 100);
63 }
64
65 Story story = processMeta(url, false, true);
66 story.setChapters(new ArrayList<Chapter>());
67 Chapter chap = new Chapter(1, null);
68 story.getChapters().add(chap);
69
70 ZipInputStream zipIn = new ZipInputStream(getInput());
71
72 pg.setProgress(10);
73 List<String> images = new ArrayList<String>();
74 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
75 .getNextEntry()) {
76 if (!entry.isDirectory()
77 && entry.getName().startsWith(getDataPrefix())) {
78 String entryLName = entry.getName().toLowerCase();
79 boolean imageEntry = false;
80 for (String ext : getImageExt(false)) {
81 if (entryLName.endsWith(ext)) {
82 imageEntry = true;
83 }
84 }
85
86 if (imageEntry) {
87 String uuid = meta.getUuid() + "_" + entry.getName();
88 images.add(uuid);
89 try {
90 Instance.getCache().addToCache(zipIn, uuid);
91 } catch (Exception e) {
92 Instance.syserr(e);
93 }
94 }
95 }
96 }
97
98 pg.setProgress(80);
99
100 // ZIP order is not sure
101 Collections.sort(images);
102 pg.setProgress(90);
103
104 for (String uuid : images) {
105 try {
106 chap.getParagraphs().add(
107 new Paragraph(new File(uuid).toURI().toURL()));
108 } catch (Exception e) {
109 Instance.syserr(e);
110 }
111 }
112
113 pg.setProgress(100);
114 return story;
115 }
116 }