1 33 34 package edu.rice.cs.util.jar; 35 36 import java.io.*; 37 import java.util.jar.JarEntry ; 38 import java.util.jar.JarOutputStream ; 39 import java.util.jar.Manifest ; 40 41 public class JarBuilder { 42 private JarOutputStream _output; 43 44 50 public JarBuilder(File file) throws IOException { 51 _output = new JarOutputStream (new BufferedOutputStream(new FileOutputStream(file)), ManifestWriter.DEFAULT); 52 } 53 54 61 public JarBuilder(File jar, File manifest) throws IOException { 62 _output = new JarOutputStream (new BufferedOutputStream(new FileOutputStream(jar)), new Manifest (new FileInputStream(manifest))); 63 } 64 65 72 public JarBuilder(File jar, Manifest manifest) { 73 try { 74 _output = new JarOutputStream (new BufferedOutputStream(new FileOutputStream(jar)), manifest); 75 } 76 catch (IOException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 88 private String makeName(String parent, String name) { 89 String sep = "/"; if( parent.equals("") ) 91 return name; 92 if (parent.endsWith(sep)) 93 return parent + name; 94 return parent + sep + name; 95 } 96 97 104 public void addFile(File file, String parent, String fileName) throws IOException { 105 byte data[] = new byte[2048]; 106 107 FileInputStream fi = new FileInputStream(file.getAbsolutePath()); 108 BufferedInputStream origin = new BufferedInputStream(fi, 2048); 109 110 JarEntry entry = new JarEntry (makeName(parent, fileName)); 111 _output.putNextEntry(entry); 112 113 int count = origin.read(data, 0, 2048); 114 while (count != -1) { 115 _output.write(data, 0, count); 116 count = origin.read(data, 0, 2048); 117 } 118 119 origin.close(); 120 } 121 122 126 public void addDirectoryRecursive(File dir, String parent) { 127 addDirectoryRecursiveHelper(dir, parent, new byte[2048], new FileFilter() { 128 public boolean accept(File pathname) { return true; } 129 }); 130 } 131 132 138 public void addDirectoryRecursive(File dir, String parent, FileFilter filter) { 139 addDirectoryRecursiveHelper(dir, parent, new byte[2048], filter); 140 } 141 142 150 private boolean addDirectoryRecursiveHelper(File dir, String parent, byte[] buffer, FileFilter filter) { 151 try { 152 File[] files = dir.listFiles(filter); 153 BufferedInputStream origin = null; 154 155 if( files == null ) return true; 157 for (int i = 0; i < files.length; i++) { 158 if( files[i].isFile() ) { 159 origin = new BufferedInputStream(new FileInputStream(files[i]), 2048); 160 161 JarEntry entry = new JarEntry (makeName(parent, files[i].getName())); 162 _output.putNextEntry(entry); 163 164 int count; 165 while((count = origin.read(buffer, 0, 2048)) != -1) { 166 _output.write(buffer, 0, count); 167 } 168 origin.close(); 169 } 170 else if( files[i].isDirectory() ) { 171 addDirectoryRecursiveHelper(files[i], makeName(parent, files[i].getName()),buffer,filter); 172 } 173 } 174 } catch(Exception e) { 175 e.printStackTrace(); 176 } 177 return true; 178 } 179 180 187 public boolean makeDirectory(String parent, String dirName) { 188 JarEntry entry = new JarEntry (makeName(parent, dirName)); 189 try { 190 _output.putNextEntry(entry); 191 } 192 catch (IOException e) { 193 return false; 194 } 195 return true; 196 } 197 198 201 public void close() throws IOException { 202 _output.flush(); 203 _output.close(); 204 } 205 } | Popular Tags |