Fix CBZ importer, improve GUI
[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;
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 */
24class 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 }
08fe2e33
NR
44
45 @Override
46 protected boolean getCover() {
47 return false;
48 }
49
68686a37
NR
50 @Override
51 protected void preprocess(URL source, InputStream in) throws IOException {
52 super.preprocess(source, in);
53 meta.setImageDocument(true);
54 }
55
08fe2e33
NR
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
d3c84ac3 65 List<String> images = new ArrayList<String>();
08fe2e33
NR
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) {
d3c84ac3
NR
79 String uuid = meta.getUuid() + "_" + entry.getName();
80 images.add(uuid);
08fe2e33 81 try {
08fe2e33 82 Instance.getCache().addToCache(zipIn, uuid);
08fe2e33
NR
83 } catch (Exception e) {
84 Instance.syserr(e);
85 }
86 }
87 }
88 }
89
d3c84ac3
NR
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
08fe2e33
NR
102 return story;
103 }
104}