1 21 package proguard.io; 22 23 import proguard.util.*; 24 25 import java.io.*; 26 27 28 34 public class DataEntryCopier implements DataEntryReader 35 { 36 private static final int BUFFER_SIZE = 1024; 37 38 private DataEntryWriter dataEntryWriter; 39 private byte[] buffer = new byte[BUFFER_SIZE]; 40 41 42 43 public DataEntryCopier(DataEntryWriter dataEntryWriter) 44 { 45 this.dataEntryWriter = dataEntryWriter; 46 } 47 48 49 51 public void read(DataEntry dataEntry) throws IOException 52 { 53 try 54 { 55 OutputStream outputStream = dataEntryWriter.getOutputStream(dataEntry); 57 if (outputStream != null) 58 { 59 InputStream inputStream = dataEntry.getInputStream(); 60 61 copyData(inputStream, outputStream); 63 64 dataEntry.closeInputStream(); 66 } 67 } 68 catch (IOException ex) 69 { 70 System.err.println("Warning: can't write resource [" + dataEntry.getName() + "] (" + ex.getMessage() + ")"); 71 } 72 } 73 74 75 77 81 private void copyData(InputStream inputStream, 82 OutputStream outputStream) 83 throws IOException 84 { 85 while (true) 86 { 87 int count = inputStream.read(buffer); 88 if (count < 0) 89 { 90 break; 91 } 92 outputStream.write(buffer, 0, count); 93 } 94 95 outputStream.flush(); 96 } 97 98 99 102 public static void main(String [] args) 103 { 104 try 105 { 106 String input = args[0]; 107 String output = args[1]; 108 109 boolean outputIsJar = output.endsWith(".jar"); 110 boolean outputIsWar = output.endsWith(".war"); 111 boolean outputIsEar = output.endsWith(".ear"); 112 boolean outputIsZip = output.endsWith(".zip"); 113 114 DataEntryWriter writer = new DirectoryWriter(new File(output), 115 outputIsJar || 116 outputIsWar || 117 outputIsEar || 118 outputIsZip); 119 120 if (!outputIsJar) 121 { 122 DataEntryWriter zipWriter = new JarWriter(writer); 124 if (outputIsZip) 125 { 126 writer = zipWriter; 128 } 129 else 130 { 131 writer = new FilteredDataEntryWriter(new DataEntryParentFilter( 133 new DataEntryNameFilter( 134 new ExtensionMatcher(".zip"))), 135 zipWriter, 136 writer); 137 } 138 139 DataEntryWriter warWriter = new JarWriter(writer); 141 if (outputIsWar) 142 { 143 writer = warWriter; 145 } 146 else 147 { 148 writer = new FilteredDataEntryWriter(new DataEntryParentFilter( 150 new DataEntryNameFilter( 151 new ExtensionMatcher(".war"))), 152 warWriter, 153 writer); 154 } 155 } 156 157 DataEntryWriter jarWriter = new JarWriter(writer); 159 if (outputIsJar) 160 { 161 writer = jarWriter; 163 } 164 else 165 { 166 writer = new FilteredDataEntryWriter(new DataEntryParentFilter( 168 new DataEntryNameFilter( 169 new ExtensionMatcher(".jar"))), 170 jarWriter, 171 writer); 172 } 173 174 175 DataEntryReader reader = new DataEntryCopier(writer); 177 178 179 boolean inputIsJar = input.endsWith(".jar"); 180 boolean inputIsWar = input.endsWith(".war"); 181 boolean inputIsZip = input.endsWith(".zip"); 182 183 DataEntryReader jarReader = new JarReader(reader); 185 if (inputIsJar) 186 { 187 reader = jarReader; 189 } 190 else 191 { 192 reader = new FilteredDataEntryReader(new DataEntryNameFilter( 194 new ExtensionMatcher(".jar")), 195 jarReader, 196 reader); 197 198 DataEntryReader warReader = new JarReader(reader); 200 if (inputIsWar) 201 { 202 reader = warReader; 204 } 205 else 206 { 207 reader = new FilteredDataEntryReader(new DataEntryNameFilter( 209 new ExtensionMatcher(".war")), 210 warReader, 211 reader); 212 } 213 214 DataEntryReader zipReader = new JarReader(reader); 216 if (inputIsZip) 217 { 218 reader = zipReader; 220 } 221 else 222 { 223 reader = new FilteredDataEntryReader(new DataEntryNameFilter( 225 new ExtensionMatcher(".zip")), 226 zipReader, 227 reader); 228 } 229 } 230 231 DirectoryPump directoryReader = new DirectoryPump(new File(input)); 232 233 directoryReader.pumpDataEntries(reader); 234 235 writer.close(); 236 } 237 catch (Exception ex) 238 { 239 ex.printStackTrace(); 240 } 241 } 242 } 243 | Popular Tags |