| 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 | public boolean isImageDocument(URL source, InputStream in) |
| 45 | throws IOException { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected boolean getCover() { |
| 51 | return false; |
| 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 | // we assume that we can get the UUID without a stream |
| 78 | String uuid = getUuid(url, null) + "_" |
| 79 | + entry.getName(); |
| 80 | |
| 81 | Instance.getCache().addToCache(zipIn, uuid); |
| 82 | chap.getParagraphs().add( |
| 83 | new Paragraph(new File(uuid).toURI().toURL())); |
| 84 | } catch (Exception e) { |
| 85 | Instance.syserr(e); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return story; |
| 92 | } |
| 93 | } |