Revision 32287 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/execution/Decompress.java
Decompress.java | ||
---|---|---|
1 | 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 |
*/ |
|
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 | 22 |
|
23 | 23 |
/* |
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2010 {Prodevelop} {Task} |
|
26 |
*/ |
|
27 |
|
|
24 |
* AUTHORS (In addition to CIT):
|
|
25 |
* 2010 {Prodevelop} {Task}
|
|
26 |
*/
|
|
27 |
|
|
28 | 28 |
package org.gvsig.installer.lib.impl.execution; |
29 | 29 |
|
30 |
import java.io.ByteArrayInputStream; |
|
31 |
import java.io.ByteArrayOutputStream; |
|
30 | 32 |
import java.io.File; |
31 | 33 |
import java.io.FileOutputStream; |
32 | 34 |
import java.io.IOException; |
33 | 35 |
import java.io.InputStream; |
36 |
import java.util.ArrayList; |
|
37 |
import java.util.Iterator; |
|
38 |
import java.util.List; |
|
39 |
import java.util.Properties; |
|
34 | 40 |
import java.util.zip.ZipEntry; |
35 | 41 |
import java.util.zip.ZipException; |
36 | 42 |
import java.util.zip.ZipInputStream; |
37 | 43 |
|
44 |
import org.gvsig.installer.lib.api.InstallerInfo; |
|
38 | 45 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException; |
46 |
import org.gvsig.installer.lib.impl.DefaultInstallerInfo; |
|
47 |
import org.gvsig.installer.lib.impl.creation.DefaultInstallerCreationService; |
|
48 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileException; |
|
49 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
|
39 | 50 |
import org.slf4j.Logger; |
40 | 51 |
import org.slf4j.LoggerFactory; |
41 | 52 |
|
... | ... | |
45 | 56 |
public class Decompress { |
46 | 57 |
private int BUFFER = 2048; |
47 | 58 |
private static final Logger logger = LoggerFactory.getLogger(Decompress.class); |
48 |
|
|
49 |
public void decompressPlugin(InputStream is, File file) throws InstallerExecutionServiceException { |
|
59 |
private List<InstallerInfo> installerInfos = null; |
|
60 |
private boolean decompress = false; |
|
61 |
private File outputDirectory = null; |
|
62 |
|
|
63 |
public void decompressPlugins(InputStream is, File outputDirectory) throws InstallerExecutionServiceException { |
|
64 |
decompress = true; |
|
65 |
this.outputDirectory = outputDirectory; |
|
66 |
decompressFolderOfPlugins(is); |
|
67 |
} |
|
68 |
|
|
69 |
public List<InstallerInfo> readInstallInfo(InputStream is) throws InstallerExecutionServiceException{ |
|
70 |
decompress = false; |
|
71 |
installerInfos = new ArrayList<InstallerInfo>(); |
|
72 |
decompressFolderOfPlugins(is); |
|
73 |
return installerInfos; |
|
74 |
} |
|
75 |
|
|
76 |
private void decompressFolderOfPlugins(InputStream is) throws InstallerExecutionServiceException { |
|
50 | 77 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
51 | 78 |
ZipEntry entry = null; |
52 |
|
|
79 |
|
|
53 | 80 |
try { |
54 | 81 |
while((entry = zipInputStream.getNextEntry()) != null) { |
55 | 82 |
logger.debug("Extracting Plugin: " + entry); |
56 | 83 |
|
57 |
decompressPlugin(new ZipInputStream(zipInputStream), file);
|
|
58 |
|
|
84 |
decompressPlugin(new ZipInputStream(zipInputStream)); |
|
85 |
|
|
59 | 86 |
zipInputStream.closeEntry(); |
60 | 87 |
} |
61 | 88 |
zipInputStream.close(); |
... | ... | |
64 | 91 |
} |
65 | 92 |
} |
66 | 93 |
|
67 |
private void decompressPlugin(ZipInputStream zipInputStream, File file) throws ZipException, IOException{
|
|
94 |
private void decompressPlugin(ZipInputStream zipInputStream) throws ZipException, IOException, InstallerInfoFileException{
|
|
68 | 95 |
ZipEntry entry = null; |
69 | 96 |
byte data[] = new byte[BUFFER]; |
70 | 97 |
int count; |
71 |
|
|
98 |
|
|
72 | 99 |
while((entry = zipInputStream.getNextEntry()) != null) { |
73 |
logger.debug("Extracting: " + entry.getName()); |
|
74 |
FileOutputStream fos = new FileOutputStream(entry.getName()); |
|
75 |
|
|
76 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
|
77 |
fos.write(data, 0, count); |
|
100 |
logger.debug("Extracting: " + entry.getName()); |
|
101 |
if (decompress){ |
|
102 |
FileOutputStream fos = new FileOutputStream(outputDirectory.getAbsolutePath() + File.separator + entry.getName()); |
|
103 |
|
|
104 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
|
105 |
fos.write(data, 0, count); |
|
106 |
} |
|
107 |
fos.flush(); |
|
108 |
fos.close(); |
|
109 |
}else{ |
|
110 |
if (entry.getName().endsWith(File.separator + DefaultInstallerCreationService.INSTALLINFO_FILE_NAME)){ |
|
111 |
installerInfos.add(readInstallInfo(zipInputStream)); |
|
112 |
} |
|
78 | 113 |
} |
79 |
fos.flush(); |
|
80 |
fos.close(); |
|
81 |
|
|
114 |
|
|
82 | 115 |
zipInputStream.closeEntry(); |
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
private InstallerInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{ |
|
120 |
int count; |
|
121 |
byte data[] = new byte[BUFFER]; |
|
122 |
|
|
123 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
124 |
|
|
125 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
|
126 |
out.write(data, 0, count); |
|
83 | 127 |
} |
128 |
out.flush(); |
|
129 |
|
|
130 |
InstallerInfo installerInfo = new DefaultInstallerInfo(); |
|
131 |
InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader(); |
|
132 |
installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray())); |
|
133 |
|
|
134 |
return installerInfo; |
|
84 | 135 |
} |
85 | 136 |
} |
86 | 137 |
|
Also available in: Unified diff