1 package csdl.jblanket.util; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.InputStream ; 7 import java.io.IOException ; 8 9 import java.util.Enumeration ; 10 import java.util.jar.JarEntry ; 11 import java.util.jar.JarException ; 12 import java.util.jar.JarFile ; 13 import java.util.jar.JarOutputStream ; 14 import java.util.zip.CRC32 ; 15 16 import org.apache.tools.ant.DirectoryScanner; 17 18 31 public class JarFactory { 32 33 34 private byte[] buffer; 35 36 37 private String dir; 38 39 40 private static final String SLASH = File.separator; 41 42 private static final String MAN_DIR = "META-INF"; 43 44 private static final String MAN_FILE_PATH = MAN_DIR + SLASH + "MANIFEST.MF"; 45 46 51 public JarFactory(String dir) { 52 53 this.dir = dir; 54 this.buffer = new byte[8092]; 55 } 56 57 64 public void unJar(File jarFile) throws IOException , JarException { 65 66 if (!jarFile.getName().endsWith(".jar")) { 67 throw new JarException ("Not a zip file? " + jarFile.getName()); 68 } 69 70 JarFile jar = new JarFile (jarFile); 72 Enumeration all = jar.entries(); 73 while (all.hasMoreElements()) { 74 getEntry(jar, ((JarEntry ) (all.nextElement()))); 75 } 76 77 jar.close(); 78 } 79 80 87 private void getEntry(JarFile jarFile, JarEntry entry) throws IOException { 88 89 String entryName = entry.getName(); 90 if (entryName.endsWith("/")) { 92 new File (dir, entryName).mkdirs(); 93 return; 94 } 95 96 File f = new File (dir, entryName); 97 98 if (!f.getParentFile().exists()) { 99 f.getParentFile().mkdirs(); 100 } 101 102 FileOutputStream fostream = new FileOutputStream (f); 104 InputStream istream = jarFile.getInputStream(entry); 105 106 int n = 0; 108 while ((n = istream.read(buffer)) > 0) { 109 fostream.write(buffer, 0, n); 110 } 111 112 try { 113 istream.close(); 114 fostream.close(); 115 } 116 catch (IOException e) { 117 } 119 } 120 121 128 public void jar(File jarFile) throws IOException , JarException { 129 130 if (!jarFile.getName().endsWith(".jar")) { 131 throw new JarException ("Not a zip file? " + jarFile.getName()); 132 } 133 134 DirectoryScanner scanner = new DirectoryScanner(); 136 scanner.setIncludes(new String [] {"**"}); 137 scanner.setExcludes(new String [] {"**" + SLASH + MAN_DIR + SLASH + "**"}); 138 scanner.setBasedir(new File (this.dir)); 139 scanner.setCaseSensitive(true); 140 scanner.scan(); 141 String [] directories = scanner.getIncludedDirectories(); 142 String [] files = scanner.getIncludedFiles(); 143 144 FileOutputStream fostream = new FileOutputStream (jarFile); 146 JarOutputStream jostream = new JarOutputStream (fostream); 147 148 if ((new File (dir, MAN_DIR)).exists()) { 150 putManifest(jostream); 151 } 152 153 String currentDirectory = ""; 155 for (int i = 0; i < directories.length; i++) { 156 if (directories[i].equals(currentDirectory)) { 158 continue; 159 } 160 putEntry(directories[i] + SLASH, jostream); 161 } 162 163 for (int i = 0; i < files.length; i++) { 165 putEntry(files[i], jostream); 166 } 167 168 jostream.finish(); 169 170 try { 171 jostream.close(); 172 fostream.close(); 173 } 174 catch (IOException e) { 175 } 177 } 178 179 185 private void putManifest(JarOutputStream jostream) throws IOException { 186 187 String manDirPath = MAN_DIR + SLASH; 190 JarEntry manifestDirEntry = new JarEntry (manDirPath.replace(File.separatorChar, '/')); 191 manifestDirEntry.setTime(System.currentTimeMillis()); 192 manifestDirEntry.setSize(0); 193 manifestDirEntry.setMethod(JarEntry.STORED); 194 195 manifestDirEntry.setCrc((new CRC32 ()).getValue()); 197 jostream.putNextEntry(manifestDirEntry); 198 199 JarEntry manifestFileEntry = new JarEntry (MAN_FILE_PATH.replace(File.separatorChar, '/')); 201 manifestFileEntry.setTime(System.currentTimeMillis()); 202 FileInputStream fistream = new FileInputStream (new File (this.dir, MAN_FILE_PATH)); 203 jostream.putNextEntry(manifestFileEntry); 204 205 int n = 0; 206 while ((n = fistream.read(buffer)) >= 0) { 207 jostream.write(buffer, 0, n); 208 } 209 jostream.closeEntry(); 210 211 try { 213 fistream.close(); 214 } 215 catch (IOException e) { 216 } 218 } 219 220 227 private void putEntry(String fileName, JarOutputStream jostream) throws IOException { 228 229 String entryName = fileName.replace(File.separatorChar, '/'); 231 232 if (entryName.endsWith("/")) { 234 jostream.putNextEntry(new JarEntry (entryName)); 235 jostream.closeEntry(); 236 return; 237 } 238 239 FileInputStream fistream = new FileInputStream (new File (this.dir, fileName)); 241 jostream.putNextEntry(new JarEntry (entryName)); 242 int n; 243 244 while ((n = fistream.read(buffer)) >= 0) { 246 jostream.write(buffer, 0, n); 247 } 248 249 jostream.closeEntry(); 250 251 try { 252 fistream.close(); 253 } 254 catch (IOException e) { 255 } 257 } 258 } | Popular Tags |