root / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / Decompress.java @ 36117
History | View | Annotate | Download (14.3 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2010 {Prodevelop} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.installer.lib.impl.utils; |
29 |
|
30 |
import java.io.ByteArrayInputStream; |
31 |
import java.io.ByteArrayOutputStream; |
32 |
import java.io.File; |
33 |
import java.io.FileOutputStream; |
34 |
import java.io.IOException; |
35 |
import java.io.InputStream; |
36 |
import java.util.ArrayList; |
37 |
import java.util.List; |
38 |
import java.util.Map; |
39 |
import java.util.zip.ZipEntry; |
40 |
import java.util.zip.ZipException; |
41 |
import java.util.zip.ZipInputStream; |
42 |
|
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
import org.gvsig.installer.lib.api.PackageInfo; |
47 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
48 |
import org.gvsig.installer.lib.impl.DefaultPackageInfo; |
49 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
50 |
import org.gvsig.installer.lib.spi.InstallerInfoFileException; |
51 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
52 |
import org.gvsig.tools.ToolsLocator; |
53 |
import org.gvsig.tools.service.ServiceException; |
54 |
import org.gvsig.tools.task.SimpleTaskStatus; |
55 |
import org.gvsig.tools.task.TaskStatusManager; |
56 |
|
57 |
/**
|
58 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
59 |
*/
|
60 |
public class Decompress { |
61 |
|
62 |
private static final int OPTION_DECOMPRESS = 1; |
63 |
private static final int OPTION_READ_INSTALLINFO = 2; |
64 |
private static final int OPTION_INSTALL = 3; |
65 |
|
66 |
private int option = 0; |
67 |
|
68 |
private int BUFFER = 2048; |
69 |
|
70 |
private static final Logger logger = |
71 |
LoggerFactory.getLogger(Decompress.class); |
72 |
|
73 |
private List<PackageInfo> readedIinstallerInfos = null; |
74 |
private File outputDirectory = null; |
75 |
private List<PackageInfo> selectedInstallerInfos = null; |
76 |
private Map<PackageInfo, String> zipEntriesMap = null; |
77 |
private List<String> defaultSelectedPackets = null; |
78 |
|
79 |
public Decompress() {
|
80 |
|
81 |
} |
82 |
|
83 |
public void decompressPlugins(InputStream is, File outputDirectory) |
84 |
throws InstallPackageServiceException {
|
85 |
option = OPTION_DECOMPRESS; |
86 |
this.outputDirectory = outputDirectory;
|
87 |
|
88 |
decompressFolderOfPlugins(is); |
89 |
} |
90 |
|
91 |
public void readPackageSetInstallInfos(InputStream is, |
92 |
List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap) |
93 |
throws InstallPackageServiceException {
|
94 |
option = OPTION_READ_INSTALLINFO; |
95 |
this.readedIinstallerInfos = installerInfos;
|
96 |
this.zipEntriesMap = zipEntriesMap;
|
97 |
decompressFolderOfPlugins(is); |
98 |
} |
99 |
|
100 |
public void readPackageInstallInfo(InputStream is, |
101 |
List<PackageInfo> installerInfos,
|
102 |
Map<PackageInfo, String> zipEntriesMap, String name) |
103 |
throws InstallPackageServiceException {
|
104 |
option = OPTION_READ_INSTALLINFO; |
105 |
this.readedIinstallerInfos = installerInfos;
|
106 |
this.zipEntriesMap = zipEntriesMap;
|
107 |
decompressFolderOfPluginFromPackage(is, name); |
108 |
} |
109 |
|
110 |
public class InstallerPluginReadException extends |
111 |
InstallPackageServiceException { |
112 |
|
113 |
private static final long serialVersionUID = -6065359474790792680L; |
114 |
|
115 |
private static final String message = "Error reading the plugin"; |
116 |
|
117 |
private static final String KEY = "_Error_reading_the_plugin"; |
118 |
|
119 |
public InstallerPluginReadException(ServiceException e) {
|
120 |
super(message, e, KEY, serialVersionUID);
|
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
public void decompressPlugin(InputStream is, File outputDirectory) |
126 |
throws InstallPackageServiceException {
|
127 |
try {
|
128 |
option = OPTION_DECOMPRESS; |
129 |
this.outputDirectory = outputDirectory;
|
130 |
decompressPlugin(is); |
131 |
} catch (Exception e) { |
132 |
throw new InstallPackageServiceException(e); |
133 |
} |
134 |
} |
135 |
|
136 |
public class InstallerPluginReadErrorException extends |
137 |
InstallPackageServiceException { |
138 |
|
139 |
private static final long serialVersionUID = 4505298394252120334L; |
140 |
|
141 |
private static final String message = |
142 |
"Error reading the plugin '%(name)s'";
|
143 |
|
144 |
private static final String KEY = "_Error_reading_the_plugin"; |
145 |
|
146 |
public InstallerPluginReadErrorException(Exception e, String packageName) { |
147 |
super(message, e, KEY, serialVersionUID);
|
148 |
setValue("name", packageName);
|
149 |
} |
150 |
|
151 |
} |
152 |
|
153 |
public InputStream searchPlugin(InputStream is, String zipEntry) |
154 |
throws InstallPackageServiceException {
|
155 |
ZipEntry entry = null; |
156 |
String name = ""; |
157 |
|
158 |
try {
|
159 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
160 |
|
161 |
while ((entry = zipInputStream.getNextEntry()) != null) { |
162 |
name = entry.getName(); |
163 |
if (entry.getName().equals(zipEntry)) {
|
164 |
return zipInputStream;
|
165 |
} |
166 |
zipInputStream.closeEntry(); |
167 |
} |
168 |
zipInputStream.closeEntry(); |
169 |
|
170 |
zipInputStream.close(); |
171 |
} catch (Exception e) { |
172 |
throw new InstallerPluginReadErrorException(e, name); |
173 |
} |
174 |
return null; |
175 |
} |
176 |
|
177 |
public void installFromStream(InputStream is, PackageInfo installerInfo) |
178 |
throws InstallPackageServiceException {
|
179 |
option = OPTION_INSTALL; |
180 |
this.selectedInstallerInfos = new ArrayList<PackageInfo>(); |
181 |
this.selectedInstallerInfos.add(installerInfo);
|
182 |
decompressFolderOfPlugins(is); |
183 |
} |
184 |
|
185 |
public void installFromStream(InputStream is, |
186 |
List<PackageInfo> installerInfos) throws InstallPackageServiceException { |
187 |
option = OPTION_INSTALL; |
188 |
this.selectedInstallerInfos = installerInfos;
|
189 |
decompressFolderOfPlugins(is); |
190 |
} |
191 |
|
192 |
private void decompressFolderOfPlugins(InputStream is) |
193 |
throws InstallPackageServiceException {
|
194 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
195 |
ZipEntry entry = null; |
196 |
|
197 |
String name = ""; |
198 |
|
199 |
try {
|
200 |
while ((entry = zipInputStream.getNextEntry()) != null) { |
201 |
name = entry.getName(); |
202 |
if (entry.getName().equals("defaultPackages") |
203 |
|| entry.getName().equals("defaultSelection")) {
|
204 |
int count;
|
205 |
byte data[] = new byte[BUFFER]; |
206 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
207 |
|
208 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
209 |
out.write(data, 0, count);
|
210 |
} |
211 |
|
212 |
String str = out.toString();
|
213 |
String lineas[] = str.split("\\n"); |
214 |
List<String> defaultPackagesList = new ArrayList<String>(); |
215 |
|
216 |
for (int i = 0; i < lineas.length; i++) { |
217 |
defaultPackagesList.add(lineas[i]); |
218 |
} |
219 |
|
220 |
defaultSelectedPackets = defaultPackagesList; |
221 |
out.flush(); |
222 |
} else {
|
223 |
logger.debug("Extracting all Plugins, plugin: " + entry);
|
224 |
if (option == OPTION_INSTALL) {
|
225 |
|
226 |
} else {
|
227 |
if (option == OPTION_DECOMPRESS) {
|
228 |
decompressPlugin(zipInputStream); |
229 |
} else
|
230 |
if (option == OPTION_READ_INSTALLINFO) {
|
231 |
readPlugin(zipInputStream, entry.getName()); |
232 |
} |
233 |
} |
234 |
} |
235 |
zipInputStream.closeEntry(); |
236 |
} |
237 |
zipInputStream.close(); |
238 |
|
239 |
} catch (Exception e) { |
240 |
throw new InstallerPluginReadErrorException(e, name); |
241 |
} |
242 |
} |
243 |
|
244 |
private void decompressFolderOfPluginFromPackage(InputStream is, String name) |
245 |
throws InstallPackageServiceException {
|
246 |
|
247 |
try {
|
248 |
if (option == OPTION_INSTALL) {
|
249 |
|
250 |
} else {
|
251 |
if (option == OPTION_DECOMPRESS) {
|
252 |
decompressPlugin(is); |
253 |
} else {
|
254 |
if (option == OPTION_READ_INSTALLINFO) {
|
255 |
readPlugin(is, name); |
256 |
} |
257 |
} |
258 |
} |
259 |
|
260 |
} catch (Exception e) { |
261 |
throw new InstallerPluginReadErrorException(e, name); |
262 |
} |
263 |
} |
264 |
|
265 |
private void readPlugin(InputStream is, String zipEntryName) |
266 |
throws ZipException, IOException, InstallerInfoFileException { |
267 |
ZipEntry entry = null; |
268 |
int installerInfoNumber = zipEntriesMap.size();
|
269 |
String packageInfoName = getPackageInfoFileName();
|
270 |
String unixPackageInfoPath = "/".concat(packageInfoName); |
271 |
String windowsPackageInfoPath = "\\".concat(packageInfoName); |
272 |
ZipInputStream zis = new ZipInputStream(is); |
273 |
while ((entry = zis.getNextEntry()) != null) { |
274 |
logger.debug("Extracting: " + entry.getName());
|
275 |
|
276 |
String name = entry.getName();
|
277 |
// Just in case, but we are going to create them always using
|
278 |
// the unix file separator
|
279 |
if (name.endsWith(unixPackageInfoPath)
|
280 |
|| name.endsWith(windowsPackageInfoPath)) { |
281 |
PackageInfo installerInfo = readInstallInfo(zis); |
282 |
zipEntriesMap.put(installerInfo, zipEntryName); |
283 |
readedIinstallerInfos.add(installerInfo); |
284 |
} |
285 |
zis.closeEntry(); |
286 |
} |
287 |
// Don't close the stream as if it is a zip file contained
|
288 |
// into another zip file, closing of the child Zip?nputStream
|
289 |
// will close also the parent's.
|
290 |
// zis.close();
|
291 |
|
292 |
if (installerInfoNumber == zipEntriesMap.size()) {
|
293 |
PackageInfo installerInfo = new DefaultPackageInfo();
|
294 |
installerInfo.setCode(zipEntryName); |
295 |
installerInfo.setName(zipEntryName); |
296 |
zipEntriesMap.put(installerInfo, zipEntryName); |
297 |
readedIinstallerInfos.add(installerInfo); |
298 |
} |
299 |
} |
300 |
|
301 |
private void decompressPlugin(InputStream inputStream) throws ZipException, |
302 |
IOException, InstallerInfoFileException {
|
303 |
ZipInputStream zis = null; |
304 |
ZipEntry entry = null; |
305 |
byte data[] = new byte[BUFFER]; |
306 |
int count = 0; |
307 |
|
308 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
309 |
SimpleTaskStatus taskStatus = |
310 |
manager.creteDefaultSimpleTaskStatus("Uncompressing...");
|
311 |
manager.add(taskStatus); |
312 |
long readed = 0; |
313 |
|
314 |
// // First read all the contents size
|
315 |
// zis = new ZipInputStream(inputStream);
|
316 |
// while ((entry = zis.getNextEntry()) != null) {
|
317 |
// count += entry.getSize();
|
318 |
// }
|
319 |
// // zis.close();
|
320 |
// taskStatus.setRangeOfValues(0, count);
|
321 |
|
322 |
// Return the stream to the initial position
|
323 |
zis = new ZipInputStream(inputStream); |
324 |
while ((entry = zis.getNextEntry()) != null) { |
325 |
String entryName = entry.getName();
|
326 |
taskStatus.message(entryName); |
327 |
|
328 |
if (File.separatorChar != '/') { |
329 |
entryName = entryName.replace('/', File.separatorChar); |
330 |
} |
331 |
logger.debug("Extracting: " + entryName);
|
332 |
|
333 |
File file =
|
334 |
new File(outputDirectory.getAbsolutePath() + File.separator |
335 |
+ entryName); |
336 |
if (file.exists()) {
|
337 |
delete(file); |
338 |
} |
339 |
if (entry.isDirectory()) {
|
340 |
file.mkdirs(); |
341 |
} else {
|
342 |
createParentFolder(file); |
343 |
FileOutputStream fos = new FileOutputStream(file); |
344 |
while ((count = zis.read(data, 0, BUFFER)) != -1) { |
345 |
fos.write(data, 0, count);
|
346 |
readed += count; |
347 |
taskStatus.setCurValue(readed); |
348 |
} |
349 |
fos.flush(); |
350 |
fos.close(); |
351 |
} |
352 |
} |
353 |
zis.close(); |
354 |
taskStatus.remove(); |
355 |
} |
356 |
|
357 |
private void createParentFolder(File file) { |
358 |
File parentFile = file.getParentFile();
|
359 |
if (!parentFile.exists()) {
|
360 |
parentFile.mkdirs(); |
361 |
} |
362 |
} |
363 |
|
364 |
public boolean delete(File dir) { |
365 |
if (dir.isDirectory()) {
|
366 |
String[] children = dir.list(); |
367 |
for (int i = 0; i < children.length; i++) { |
368 |
boolean success = delete(new File(dir, children[i])); |
369 |
if (!success) {
|
370 |
return false; |
371 |
} |
372 |
} |
373 |
} |
374 |
return dir.delete();
|
375 |
} |
376 |
|
377 |
public PackageInfo readInstallerInfo(InputStream is) |
378 |
throws InstallerInfoFileException {
|
379 |
try {
|
380 |
return readInstallInfo(new ZipInputStream(is)); |
381 |
} catch (IOException e) { |
382 |
throw new InstallerInfoFileException("error_reading_installerinfo", |
383 |
e); |
384 |
} |
385 |
} |
386 |
|
387 |
private PackageInfo readInstallInfo(ZipInputStream zipInputStream) |
388 |
throws IOException, InstallerInfoFileException { |
389 |
int count;
|
390 |
byte data[] = new byte[BUFFER]; |
391 |
|
392 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
393 |
|
394 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
395 |
out.write(data, 0, count);
|
396 |
} |
397 |
out.flush(); |
398 |
|
399 |
DefaultPackageInfo installerInfo = new DefaultPackageInfo();
|
400 |
InstallerInfoFileReader installerInfoFileReader = |
401 |
new InstallerInfoFileReader();
|
402 |
installerInfoFileReader.read(installerInfo, new ByteArrayInputStream( |
403 |
out.toByteArray())); |
404 |
|
405 |
return installerInfo;
|
406 |
} |
407 |
|
408 |
private String getPackageInfoFileName() { |
409 |
return InstallerProviderLocator.getProviderManager()
|
410 |
.getPackageInfoFileName(); |
411 |
} |
412 |
|
413 |
public List<String> getDefaultSelectedPackages() { |
414 |
return defaultSelectedPackets;
|
415 |
} |
416 |
} |