948a2d65ece471813f79af289f59b38a49fb8fec
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();
78 File tmpDir
= Instance
.getTempFiles().createTempDir("info-text");
79 String basename
= null;
81 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
82 InputStream cbzIn
= null;
83 ZipInputStream zipIn
= null;
85 cbzIn
= new MarkableFileInputStream(new FileInputStream(
86 getSourceFileOriginal()));
87 zipIn
= new ZipInputStream(cbzIn
);
88 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
90 if (!entry
.isDirectory()
91 && entry
.getName().startsWith(getDataPrefix())) {
92 String entryLName
= entry
.getName().toLowerCase();
93 boolean imageEntry
= false;
94 for (String ext
: BasicSupportImages
.getImageExt(false)) {
95 if (entryLName
.endsWith(ext
)) {
101 String uuid
= meta
.getUuid() + "_" + entry
.getName();
103 images
.put(uuid
, new Image(zipIn
));
104 } catch (Exception e
) {
105 Instance
.getTraceHandler().error(e
);
108 if (pg
.getProgress() < 85) {
111 } else if (entryLName
.endsWith(".info")) {
112 basename
= entryLName
.substring(0, entryLName
.length()
114 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
115 } else if (entryLName
.endsWith(".txt")) {
116 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
123 // ZIP order is not correct for us
124 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
125 Collections
.sort(imagesList
);
129 // include original story
130 Story origStory
= getStoryFromTxt(tmpDir
, basename
);
131 if (origStory
!= null) {
132 story
.setChapters(origStory
.getChapters());
133 story
.setMeta(origStory
.getMeta());
135 story
.setChapters(new ArrayList
<Chapter
>());
138 if (!imagesList
.isEmpty()) {
139 Chapter chap
= new Chapter(story
.getChapters().size() + 1, null);
140 story
.getChapters().add(chap
);
142 for (String uuid
: imagesList
) {
144 chap
.getParagraphs().add(
145 new Paragraph(images
.get(uuid
)));
146 } catch (Exception e
) {
147 Instance
.getTraceHandler().error(e
);
152 if (meta
.getCover() == null && !images
.isEmpty()) {
153 meta
.setCover(images
.get(imagesList
.get(0)));
154 meta
.setFakeCover(true);
158 IOUtils
.deltree(tmpDir
);
171 private Story
getStoryFromTxt(File tmpDir
, String basename
) {
172 Story origStory
= null;
174 File txt
= new File(tmpDir
, basename
+ ".txt");
178 if (basename
!= null) {
180 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
182 origStory
= support
.process(null);
183 } catch (Exception e
) {