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