| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 53 | | | |
| 54 | | | @author |
| 55 | | | @author |
| 56 | | | |
| 57 | | | public final class JarUtils |
| 58 | | | { |
| 59 | | | private static final int BUFFER_SIZE = 4096; |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | private JarUtils () |
| 65 | 0 | | { |
| 66 | | | |
| 67 | 0 | | } |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | <code></code> |
| 72 | | | |
| 73 | | | @param |
| 74 | | | |
| 75 | | | @param |
| 76 | | | @throws |
| 77 | | | |
| 78 | | | public static void extractJarArchive (File baseDir, File archive) |
| 79 | | | throws IOException |
| 80 | | | { |
| 81 | 0 | | final JarFile archiveFile = new JarFile(archive); |
| 82 | 0 | | final List archiveEntries = Collections.list(archiveFile.entries()); |
| 83 | 0 | | for (final Iterator iterator = archiveEntries.iterator(); iterator |
| 84 | 0 | | .hasNext();) |
| 85 | | | { |
| 86 | 0 | | InputStream in = null; |
| 87 | 0 | | FileOutputStream out = null; |
| 88 | | | try |
| 89 | | | { |
| 90 | 0 | | final ZipEntry e = (ZipEntry) iterator.next(); |
| 91 | 0 | | in = archiveFile.getInputStream(e); |
| 92 | 0 | | final File f = new File(baseDir, e.getName()); |
| 93 | 0 | | if (e.isDirectory()) |
| 94 | | | { |
| 95 | 0 | | if (!f.exists()) |
| 96 | | | { |
| 97 | 0 | | if (!f.mkdirs()) |
| 98 | | | { |
| 99 | 0 | | throw new IOException("Cannot create directory " |
| 100 | | | + f); |
| 101 | | | } |
| 102 | | | } |
| 103 | | | } |
| 104 | | | else |
| 105 | | | { |
| 106 | 0 | | if (!f.getParentFile().exists()) |
| 107 | | | { |
| 108 | 0 | | if (!f.getParentFile().mkdirs()) |
| 109 | | | { |
| 110 | 0 | | throw new IOException("Cannot create directory " |
| 111 | | | + f.getParentFile()); |
| 112 | | | } |
| 113 | | | } |
| 114 | 0 | | out = new FileOutputStream(f); |
| 115 | 0 | | copy(in, out); |
| 116 | | | } |
| 117 | | | } |
| 118 | | | finally |
| 119 | | | { |
| 120 | 0 | | IoUtil.close(out); |
| 121 | 0 | | IoUtil.close(in); |
| 122 | 0 | | } |
| 123 | 0 | | } |
| 124 | 0 | | } |
| 125 | | | |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | @param |
| 130 | | | @param |
| 131 | | | @throws |
| 132 | | | |
| 133 | | | public static void createJarArchive (File baseDir, File archive) |
| 134 | | | throws IOException |
| 135 | | | { |
| 136 | 0 | | JarOutputStream jarArchive = null; |
| 137 | | | try |
| 138 | | | { |
| 139 | 0 | | jarArchive = new JarOutputStream(new FileOutputStream(archive)); |
| 140 | 0 | | addFileToJar(baseDir, baseDir, jarArchive); |
| 141 | | | } |
| 142 | | | finally |
| 143 | | | { |
| 144 | 0 | | IoUtil.close(jarArchive); |
| 145 | 0 | | } |
| 146 | 0 | | } |
| 147 | | | |
| 148 | | | private static void addFileToJar (File baseDir, File file, |
| 149 | | | JarOutputStream archive) |
| 150 | | | throws IOException |
| 151 | | | { |
| 152 | 0 | | if (file == null) |
| 153 | | | { |
| 154 | | | |
| 155 | | | } |
| 156 | 0 | | else if (file.isDirectory()) |
| 157 | | | { |
| 158 | 0 | | String path = FileUtils.getRelativePath(baseDir, file); |
| 159 | 0 | (1)(2)(3) | if (!path.equals("/") && !path.equals("")) |
| 160 | | | { |
| 161 | 0 | | if (!path.endsWith("/")) |
| 162 | | | { |
| 163 | 0 | (4) | path += "/"; |
| 164 | | | } |
| 165 | 0 | | final JarEntry entry = new JarEntry(path); |
| 166 | 0 | | archive.putNextEntry(entry); |
| 167 | 0 | (5) | archive.closeEntry(); |
| 168 | | | } |
| 169 | 0 | | final File[] files = file.listFiles(); |
| 170 | 0 | | for (int i = 0; i < files.length; i++) |
| 171 | | | { |
| 172 | 0 | | addFileToJar(baseDir, files[i], archive); |
| 173 | | | } |
| 174 | 0 | | } |
| 175 | | | else |
| 176 | | | { |
| 177 | 0 | | final String path = FileUtils.getRelativePath(baseDir, file); |
| 178 | 0 | | final JarEntry entry = new JarEntry(path); |
| 179 | 0 | | archive.putNextEntry(entry); |
| 180 | 0 | | InputStream in = null; |
| 181 | | | try |
| 182 | | | { |
| 183 | 0 | | in = new FileInputStream(file); |
| 184 | 0 | | copy(in, archive); |
| 185 | | | } |
| 186 | | | finally |
| 187 | | | { |
| 188 | 0 | | IoUtil.close(in); |
| 189 | 0 | | } |
| 190 | 0 | | archive.closeEntry(); |
| 191 | | | } |
| 192 | 0 | | } |
| 193 | | | |
| 194 | | | |
| 195 | | | <code></code> |
| 196 | | | <code></code> |
| 197 | | | |
| 198 | | | @param |
| 199 | | | @param |
| 200 | | | @throws |
| 201 | | | |
| 202 | | | private static void copy (InputStream in, OutputStream out) |
| 203 | | | throws IOException |
| 204 | | | { |
| 205 | 0 | | final byte[] buffer = new byte[BUFFER_SIZE]; |
| 206 | | | int nread; |
| 207 | 0 | | while ((nread = in.read(buffer)) != -1) |
| 208 | | | { |
| 209 | 0 | | out.write(buffer, 0, nread); |
| 210 | | | } |
| 211 | 0 | | } |
| 212 | | | } |