KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > jarprocessor > ZipProcessor


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.jarprocessor;
12
13 import java.io.*;
14 import java.util.*;
15 import java.util.zip.*;
16
17 /**
18  * @author aniefer@ca.ibm.com
19  *
20  */

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 JavaDoc workingDirectory = null;
29     private Properties properties = null;
30     private Set packExclusions = null;
31     private Set signExclusions = null;
32     private String JavaDoc 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 JavaDoc dir) {
41         workingDirectory = dir;
42     }
43
44     public String JavaDoc getWorkingDirectory() {
45         if (workingDirectory == null)
46             workingDirectory = "."; //$NON-NLS-1$
47
return workingDirectory;
48     }
49
50     public void setSignCommand(String JavaDoc 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()); //$NON-NLS-1$
78
ZipFile zip = new ZipFile(zipFile);
79         initialize(zip);
80
81         String JavaDoc extension = unpacking ? "pack.gz" : ".jar"; //$NON-NLS-1$ //$NON-NLS-2$
82
File tempDir = new File(getWorkingDirectory(), "temp_" + zipFile.getName()); //$NON-NLS-1$
83
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"); //$NON-NLS-1$
92
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 JavaDoc 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()); //$NON-NLS-1$
116
FileOutputStream extracted = new FileOutputStream(extractedFile);
117                     Utils.transferStreams(entryStream, extracted, true); // this will close the stream
118
entryStream = null;
119
120                     boolean skip = Utils.shouldSkipJar(extractedFile, processAll, verbose);
121                     if (skip) {
122                         //skipping this file
123
entryStream = new FileInputStream(extractedFile);
124                         if (verbose)
125                             System.out.println(entry.getName() + " is not marked, skipping."); //$NON-NLS-1$
126
} 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 JavaDoc newName = name.substring(0, name.length() - extractedFile.getName().length()) + modifiedFile.getName();
147                                         if (verbose) {
148                                             System.out.println("Adding " + newName + " to " + outputFile.getPath()); //$NON-NLS-1$ //$NON-NLS-2$
149
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); //we want to keep zipOut open
155
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()); //$NON-NLS-1$ //$NON-NLS-2$
183
}
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()); //$NON-NLS-1$
206
}
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"); //$NON-NLS-1$
221
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