1 11 package org.eclipse.update.internal.jarprocessor; 12 13 import java.io.*; 14 import java.util.*; 15 import java.util.zip.*; 16 17 21 public class ZipProcessor { 22 23 private IProcessStep signStep = null; 24 private IProcessStep packStep = null; 25 private IProcessStep packUnpackStep = null; 26 private IProcessStep unpackStep = null; 27 28 private String workingDirectory = null; 29 private Properties properties = null; 30 private Set packExclusions = null; 31 private Set signExclusions = null; 32 private String command = null; 33 private boolean packing = false; 34 private boolean signing = false; 35 private boolean repacking = false; 36 private boolean unpacking = false; 37 private boolean verbose = false; 38 private boolean processAll = false; 39 40 public void setWorkingDirectory(String dir) { 41 workingDirectory = dir; 42 } 43 44 public String getWorkingDirectory() { 45 if (workingDirectory == null) 46 workingDirectory = "."; return workingDirectory; 48 } 49 50 public void setSignCommand(String command) { 51 this.command = command; 52 this.signing = (command != null); 53 } 54 55 public void setPack(boolean pack) { 56 this.packing = pack; 57 } 58 59 public void setRepack(boolean repack) { 60 this.repacking = repack; 61 } 62 63 public void setUnpack(boolean unpack) { 64 this.unpacking = unpack; 65 } 66 67 public void setVerbose(boolean verbose) { 68 this.verbose = verbose; 69 } 70 71 public void setProcessAll(boolean all) { 72 this.processAll = all; 73 } 74 75 public void processZip(File zipFile) throws ZipException, IOException { 76 if (verbose) 77 System.out.println("Processing " + zipFile.getPath()); ZipFile zip = new ZipFile(zipFile); 79 initialize(zip); 80 81 String extension = unpacking ? "pack.gz" : ".jar"; File tempDir = new File(getWorkingDirectory(), "temp_" + zipFile.getName()); JarProcessor processor = new JarProcessor(); 84 processor.setVerbose(verbose); 85 processor.setProcessAll(processAll); 86 processor.setWorkingDirectory(tempDir.getCanonicalPath()); 87 if (unpacking) { 88 processor.addProcessStep(unpackStep); 89 } 90 91 File outputFile = new File(getWorkingDirectory(), zipFile.getName() + ".temp"); File parent = outputFile.getParentFile(); 93 if (!parent.exists()) 94 parent.mkdirs(); 95 ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outputFile)); 96 Enumeration entries = zip.entries(); 97 if (entries.hasMoreElements()) { 98 for (ZipEntry entry = (ZipEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (ZipEntry) entries.nextElement() : null) { 99 String name = entry.getName(); 100 101 InputStream entryStream = zip.getInputStream(entry); 102 103 boolean pack = packing && !packExclusions.contains(name); 104 boolean sign = signing && !signExclusions.contains(name); 105 boolean repack = repacking && !packExclusions.contains(name); 106 107 File extractedFile = null; 108 109 if (entry.getName().endsWith(extension) && (pack || sign || repack || unpacking)) { 110 extractedFile = new File(tempDir, name); 111 parent = extractedFile.getParentFile(); 112 if (!parent.exists()) 113 parent.mkdirs(); 114 if (verbose) 115 System.out.println("Extracting " + entry.getName()); FileOutputStream extracted = new FileOutputStream(extractedFile); 117 Utils.transferStreams(entryStream, extracted, true); entryStream = null; 119 120 boolean skip = Utils.shouldSkipJar(extractedFile, processAll, verbose); 121 if (skip) { 122 entryStream = new FileInputStream(extractedFile); 124 if (verbose) 125 System.out.println(entry.getName() + " is not marked, skipping."); } else { 127 if (unpacking) { 128 File result = processor.processJar(extractedFile); 129 name = name.substring(0, name.length() - extractedFile.getName().length()) + result.getName(); 130 extractedFile = result; 131 } else { 132 if (repack || sign) { 133 processor.clearProcessSteps(); 134 if (repack) 135 processor.addProcessStep(packUnpackStep); 136 if (sign) 137 processor.addProcessStep(signStep); 138 extractedFile = processor.processJar(extractedFile); 139 } 140 if (pack) { 141 processor.clearProcessSteps(); 142 processor.addProcessStep(packStep); 143 File modifiedFile = processor.processJar(extractedFile); 144 if (modifiedFile.exists()) { 145 try { 146 String newName = name.substring(0, name.length() - extractedFile.getName().length()) + modifiedFile.getName(); 147 if (verbose) { 148 System.out.println("Adding " + newName + " to " + outputFile.getPath()); System.out.println(); 150 } 151 ZipEntry zipEntry = new ZipEntry(newName); 152 entryStream = new FileInputStream(modifiedFile); 153 zipOut.putNextEntry(zipEntry); 154 Utils.transferStreams(entryStream, zipOut, false); entryStream.close(); 156 Utils.clear(modifiedFile); 157 } catch (IOException e) { 158 Utils.close(entryStream); 159 if (verbose) { 160 e.printStackTrace(); 161 System.out.println("Warning: Problem reading " + modifiedFile.getPath() + "."); 162 } 163 } 164 entryStream = null; 165 } else if (verbose) { 166 System.out.println("Warning: " + modifiedFile.getPath() + " not found."); 167 } 168 } 169 } 170 if (extractedFile.exists()) { 171 try { 172 entryStream = new FileInputStream(extractedFile); 173 } catch (IOException e) { 174 if (verbose) { 175 e.printStackTrace(); 176 System.out.println("Warning: Problem reading " + extractedFile.getPath() + "."); 177 } 178 } 179 } 180 181 if (verbose && entryStream != null) { 182 System.out.println("Adding " + name + " to " + outputFile.getPath()); } 184 } 185 } 186 if (entryStream != null) { 187 ZipEntry newEntry = new ZipEntry(name); 188 try { 189 zipOut.putNextEntry(newEntry); 190 Utils.transferStreams(entryStream, zipOut, false); 191 zipOut.closeEntry(); 192 } catch (ZipException e) { 193 if(verbose) { 194 System.out.println("Warning: " + name + " already exists in " + outputFile.getName() + ". Skipping."); 195 } 196 } 197 entryStream.close(); 198 } 199 200 if (extractedFile != null) 201 Utils.clear(extractedFile); 202 203 if (verbose) { 204 System.out.println(); 205 System.out.println("Processing " + zipFile.getPath()); } 207 } 208 } 209 zipOut.close(); 210 zip.close(); 211 212 File finalFile = new File(getWorkingDirectory(), zipFile.getName()); 213 if (finalFile.exists()) 214 finalFile.delete(); 215 outputFile.renameTo(finalFile); 216 Utils.clear(tempDir); 217 } 218 219 private void initialize(ZipFile zip) { 220 ZipEntry entry = zip.getEntry("pack.properties"); properties = new Properties(); 222 if (entry != null) { 223 InputStream stream = null; 224 try { 225 stream = zip.getInputStream(entry); 226 properties.load(stream); 227 } catch (IOException e) { 228 if (verbose) 229 e.printStackTrace(); 230 } finally { 231 Utils.close(stream); 232 } 233 } 234 235 packExclusions = Utils.getPackExclusions(properties); 236 signExclusions = Utils.getSignExclusions(properties); 237 238 packUnpackStep = new PackUnpackStep(properties, verbose); 239 packStep = new PackStep(properties, verbose); 240 signStep = new SignCommandStep(properties, command, verbose); 241 unpackStep = new UnpackStep(properties, verbose); 242 } 243 } 244 | Popular Tags |