Skip to main content
summaryrefslogtreecommitdiffstats
blob: 15b7a743d92bb10b5d83c512bc51d4a8398cac54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.jarprocessor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.eclipse.internal.provisional.equinox.p2.jarprocessor.JarProcessor;
import org.eclipse.internal.provisional.equinox.p2.jarprocessor.JarProcessorExecutor;

/**
 * @author aniefer@ca.ibm.com
 *
 */
public class ZipProcessor {

	private JarProcessorExecutor executor = null;
	private JarProcessorExecutor.Options options = null;
	
	private String workingDirectory = null;
	private Properties properties = null;
	private Set packExclusions = null;
	private Set signExclusions = null;
	
	public void setExecutor(JarProcessorExecutor executor) {
		this.executor = executor;
	}
	
	public void setOptions(JarProcessorExecutor.Options options) {
		this.options = options;
	}
	
	public void setWorkingDirectory(String dir) {
		workingDirectory = dir;
	}

	public String getWorkingDirectory() {
		if (workingDirectory == null)
			workingDirectory = "."; //$NON-NLS-1$
		return workingDirectory;
	}

	private boolean repacking() {
		return options.repack || (options.pack && options.signCommand != null);
	}
	
	public void processZip(File zipFile) throws ZipException, IOException {
		if (options.verbose)
			System.out.println("Processing " + zipFile.getPath()); //$NON-NLS-1$
		ZipFile zip = new ZipFile(zipFile);
		initialize(zip);

		String extension = options.unpack ? "pack.gz" : ".jar"; //$NON-NLS-1$ //$NON-NLS-2$
		File tempDir = new File(getWorkingDirectory(), "temp_" + zipFile.getName()); //$NON-NLS-1$
		JarProcessor processor = new JarProcessor();
		processor.setVerbose(options.verbose);
		processor.setProcessAll(options.processAll);
		processor.setWorkingDirectory(tempDir.getCanonicalPath());
		if (options.unpack) {
			executor.addPackUnpackStep(processor, properties, options);
		}

		File outputFile = new File(getWorkingDirectory(), zipFile.getName() + ".temp"); //$NON-NLS-1$
		File parent = outputFile.getParentFile();
		if (!parent.exists())
			parent.mkdirs();
		ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outputFile));
		Enumeration entries = zip.entries();
		if (entries.hasMoreElements()) {
			for (ZipEntry entry = (ZipEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (ZipEntry) entries.nextElement() : null) {
				String name = entry.getName();

				InputStream entryStream = zip.getInputStream(entry);

				boolean pack = options.pack && !packExclusions.contains(name);
				boolean sign = options.signCommand != null && !signExclusions.contains(name);
				boolean repack = repacking() && !packExclusions.contains(name);

				File extractedFile = null;

				if (entry.getName().endsWith(extension) && (pack || sign || repack || options.unpack)) {
					extractedFile = new File(tempDir, name);
					parent = extractedFile.getParentFile();
					if (!parent.exists())
						parent.mkdirs();
					if (options.verbose)
						System.out.println("Extracting " + entry.getName()); //$NON-NLS-1$
					FileOutputStream extracted = new FileOutputStream(extractedFile);
					Utils.transferStreams(entryStream, extracted, true); // this will close the stream
					entryStream = null;

					boolean skip = Utils.shouldSkipJar(extractedFile, options.processAll, options.verbose);
					if (skip) {
						//skipping this file 
						entryStream = new FileInputStream(extractedFile);
						if (options.verbose)
							System.out.println(entry.getName() + " is not marked, skipping."); //$NON-NLS-1$
					} else {
						if (options.unpack) {
							File result = processor.processJar(extractedFile);
							name = name.substring(0, name.length() - extractedFile.getName().length()) + result.getName();
							extractedFile = result;
						} else {
							if (repack || sign) {
								processor.clearProcessSteps();
								if (repack)
									executor.addPackUnpackStep(processor, properties, options);
								if (sign)
									executor.addSignStep(processor, properties, options);
								extractedFile = processor.processJar(extractedFile);
							}
							if (pack) {
								processor.clearProcessSteps();
								executor.addPackStep(processor, properties, options);
								File modifiedFile = processor.processJar(extractedFile);
								if (modifiedFile.exists()) {
									try {
										String newName = name.substring(0, name.length() - extractedFile.getName().length()) + modifiedFile.getName();
										if (options.verbose) {
											System.out.println("Adding " + newName + " to " + outputFile.getPath()); //$NON-NLS-1$ //$NON-NLS-2$
											System.out.println();
										}
										ZipEntry zipEntry = new ZipEntry(newName);
										entryStream = new FileInputStream(modifiedFile);
										zipOut.putNextEntry(zipEntry);
										Utils.transferStreams(entryStream, zipOut, false); //we want to keep zipOut open
										entryStream.close();
										Utils.clear(modifiedFile);
									} catch (IOException e) {
										Utils.close(entryStream);
										if (options.verbose) {
											e.printStackTrace();
											System.out.println("Warning: Problem reading " + modifiedFile.getPath() + ".");
										}
									}
									entryStream = null;
								} else if (options.verbose) {
									System.out.println("Warning: " + modifiedFile.getPath() + " not found.");
								}
							}
						}
						if (extractedFile.exists()) {
							try {
								entryStream = new FileInputStream(extractedFile);
							} catch (IOException e) {
								if (options.verbose) {
									e.printStackTrace();
									System.out.println("Warning: Problem reading " + extractedFile.getPath() + ".");
								}
							}
						}

						if (options.verbose && entryStream != null) {
							System.out.println("Adding " + name + " to " + outputFile.getPath()); //$NON-NLS-1$ //$NON-NLS-2$
						}
					}
				}
				if (entryStream != null) {
					ZipEntry newEntry = new ZipEntry(name);
					try {
						zipOut.putNextEntry(newEntry);
						Utils.transferStreams(entryStream, zipOut, false);
						zipOut.closeEntry();
					} catch (ZipException e) {
						if(options.verbose) {
							System.out.println("Warning: " + name + " already exists in " + outputFile.getName() + ".  Skipping.");
						}
					}
					entryStream.close();
				}

				if (extractedFile != null)
					Utils.clear(extractedFile);
				
				if (options.verbose) {
					System.out.println();
					System.out.println("Processing " + zipFile.getPath()); //$NON-NLS-1$
				}
			}
		}
		zipOut.close();
		zip.close();

		File finalFile = new File(getWorkingDirectory(), zipFile.getName());
		if (finalFile.exists())
			finalFile.delete();
		outputFile.renameTo(finalFile);
		Utils.clear(tempDir);
	}

	private void initialize(ZipFile zip) {
		ZipEntry entry = zip.getEntry("pack.properties"); //$NON-NLS-1$
		properties = new Properties();
		if (entry != null) {
			InputStream stream = null;
			try {
				stream = zip.getInputStream(entry);
				properties.load(stream);
			} catch (IOException e) {
				if (options.verbose)
					e.printStackTrace();
			} finally {
				Utils.close(stream);
			}
		}

		packExclusions = Utils.getPackExclusions(properties);
		signExclusions = Utils.getSignExclusions(properties);

		if (executor == null)
			executor = new JarProcessorExecutor();
	}
}

Back to the top