| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.comic; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.FileInputStream; |
| 20 | import java.io.FileOutputStream; |
| 21 | import java.io.IOException; |
| 22 | import java.util.zip.ZipEntry; |
| 23 | import java.util.zip.ZipOutputStream; |
| 24 | |
| 25 | import net.sf.jomic.tools.AbstractTask; |
| 26 | import net.sf.jomic.tools.FileTools; |
| 27 | import net.sf.jomic.tools.IOExceptionWithCause; |
| 28 | import net.sf.jomic.tools.LocaleTools; |
| 29 | import net.sf.jomic.tools.StringTools; |
| 30 | |
| 31 | import org.apache.commons.logging.Log; |
| 32 | import org.apache.commons.logging.LogFactory; |
| 33 | |
| 34 | /** |
| 35 | * Task to create a ZIP archive. |
| 36 | * |
| 37 | * @author Thomas Aglassinger |
| 38 | */ |
| 39 | // TODO: Use antzip to encode zipped filenames using ISO-Latin1. |
| 40 | public class CreateZipArchiveTask extends AbstractTask |
| 41 | { |
| 42 | private static final int BUFFER_SIZE = 4096; |
| 43 | private FileTools fileTools; |
| 44 | private LocaleTools localeTools; |
| 45 | private Log logger; |
| 46 | private File sourceBaseDir; |
| 47 | private String[] sourceFileNames; |
| 48 | private StringTools stringTools; |
| 49 | private File targetComicFile; |
| 50 | |
| 51 | /** |
| 52 | * Create a new task to create a ZIP archive. |
| 53 | * |
| 54 | * @param newTargetZipFile the ZIP file to create |
| 55 | * @param newSourceBaseDir the base directory where the source files are located |
| 56 | * @param newSourceFileNames the names of the files relative to <code>newSourceBaseDir</code> |
| 57 | * that should be included in the archive |
| 58 | */ |
| 59 | public CreateZipArchiveTask(File newTargetZipFile, File newSourceBaseDir, String[] newSourceFileNames) { |
| 60 | this(); |
| 61 | assert newTargetZipFile != null; |
| 62 | assert newSourceBaseDir != null; |
| 63 | assert newSourceFileNames != null; |
| 64 | assert newSourceFileNames.length > 0; |
| 65 | targetComicFile = newTargetZipFile; |
| 66 | sourceBaseDir = newSourceBaseDir; |
| 67 | sourceFileNames = newSourceFileNames; |
| 68 | for (int i = 0; i < sourceFileNames.length; i += 1) { |
| 69 | File sourceFile = new File(sourceBaseDir, sourceFileNames[i]); |
| 70 | |
| 71 | if (!sourceFile.isFile()) { |
| 72 | String message = localeTools.getMessage( |
| 73 | "errors.itemAddedToZipArchiveMustBeFile", stringTools.sourced(sourceFile)); |
| 74 | |
| 75 | throw new IllegalArgumentException(message); |
| 76 | } |
| 77 | setMaxProgress(getMaxProgress() + 1 + sourceFile.length()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private CreateZipArchiveTask() { |
| 82 | super(); |
| 83 | logger = LogFactory.getLog(CreateZipArchiveTask.class); |
| 84 | fileTools = FileTools.instance(); |
| 85 | localeTools = LocaleTools.instance(); |
| 86 | stringTools = StringTools.instance(); |
| 87 | } |
| 88 | |
| 89 | public void start() |
| 90 | throws IOException { |
| 91 | boolean done = false; |
| 92 | |
| 93 | if (logger.isInfoEnabled()) { |
| 94 | logger.info("create zip archive: " + targetComicFile); |
| 95 | } |
| 96 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 97 | ZipOutputStream out = new ZipOutputStream(new FileOutputStream(targetComicFile)); |
| 98 | |
| 99 | try { |
| 100 | setProgress(0); |
| 101 | for (int i = 0; !isInterrupted() && (i < sourceFileNames.length); i += 1) { |
| 102 | String outName = sourceFileNames[i]; |
| 103 | File inName = new File(sourceBaseDir, outName); |
| 104 | |
| 105 | if (logger.isDebugEnabled()) { |
| 106 | logger.debug("add " + stringTools.sourced(inName) + " as " |
| 107 | + stringTools.sourced(outName)); |
| 108 | } |
| 109 | ZipEntry zipEntry = new ZipEntry(outName); |
| 110 | |
| 111 | zipEntry.setTime(inName.lastModified()); |
| 112 | out.putNextEntry(zipEntry); |
| 113 | |
| 114 | FileInputStream in = new FileInputStream(inName); |
| 115 | |
| 116 | try { |
| 117 | int bytesRead; |
| 118 | |
| 119 | do { |
| 120 | bytesRead = in.read(buffer); |
| 121 | if (bytesRead > 0) { |
| 122 | out.write(buffer, 0, bytesRead); |
| 123 | setProgress(getProgress() + buffer.length); |
| 124 | } |
| 125 | } while (!isInterrupted() && (bytesRead > 0)); |
| 126 | |
| 127 | // Make sure progress advances at least 1 unit per file. |
| 128 | setProgress(getProgress() + 1); |
| 129 | } finally { |
| 130 | in.close(); |
| 131 | } |
| 132 | out.closeEntry(); |
| 133 | done = true; |
| 134 | } |
| 135 | } finally { |
| 136 | Throwable cause = null; |
| 137 | |
| 138 | try { |
| 139 | out.close(); |
| 140 | } catch (Throwable error) { |
| 141 | cause = error; |
| 142 | } |
| 143 | if (!done) { |
| 144 | // Delete incomplete archive. |
| 145 | fileTools.deleteOrWarn(targetComicFile, logger); |
| 146 | } |
| 147 | if (cause != null) { |
| 148 | String message = localeTools.getMessage("errors.cannotCreateArchive", targetComicFile); |
| 149 | |
| 150 | throw new IOExceptionWithCause(message, cause); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |