1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Collections
;
10 import java
.util
.HashMap
;
11 import java
.util
.List
;
13 import java
.util
.zip
.ZipEntry
;
14 import java
.util
.zip
.ZipInputStream
;
16 import be
.nikiroo
.fanfix
.Instance
;
17 import be
.nikiroo
.fanfix
.data
.Chapter
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.fanfix
.data
.Paragraph
;
20 import be
.nikiroo
.fanfix
.data
.Story
;
21 import be
.nikiroo
.utils
.IOUtils
;
22 import be
.nikiroo
.utils
.Image
;
23 import be
.nikiroo
.utils
.MarkableFileInputStream
;
24 import be
.nikiroo
.utils
.Progress
;
27 * Support class for CBZ files (works better with CBZ created with this program,
28 * as they have some metadata available).
32 class Cbz
extends Epub
{
34 protected boolean supports(URL url
) {
35 return url
.toString().toLowerCase().endsWith(".cbz");
39 public String
getSourceName() {
44 protected String
getDataPrefix() {
49 protected boolean requireInfo() {
54 protected boolean isImagesDocumentByDefault() {
59 protected boolean getCover() {
64 public Story
doProcess(Progress pg
) throws IOException
{
71 Progress pgMeta
= new Progress();
72 pg
.addProgress(pgMeta
, 10);
73 Story story
= processMeta(true, pgMeta
);
74 MetaData meta
= story
.getMeta();
76 System
.out
.println("Meta from Cbz support: "+meta
);
80 File tmpDir
= Instance
.getTempFiles().createTempDir("info-text");
81 String basename
= null;
83 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
84 InputStream cbzIn
= null;
85 ZipInputStream zipIn
= null;
87 cbzIn
= new MarkableFileInputStream(new FileInputStream(
88 getSourceFileOriginal()));
89 zipIn
= new ZipInputStream(cbzIn
);
90 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
92 if (!entry
.isDirectory()
93 && entry
.getName().startsWith(getDataPrefix())) {
94 String entryLName
= entry
.getName().toLowerCase();
95 boolean imageEntry
= false;
96 for (String ext
: BasicSupportImages
.getImageExt(false)) {
97 if (entryLName
.endsWith(ext
)) {
103 String uuid
= meta
.getUuid() + "_" + entry
.getName();
105 images
.put(uuid
, new Image(zipIn
));
106 } catch (Exception e
) {
107 Instance
.getTraceHandler().error(e
);
110 if (pg
.getProgress() < 85) {
113 } else if (entryLName
.endsWith(".info")) {
114 basename
= entryLName
.substring(0, entryLName
.length()
116 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
117 } else if (entryLName
.endsWith(".txt")) {
118 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
125 // ZIP order is not correct for us
126 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
127 Collections
.sort(imagesList
);
131 // include original story
132 Story origStory
= getStoryFromTxt(tmpDir
, basename
);
133 if (origStory
!= null) {
134 story
.setChapters(origStory
.getChapters());
135 if (origStory
.getMeta().getCover() == null) {
136 origStory
.getMeta().setCover(story
.getMeta().getCover());
138 story
.setMeta(origStory
.getMeta());
140 story
.setChapters(new ArrayList
<Chapter
>());
143 if (!imagesList
.isEmpty()) {
144 Chapter chap
= new Chapter(story
.getChapters().size() + 1, null);
145 story
.getChapters().add(chap
);
147 for (String uuid
: imagesList
) {
149 chap
.getParagraphs().add(
150 new Paragraph(images
.get(uuid
)));
151 } catch (Exception e
) {
152 Instance
.getTraceHandler().error(e
);
157 if (meta
.getCover() == null && !images
.isEmpty()) {
158 meta
.setCover(images
.get(imagesList
.get(0)));
159 meta
.setFakeCover(true);
162 System
.out
.println("Meta from Cbz support at end: "+meta
);
164 IOUtils
.deltree(tmpDir
);
177 private Story
getStoryFromTxt(File tmpDir
, String basename
) {
178 Story origStory
= null;
180 File txt
= new File(tmpDir
, basename
+ ".txt");
184 if (basename
!= null) {
186 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
188 origStory
= support
.process(null);
189 } catch (Exception e
) {