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
.Paragraph
;
17 import be
.nikiroo
.fanfix
.data
.Story
;
18 import be
.nikiroo
.utils
.IOUtils
;
19 import be
.nikiroo
.utils
.Image
;
20 import be
.nikiroo
.utils
.Progress
;
23 * Support class for CBZ files (works better with CBZ created with this program,
24 * as they have some metadata available).
28 class Cbz
extends Epub
{
30 protected boolean supports(URL url
) {
31 return url
.toString().toLowerCase().endsWith(".cbz");
35 public String
getSourceName() {
40 protected String
getDataPrefix() {
45 protected boolean requireInfo() {
50 protected boolean isImagesDocumentByDefault() {
55 protected boolean getCover() {
60 public Story
process(URL url
, Progress pg
) throws IOException
{
67 Progress pgMeta
= new Progress();
68 pg
.addProgress(pgMeta
, 10);
69 Story story
= processMeta(url
, false, true, pgMeta
);
72 File tmpDir
= Instance
.getTempFiles().createTempDir("info-text");
73 String basename
= null;
75 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
77 ZipInputStream zipIn
= new ZipInputStream(getInput());
78 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
80 if (!entry
.isDirectory()
81 && entry
.getName().startsWith(getDataPrefix())) {
82 String entryLName
= entry
.getName().toLowerCase();
83 boolean imageEntry
= false;
84 for (String ext
: getImageExt(false)) {
85 if (entryLName
.endsWith(ext
)) {
91 String uuid
= meta
.getUuid() + "_" + entry
.getName();
93 images
.put(uuid
, new Image(zipIn
));
94 } catch (Exception e
) {
95 Instance
.getTraceHandler().error(e
);
98 if (pg
.getProgress() < 85) {
101 } else if (entryLName
.endsWith(".info")) {
102 basename
= entryLName
.substring(0, entryLName
.length()
104 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
105 } else if (entryLName
.endsWith(".txt")) {
106 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
113 // ZIP order is not correct for us
114 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
115 Collections
.sort(imagesList
);
119 File txt
= new File(tmpDir
, basename
+ ".txt");
123 if (basename
!= null) {
125 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
127 Story origStory
= support
.process(null);
128 story
.setChapters(origStory
.getChapters());
129 story
.setMeta(origStory
.getMeta());
130 } catch (Exception e
) {
135 if (basename
== null) {
136 story
.setChapters(new ArrayList
<Chapter
>());
139 if (!imagesList
.isEmpty()) {
140 Chapter chap
= new Chapter(story
.getChapters().size() + 1, null);
141 story
.getChapters().add(chap
);
143 for (String uuid
: imagesList
) {
145 chap
.getParagraphs().add(
146 new Paragraph(images
.get(uuid
)));
147 } catch (Exception e
) {
148 Instance
.getTraceHandler().error(e
);
153 if (meta
.getCover() == null && !images
.isEmpty()) {
154 meta
.setCover(images
.get(imagesList
.get(0)));
155 meta
.setFakeCover(true);
159 IOUtils
.deltree(tmpDir
);