Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2fc3d957da8d283aacd2dc98312ee91183976f51 (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
232
233
234
235
236
237
/*******************************************************************************
 * Copyright (c) 2007 compeople AG 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:
 * 	compeople AG (Stefan Liebig) - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.sar;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.eclipse.equinox.internal.p2.sar.SarConstants;
import org.eclipse.equinox.internal.p2.sar.SarEntry;
import org.eclipse.equinox.internal.p2.sar.SarInputStream;
import org.eclipse.equinox.internal.p2.sar.SarOutputStream;

/**
 * Helper class for converting Zips/Jars to Sars and vice versa.
 */
public class SarUtil {

	private static final int BUFFER_SIZE = 8 * 1024;
	private static final boolean DEBUG = SarConstants.DEBUG;

	/**
	 * 
	 */
	private SarUtil() {
		// utility class
	}

	/**
	 * Normalize the given zip/jar.
	 * 
	 * @param zipSource
	 * @param zipTarget
	 * @throws IOException
	 */
	public static void normalize(File zipSource, File zipTarget)
			throws IOException {
		File tempSar = File.createTempFile("temp", ".sar");
		try {
			zipToSar(zipSource, tempSar);
			sarToZip(tempSar, zipTarget);
		} finally {
			tempSar.delete();
		}
	}

	/**
	 * Normalize the given zip/jar.
	 * 
	 * @param zipSource
	 * @param zipTarget
	 * @throws IOException
	 */
	public static void normalize(InputStream zipSource, OutputStream zipTarget)
			throws IOException {
		DirectByteArrayOutputStream tempSar = new DirectByteArrayOutputStream();
		zipToSar(zipSource, tempSar);
		sarToZip(tempSar.getInputStream(), zipTarget);
	}

	/**
	 * @param zipFile
	 * @return
	 * @throws IOException
	 */
	public static void zipToSar(File zipFile, File sarFile) throws IOException {
		InputStream zipInputStream = new BufferedInputStream(
				new FileInputStream(zipFile));
		OutputStream sarOutputStream = new BufferedOutputStream(
				new FileOutputStream(sarFile));
		SarUtil.zipToSar(zipInputStream, sarOutputStream);
	}

	/**
	 * @param zippedInputStream
	 * @param saredOutputStream
	 * @throws IOException
	 */
	public static void zipToSar(InputStream zippedInputStream,
			OutputStream saredOutputStream) throws IOException {
		zipToSar(zippedInputStream, true, saredOutputStream, true);
	}

	/**
	 * @param zippedInputStream
	 * @param closeIn
	 * @param saredOutputStream
	 * @param closeOut
	 * @throws IOException
	 */
	public static void zipToSar(InputStream zippedInputStream, boolean closeIn,
			OutputStream saredOutputStream, boolean closeOut)
			throws IOException {
		zipToSarNoClose(zippedInputStream, saredOutputStream);

		if (closeIn)
			zippedInputStream.close();
		if (closeOut)
			saredOutputStream.close();
	}

	/**
	 * @param sarFile
	 * @param zipFile
	 * @throws IOException
	 */
	public static void sarToZip(File sarFile, File zipFile) throws IOException {
		InputStream saredInputStream = new BufferedInputStream(
				new FileInputStream(sarFile));
		OutputStream zippedOutputStream = new BufferedOutputStream(
				new FileOutputStream(zipFile));

		sarToZip(saredInputStream, zippedOutputStream);
	}

	/**
	 * @param saredInputStream
	 * @param zippedOutputStream
	 * @param level
	 * @throws IOException
	 */
	public static void sarToZip(InputStream saredInputStream,
			OutputStream zippedOutputStream) throws IOException {
		sarToZip(saredInputStream, true, zippedOutputStream, true);
	}

	/**
	 * @param saredInputStream
	 * @param closeIn
	 * @param zippedOutputStream
	 * @param closeOut
	 * @throws IOException
	 */
	public static void sarToZip(InputStream saredInputStream, boolean closeIn,
			OutputStream zippedOutputStream, boolean closeOut)
			throws IOException {
		sarToZipNoClose(saredInputStream, zippedOutputStream);

		if (closeIn)
			saredInputStream.close();
		if (closeOut)
			zippedOutputStream.close();
	}

	/**
	 * @param zippedInputStream
	 * @param saredOutputStream
	 * @throws IOException
	 */
	private static void zipToSarNoClose(InputStream zippedInputStream,
			OutputStream saredOutputStream) throws IOException {

		ZipInputStream zipInputStream = new ZipInputStream(zippedInputStream);
		SarOutputStream sarOutputStream = new SarOutputStream(saredOutputStream);

		ZipEntry zipEntry;
		byte[] buf = new byte[BUFFER_SIZE];
		while ((zipEntry = zipInputStream.getNextEntry()) != null) {
			boolean isZip = isZip(zipEntry);
			SarEntry sarEntry = new SarEntry(zipEntry, isZip);
			sarOutputStream.putNextEntry(sarEntry);
			if (isZip) {
				zipToSarNoClose(zipInputStream, sarOutputStream);
			} else {
				int read;
				while ((read = zipInputStream.read(buf)) != -1) {
					if (DEBUG) {
						System.out.println("Content: "
								+ new String(buf, 0, read));
					}
					sarOutputStream.write(buf, 0, read);
				}
			}
			zipInputStream.closeEntry();
			sarOutputStream.closeEntry();
		}
		sarOutputStream.finish();
	}

	/**
	 * @param saredInputStream
	 * @param zippedOutputStream
	 * @param level
	 * @throws IOException
	 */
	private static void sarToZipNoClose(InputStream saredInputStream,
			OutputStream zippedOutputStream) throws IOException {

		SarInputStream sarInputStream = new SarInputStream(saredInputStream);
		ZipOutputStream zipOutputStream = new ZipOutputStream(
				zippedOutputStream);

		SarEntry sarEntry;
		byte[] buf = new byte[BUFFER_SIZE];
		while ((sarEntry = sarInputStream.getNextEntry()) != null) {
			ZipEntry zipEntry = new ZipEntry(sarEntry);
			zipOutputStream.putNextEntry(zipEntry);
			if (sarEntry.isZip()) {
				sarToZipNoClose(sarInputStream, zipOutputStream);
			} else {
				int read;
				while ((read = sarInputStream.read(buf)) != -1) {
					if (DEBUG) {
						System.out.println("Content: "
								+ new String(buf, 0, read));
					}
					zipOutputStream.write(buf, 0, read);
				}
			}
			sarInputStream.closeEntry();
			zipOutputStream.closeEntry();
		}

		zipOutputStream.finish();
	}

	private static boolean isZip(ZipEntry zipEntry) {
		String name = zipEntry.getName().toLowerCase();
		return name.endsWith(".zip") || name.endsWith(".jar");
	}
}

Back to the top