| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.util; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | import java.io.File; |
|---|
| 37 | import java.io.FileInputStream; |
|---|
| 38 | import java.io.FileOutputStream; |
|---|
| 39 | import java.io.IOException; |
|---|
| 40 | import java.io.InputStream; |
|---|
| 41 | import java.io.OutputStream; |
|---|
| 42 | import java.util.Collections; |
|---|
| 43 | import java.util.Iterator; |
|---|
| 44 | import java.util.List; |
|---|
| 45 | import java.util.jar.JarEntry; |
|---|
| 46 | import java.util.jar.JarFile; |
|---|
| 47 | import java.util.jar.JarOutputStream; |
|---|
| 48 | import java.util.zip.ZipEntry; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Jar Utility class. |
|---|
| 53 | * |
|---|
| 54 | * @author Michael Griffel |
|---|
| 55 | * @author Andreas Mandel |
|---|
| 56 | */ |
|---|
| 57 | public final class JarUtils |
|---|
| 58 | { |
|---|
| 59 | private static final int BUFFER_SIZE = 4096; |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Utility class - no instances allowed. |
|---|
| 63 | */ |
|---|
| 64 | private JarUtils () |
|---|
| 65 | { |
|---|
| 66 | // no instances allowed -- provides only static helper functions |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Extract a jar archive into the base directory |
|---|
| 71 | * <code>baseDir</code>. |
|---|
| 72 | * |
|---|
| 73 | * @param baseDir the root directory where the archive is extracted |
|---|
| 74 | * to. |
|---|
| 75 | * @param archive jar file. |
|---|
| 76 | * @throws IOException in case of an I/O error. |
|---|
| 77 | */ |
|---|
| 78 | public static void extractJarArchive (File baseDir, File archive) |
|---|
| 79 | throws IOException |
|---|
| 80 | { |
|---|
| 81 | final JarFile archiveFile = new JarFile(archive); |
|---|
| 82 | final List archiveEntries = Collections.list(archiveFile.entries()); |
|---|
| 83 | for (final Iterator iterator = archiveEntries.iterator(); iterator |
|---|
| 84 | .hasNext();) |
|---|
| 85 | { |
|---|
| 86 | InputStream in = null; |
|---|
| 87 | FileOutputStream out = null; |
|---|
| 88 | try |
|---|
| 89 | { |
|---|
| 90 | final ZipEntry e = (ZipEntry) iterator.next(); |
|---|
| 91 | in = archiveFile.getInputStream(e); |
|---|
| 92 | final File f = new File(baseDir, e.getName()); |
|---|
| 93 | if (e.isDirectory()) |
|---|
| 94 | { |
|---|
| 95 | if (!f.exists()) |
|---|
| 96 | { |
|---|
| 97 | if (!f.mkdirs()) |
|---|
| 98 | { |
|---|
| 99 | throw new IOException("Cannot create directory " |
|---|
| 100 | + f); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | if (!f.getParentFile().exists()) |
|---|
| 107 | { |
|---|
| 108 | if (!f.getParentFile().mkdirs()) |
|---|
| 109 | { |
|---|
| 110 | throw new IOException("Cannot create directory " |
|---|
| 111 | + f.getParentFile()); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | out = new FileOutputStream(f); |
|---|
| 115 | copy(in, out); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | finally |
|---|
| 119 | { |
|---|
| 120 | IoUtil.close(out); |
|---|
| 121 | IoUtil.close(in); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Creates a jar archive from the directory. |
|---|
| 128 | * |
|---|
| 129 | * @param baseDir for the jar archive. |
|---|
| 130 | * @param archive the jar file. |
|---|
| 131 | * @throws IOException in case of an I/O error. |
|---|
| 132 | */ |
|---|
| 133 | public static void createJarArchive (File baseDir, File archive) |
|---|
| 134 | throws IOException |
|---|
| 135 | { |
|---|
| 136 | JarOutputStream jarArchive = null; |
|---|
| 137 | try |
|---|
| 138 | { |
|---|
| 139 | jarArchive = new JarOutputStream(new FileOutputStream(archive)); |
|---|
| 140 | addFileToJar(baseDir, baseDir, jarArchive); |
|---|
| 141 | } |
|---|
| 142 | finally |
|---|
| 143 | { |
|---|
| 144 | IoUtil.close(jarArchive); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | private static void addFileToJar (File baseDir, File file, |
|---|
| 149 | JarOutputStream archive) |
|---|
| 150 | throws IOException |
|---|
| 151 | { |
|---|
| 152 | if (file == null) |
|---|
| 153 | { |
|---|
| 154 | // done |
|---|
| 155 | } |
|---|
| 156 | else if (file.isDirectory()) |
|---|
| 157 | { |
|---|
| 158 | String path = FileUtils.getRelativePath(baseDir, file); |
|---|
| 159 | if (!path.equals("/") && !path.equals("")) |
|---|
| 160 | { |
|---|
| 161 | if (!path.endsWith("/")) |
|---|
| 162 | { |
|---|
| 163 | path += "/"; |
|---|
| 164 | } |
|---|
| 165 | final JarEntry entry = new JarEntry(path); |
|---|
| 166 | archive.putNextEntry(entry); |
|---|
| 167 | archive.closeEntry(); |
|---|
| 168 | } |
|---|
| 169 | final File[] files = file.listFiles(); |
|---|
| 170 | for (int i = 0; i < files.length; i++) |
|---|
| 171 | { |
|---|
| 172 | addFileToJar(baseDir, files[i], archive); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | else |
|---|
| 176 | { |
|---|
| 177 | final String path = FileUtils.getRelativePath(baseDir, file); |
|---|
| 178 | final JarEntry entry = new JarEntry(path); |
|---|
| 179 | archive.putNextEntry(entry); |
|---|
| 180 | InputStream in = null; |
|---|
| 181 | try |
|---|
| 182 | { |
|---|
| 183 | in = new FileInputStream(file); |
|---|
| 184 | copy(in, archive); |
|---|
| 185 | } |
|---|
| 186 | finally |
|---|
| 187 | { |
|---|
| 188 | IoUtil.close(in); |
|---|
| 189 | } |
|---|
| 190 | archive.closeEntry(); |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /** |
|---|
| 195 | * Copies the content of the input stream <code>in</code> to the |
|---|
| 196 | * output stream <code>out</code>. |
|---|
| 197 | * |
|---|
| 198 | * @param in input stream. |
|---|
| 199 | * @param out output stream. |
|---|
| 200 | * @throws IOException in case of an I/O error. |
|---|
| 201 | */ |
|---|
| 202 | private static void copy (InputStream in, OutputStream out) |
|---|
| 203 | throws IOException |
|---|
| 204 | { |
|---|
| 205 | final byte[] buffer = new byte[BUFFER_SIZE]; |
|---|
| 206 | int nread; |
|---|
| 207 | while ((nread = in.read(buffer)) != -1) |
|---|
| 208 | { |
|---|
| 209 | out.write(buffer, 0, nread); |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | } |
|---|