Version 1.1.0
[fanfix.git] / src / be / nikiroo / fanfix / supported / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.URL;
7import java.util.ArrayList;
d3c84ac3
NR
8import java.util.Collections;
9import java.util.List;
08fe2e33
NR
10import java.util.zip.ZipEntry;
11import java.util.zip.ZipInputStream;
12
13import be.nikiroo.fanfix.Instance;
14import be.nikiroo.fanfix.data.Chapter;
15import be.nikiroo.fanfix.data.Paragraph;
16import be.nikiroo.fanfix.data.Story;
92fb0719 17import be.nikiroo.utils.ui.Progress;
08fe2e33
NR
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 */
25class 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 }
08fe2e33
NR
45
46 @Override
47 protected boolean getCover() {
48 return false;
49 }
50
68686a37
NR
51 @Override
52 protected void preprocess(URL source, InputStream in) throws IOException {
53 super.preprocess(source, in);
54 meta.setImageDocument(true);
55 }
56
08fe2e33 57 @Override
92fb0719
NR
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
08fe2e33
NR
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
92fb0719 72 pg.setProgress(10);
d3c84ac3 73 List<String> images = new ArrayList<String>();
08fe2e33
NR
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) {
d3c84ac3
NR
87 String uuid = meta.getUuid() + "_" + entry.getName();
88 images.add(uuid);
08fe2e33 89 try {
08fe2e33 90 Instance.getCache().addToCache(zipIn, uuid);
08fe2e33
NR
91 } catch (Exception e) {
92 Instance.syserr(e);
93 }
94 }
95 }
96 }
97
92fb0719
NR
98 pg.setProgress(80);
99
d3c84ac3
NR
100 // ZIP order is not sure
101 Collections.sort(images);
92fb0719 102 pg.setProgress(90);
d3c84ac3
NR
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
92fb0719 113 pg.setProgress(100);
08fe2e33
NR
114 return story;
115 }
116}