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