EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.comic]

COVERAGE SUMMARY FOR SOURCE FILE [CreateZipArchiveTask.java]

nameclass, %method, %block, %line, %
CreateZipArchiveTask.java100% (1/1)100% (4/4)70%  (237/340)78%  (46.1/59)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateZipArchiveTask100% (1/1)100% (4/4)70%  (237/340)78%  (46.1/59)
CreateZipArchiveTask (File, File, String []): void 100% (1/1)67%  (60/90)74%  (11.2/15)
start (): void 100% (1/1)69%  (147/212)77%  (28.3/37)
CreateZipArchiveTask (): void 100% (1/1)78%  (18/23)96%  (5.8/6)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 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/>.
16package net.sf.jomic.comic;
17 
18import java.io.File;
19import java.io.FileInputStream;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.util.zip.ZipEntry;
23import java.util.zip.ZipOutputStream;
24 
25import net.sf.jomic.tools.AbstractTask;
26import net.sf.jomic.tools.FileTools;
27import net.sf.jomic.tools.IOExceptionWithCause;
28import net.sf.jomic.tools.LocaleTools;
29import net.sf.jomic.tools.StringTools;
30import net.sf.wraplog.Logger;
31 
32/**
33 *  Task to create a ZIP archive.
34 *
35 * @author    Thomas Aglassinger
36 */
37// TODO: Use antzip to encode zipped filenames using ISO-Latin1.
38public class CreateZipArchiveTask extends AbstractTask
39{
40    private static final int BUFFER_SIZE = 4096;
41    private FileTools fileTools;
42    private LocaleTools localeTools;
43    private Logger logger;
44    private File sourceBaseDir;
45    private String[] sourceFileNames;
46    private StringTools stringTools;
47    private File targetComicFile;
48 
49    /**
50     *  Create a new task to create a ZIP archive.
51     *
52     * @param  newTargetZipFile    the ZIP file to create
53     * @param  newSourceBaseDir    the base directory where the source files are located
54     * @param  newSourceFileNames  the names of the files relative to <code>newSourceBaseDir</code>
55     *      that should be included in the archive
56     */
57    public CreateZipArchiveTask(File newTargetZipFile, File newSourceBaseDir, String[] newSourceFileNames) {
58        this();
59        assert newTargetZipFile != null;
60        assert newSourceBaseDir != null;
61        assert newSourceFileNames != null;
62        assert newSourceFileNames.length > 0;
63        targetComicFile = newTargetZipFile;
64        sourceBaseDir = newSourceBaseDir;
65        sourceFileNames = newSourceFileNames;
66        for (int i = 0; i < sourceFileNames.length; i += 1) {
67            File sourceFile = new File(sourceBaseDir, sourceFileNames[i]);
68 
69            if (!sourceFile.isFile()) {
70                String message = localeTools.getMessage(
71                        "errors.itemAddedToZipArchiveMustBeFile", stringTools.sourced(sourceFile));
72 
73                throw new IllegalArgumentException(message);
74            }
75            setMaxProgress(getMaxProgress() + 1 + sourceFile.length());
76        }
77    }
78 
79    private CreateZipArchiveTask() {
80        super();
81        logger = Logger.getLogger(CreateZipArchiveTask.class);
82        fileTools = FileTools.instance();
83        localeTools = LocaleTools.instance();
84        stringTools = StringTools.instance();
85    }
86 
87    public void start()
88        throws IOException {
89        boolean done = false;
90 
91        if (logger.isInfoEnabled()) {
92            logger.info("create zip archive: " + targetComicFile);
93        }
94        byte[] buffer = new byte[BUFFER_SIZE];
95        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(targetComicFile));
96 
97        try {
98            setProgress(0);
99            for (int i = 0; !isInterrupted() && (i < sourceFileNames.length); i += 1) {
100                String outName = sourceFileNames[i];
101                File inName = new File(sourceBaseDir, outName);
102 
103                if (logger.isDebugEnabled()) {
104                    logger.debug("add " + stringTools.sourced(inName) + " as "
105                            + stringTools.sourced(outName));
106                }
107                ZipEntry zipEntry = new ZipEntry(outName);
108 
109                zipEntry.setTime(inName.lastModified());
110                out.putNextEntry(zipEntry);
111 
112                FileInputStream in = new FileInputStream(inName);
113 
114                try {
115                    int bytesRead;
116 
117                    do {
118                        bytesRead = in.read(buffer);
119                        if (bytesRead > 0) {
120                            out.write(buffer, 0, bytesRead);
121                            setProgress(getProgress() + buffer.length);
122                        }
123                    } while (!isInterrupted() && (bytesRead > 0));
124 
125                    // Make sure progress advances at least 1 unit per file.
126                    setProgress(getProgress() + 1);
127                } finally {
128                    in.close();
129                }
130                out.closeEntry();
131                done = true;
132            }
133        } finally {
134            Throwable cause = null;
135 
136            try {
137                out.close();
138            } catch (Throwable error) {
139                cause = error;
140            }
141            if (!done) {
142                // Delete incomplete archive.
143                fileTools.deleteOrWarn(targetComicFile, logger);
144            }
145            if (cause != null) {
146                String message = localeTools.getMessage("errors.cannotCreateArchive", targetComicFile);
147 
148                throw new IOExceptionWithCause(message, cause);
149            }
150        }
151    }
152}

[all classes][net.sf.jomic.comic]
EMMA 2.0.4217 (C) Vladimir Roubtsov