ca0f48d2df6a0fc75d93b2bbf0fb705799420877
1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collections
;
8 import java
.util
.HashMap
;
11 import java
.util
.zip
.ZipEntry
;
12 import java
.util
.zip
.ZipInputStream
;
14 import be
.nikiroo
.fanfix
.Instance
;
15 import be
.nikiroo
.fanfix
.data
.Chapter
;
16 import be
.nikiroo
.fanfix
.data
.MetaData
;
17 import be
.nikiroo
.fanfix
.data
.Paragraph
;
18 import be
.nikiroo
.fanfix
.data
.Story
;
19 import be
.nikiroo
.utils
.IOUtils
;
20 import be
.nikiroo
.utils
.Image
;
21 import be
.nikiroo
.utils
.Progress
;
24 * Support class for CBZ files (works better with CBZ created with this program,
25 * as they have some metadata available).
29 class Cbz
extends Epub
{
31 protected boolean supports(URL url
) {
32 return url
.toString().toLowerCase().endsWith(".cbz");
36 public String
getSourceName() {
41 protected String
getDataPrefix() {
46 protected boolean requireInfo() {
51 protected boolean isImagesDocumentByDefault() {
56 protected boolean getCover() {
61 public Story
process(Progress pg
) throws IOException
{
68 Progress pgMeta
= new Progress();
69 pg
.addProgress(pgMeta
, 10);
70 Story story
= processMeta(true, pgMeta
);
71 MetaData meta
= story
.getMeta();
75 File tmpDir
= Instance
.getTempFiles().createTempDir("info-text");
76 String basename
= null;
78 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
80 ZipInputStream zipIn
= new ZipInputStream(getInput());
81 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
83 if (!entry
.isDirectory()
84 && entry
.getName().startsWith(getDataPrefix())) {
85 String entryLName
= entry
.getName().toLowerCase();
86 boolean imageEntry
= false;
87 for (String ext
: BasicSupportImages
.getImageExt(false)) {
88 if (entryLName
.endsWith(ext
)) {
94 String uuid
= meta
.getUuid() + "_" + entry
.getName();
96 images
.put(uuid
, new Image(zipIn
));
97 } catch (Exception e
) {
98 Instance
.getTraceHandler().error(e
);
101 if (pg
.getProgress() < 85) {
104 } else if (entryLName
.endsWith(".info")) {
105 basename
= entryLName
.substring(0, entryLName
.length()
107 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
108 } else if (entryLName
.endsWith(".txt")) {
109 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
116 // ZIP order is not correct for us
117 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
118 Collections
.sort(imagesList
);
122 File txt
= new File(tmpDir
, basename
+ ".txt");
126 if (basename
!= null) {
128 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
130 Story origStory
= support
.process(null);
131 story
.setChapters(origStory
.getChapters());
132 story
.setMeta(origStory
.getMeta());
133 } catch (Exception e
) {
138 if (basename
== null) {
139 story
.setChapters(new ArrayList
<Chapter
>());
142 if (!imagesList
.isEmpty()) {
143 Chapter chap
= new Chapter(story
.getChapters().size() + 1, null);
144 story
.getChapters().add(chap
);
146 for (String uuid
: imagesList
) {
148 chap
.getParagraphs().add(
149 new Paragraph(images
.get(uuid
)));
150 } catch (Exception e
) {
151 Instance
.getTraceHandler().error(e
);
156 if (meta
.getCover() == null && !images
.isEmpty()) {
157 meta
.setCover(images
.get(imagesList
.get(0)));
158 meta
.setFakeCover(true);
162 IOUtils
.deltree(tmpDir
);