1 22 package org.jboss.deployment.spi; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.ArrayList ; 30 import java.util.jar.JarEntry ; 31 import java.util.jar.JarInputStream ; 32 import java.util.jar.JarOutputStream ; 33 34 import org.jboss.logging.Logger; 35 36 42 public class JarUtils 43 { 44 private static final Logger log = Logger.getLogger(JarUtils.class); 46 47 50 public static String [] addJar(JarOutputStream outputStream, String prefix, File jar) throws IOException 51 { 52 log.trace("addJar: " + jar); 53 ArrayList tmp = new ArrayList (); 54 FileInputStream fis = new FileInputStream (jar); 55 JarInputStream jis = new JarInputStream (fis); 56 JarEntry entry = jis.getNextJarEntry(); 57 while (entry != null) 58 { 59 if (entry.isDirectory() == false) 60 { 61 String entryName = prefix + entry.getName(); 62 tmp.add(entryName); 63 addJarEntry(outputStream, entryName, jis); 64 } 65 entry = jis.getNextJarEntry(); 66 } 67 jis.close(); 68 String [] names = new String [tmp.size()]; 69 tmp.toArray(names); 70 return names; 71 } 72 73 76 public static void addJarEntry(JarOutputStream outputStream, String entryName, InputStream inputStream) throws IOException 77 { 78 log.trace("addJarEntry: " + entryName); 79 outputStream.putNextEntry(new JarEntry (entryName)); 80 copyStream(outputStream, inputStream); 81 } 82 83 86 public static void copyStream(OutputStream outputStream, InputStream inputStream) throws IOException 87 { 88 byte[] bytes = new byte[4096]; 89 int read = inputStream.read(bytes, 0, 4096); 90 while (read > 0) 91 { 92 outputStream.write(bytes, 0, read); 93 read = inputStream.read(bytes, 0, 4096); 94 } 95 } 96 97 } 98 | Popular Tags |