svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / Decompress.java @ 34107
History | View | Annotate | Download (10.6 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 |
|
52 |
/**
|
53 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
54 |
*/
|
55 |
public class Decompress { |
56 |
|
57 |
private static final int OPTION_DECOMPRESS = 1; |
58 |
private static final int OPTION_READ_INSTALLINFO = 2; |
59 |
private static final int OPTION_INSTALL = 3; |
60 |
|
61 |
private int option = 0; |
62 |
|
63 |
private int BUFFER = 2048; |
64 |
|
65 |
private static final Logger logger = LoggerFactory |
66 |
.getLogger(Decompress.class); |
67 |
|
68 |
private List<PackageInfo> readedIinstallerInfos = null; |
69 |
private File outputDirectory = null; |
70 |
private List<PackageInfo> selectedInstallerInfos = null; |
71 |
private Map<PackageInfo, String> zipEntriesMap = null; |
72 |
|
73 |
public Decompress() {
|
74 |
|
75 |
} |
76 |
|
77 |
public void decompressPlugins(InputStream is, File outputDirectory) |
78 |
throws InstallPackageServiceException {
|
79 |
option = OPTION_DECOMPRESS; |
80 |
this.outputDirectory = outputDirectory;
|
81 |
|
82 |
decompressFolderOfPlugins(is); |
83 |
} |
84 |
|
85 |
public void readPackageSetInstallInfos(InputStream is, |
86 |
List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap) |
87 |
throws InstallPackageServiceException {
|
88 |
option = OPTION_READ_INSTALLINFO; |
89 |
this.readedIinstallerInfos = installerInfos;
|
90 |
this.zipEntriesMap = zipEntriesMap;
|
91 |
decompressFolderOfPlugins(is); |
92 |
} |
93 |
|
94 |
public void readPackageInstallInfo(InputStream is, |
95 |
List<PackageInfo> installerInfos,
|
96 |
Map<PackageInfo, String> zipEntriesMap, String name) |
97 |
throws InstallPackageServiceException {
|
98 |
option = OPTION_READ_INSTALLINFO; |
99 |
this.readedIinstallerInfos = installerInfos;
|
100 |
this.zipEntriesMap = zipEntriesMap;
|
101 |
decompressFolderOfPluginFromPackage(is, name); |
102 |
} |
103 |
|
104 |
public void decompressPlugin(InputStream is, File outputDirectory) |
105 |
throws InstallPackageServiceException {
|
106 |
try {
|
107 |
option = OPTION_DECOMPRESS; |
108 |
this.outputDirectory = outputDirectory;
|
109 |
decompressPlugin(new ZipInputStream(is)); |
110 |
} catch (Exception e) { |
111 |
throw new InstallPackageServiceException( |
112 |
"Error reading the plugin", e);
|
113 |
} |
114 |
} |
115 |
|
116 |
public InputStream searchPlugin(InputStream is, String zipEntry) |
117 |
throws InstallPackageServiceException {
|
118 |
ZipEntry entry = null; |
119 |
|
120 |
try {
|
121 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
122 |
|
123 |
while ((entry = zipInputStream.getNextEntry()) != null) { |
124 |
if (entry.getName().equals(zipEntry)) {
|
125 |
return zipInputStream;
|
126 |
} |
127 |
zipInputStream.closeEntry(); |
128 |
} |
129 |
zipInputStream.closeEntry(); |
130 |
|
131 |
zipInputStream.close(); |
132 |
} catch (Exception e) { |
133 |
throw new InstallPackageServiceException( |
134 |
"Error reading the plugin", e);
|
135 |
} |
136 |
return null; |
137 |
} |
138 |
|
139 |
public void installFromStream(InputStream is, PackageInfo installerInfo) |
140 |
throws InstallPackageServiceException {
|
141 |
option = OPTION_INSTALL; |
142 |
this.selectedInstallerInfos = new ArrayList<PackageInfo>(); |
143 |
this.selectedInstallerInfos.add(installerInfo);
|
144 |
decompressFolderOfPlugins(is); |
145 |
} |
146 |
|
147 |
public void installFromStream(InputStream is, |
148 |
List<PackageInfo> installerInfos) throws InstallPackageServiceException { |
149 |
option = OPTION_INSTALL; |
150 |
this.selectedInstallerInfos = installerInfos;
|
151 |
decompressFolderOfPlugins(is); |
152 |
} |
153 |
|
154 |
private void decompressFolderOfPlugins(InputStream is) |
155 |
throws InstallPackageServiceException {
|
156 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
157 |
ZipEntry entry = null; |
158 |
|
159 |
try {
|
160 |
|
161 |
while ((entry = zipInputStream.getNextEntry()) != null) { |
162 |
logger.debug("Extracting all Plugins, plugin: " + entry);
|
163 |
if (option == OPTION_INSTALL) {
|
164 |
|
165 |
} else
|
166 |
if (option == OPTION_DECOMPRESS) {
|
167 |
decompressPlugin(new ZipInputStream(zipInputStream)); |
168 |
} else
|
169 |
if (option == OPTION_READ_INSTALLINFO) {
|
170 |
readPlugin(new ZipInputStream(zipInputStream), |
171 |
entry.getName()); |
172 |
} |
173 |
zipInputStream.closeEntry(); |
174 |
} |
175 |
zipInputStream.close(); |
176 |
|
177 |
} catch (Exception e) { |
178 |
throw new InstallPackageServiceException( |
179 |
"Error reading the plugin", e);
|
180 |
} |
181 |
} |
182 |
|
183 |
private void decompressFolderOfPluginFromPackage(InputStream is, String name) |
184 |
throws InstallPackageServiceException {
|
185 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
186 |
|
187 |
try {
|
188 |
if (option == OPTION_INSTALL) {
|
189 |
|
190 |
} else {
|
191 |
if (option == OPTION_DECOMPRESS) {
|
192 |
decompressPlugin(zipInputStream); |
193 |
} else {
|
194 |
if (option == OPTION_READ_INSTALLINFO) {
|
195 |
readPlugin(zipInputStream, name); |
196 |
} |
197 |
} |
198 |
} |
199 |
zipInputStream.close(); |
200 |
|
201 |
} catch (Exception e) { |
202 |
throw new InstallPackageServiceException( |
203 |
"Error reading the plugin", e);
|
204 |
} |
205 |
} |
206 |
|
207 |
private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) |
208 |
throws ZipException, IOException, InstallerInfoFileException { |
209 |
ZipEntry entry = null; |
210 |
int installerInfoNumber = zipEntriesMap.size();
|
211 |
|
212 |
while ((entry = zipInputStream.getNextEntry()) != null) { |
213 |
logger.debug("Extracting: " + entry.getName());
|
214 |
|
215 |
if (entry.getName().endsWith(File.separator + "package.info")) { |
216 |
PackageInfo installerInfo = readInstallInfo(zipInputStream); |
217 |
zipEntriesMap.put(installerInfo, zipEntryName); |
218 |
readedIinstallerInfos.add(installerInfo); |
219 |
} |
220 |
zipInputStream.closeEntry(); |
221 |
} |
222 |
// zipInputStream.close();
|
223 |
|
224 |
if (installerInfoNumber == zipEntriesMap.size()) {
|
225 |
PackageInfo installerInfo = new DefaultPackageInfo();
|
226 |
installerInfo.setCode(zipEntryName); |
227 |
installerInfo.setName(zipEntryName); |
228 |
zipEntriesMap.put(installerInfo, zipEntryName); |
229 |
readedIinstallerInfos.add(installerInfo); |
230 |
} |
231 |
} |
232 |
|
233 |
private void decompressPlugin(ZipInputStream zipInputStream) |
234 |
throws ZipException, IOException, |
235 |
InstallerInfoFileException { |
236 |
ZipEntry entry = null; |
237 |
byte data[] = new byte[BUFFER]; |
238 |
int count;
|
239 |
|
240 |
while ((entry = zipInputStream.getNextEntry()) != null) { |
241 |
logger.debug("Extracting: " + entry.getName());
|
242 |
|
243 |
File file =
|
244 |
new File(outputDirectory.getAbsolutePath() + File.separator |
245 |
+ entry.getName()); |
246 |
if (file.exists()) {
|
247 |
delete(file); |
248 |
} |
249 |
if (entry.isDirectory()) {
|
250 |
file.mkdir(); |
251 |
} else {
|
252 |
createParentFolder(file); |
253 |
FileOutputStream fos = new FileOutputStream(file); |
254 |
|
255 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
256 |
fos.write(data, 0, count);
|
257 |
} |
258 |
fos.flush(); |
259 |
fos.close(); |
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
private void createParentFolder(File file) { |
265 |
File parentFile = file.getParentFile();
|
266 |
if (!parentFile.exists()) {
|
267 |
createParentFolder(parentFile); |
268 |
parentFile.mkdir(); |
269 |
} |
270 |
} |
271 |
|
272 |
public boolean delete(File dir) { |
273 |
if (dir.isDirectory()) {
|
274 |
String[] children = dir.list(); |
275 |
for (int i = 0; i < children.length; i++) { |
276 |
boolean success = delete(new File(dir, children[i])); |
277 |
if (!success) {
|
278 |
return false; |
279 |
} |
280 |
} |
281 |
} |
282 |
return dir.delete();
|
283 |
} |
284 |
|
285 |
public PackageInfo readInstallerInfo(InputStream is) |
286 |
throws InstallerInfoFileException {
|
287 |
try {
|
288 |
return readInstallInfo(new ZipInputStream(is)); |
289 |
} catch (IOException e) { |
290 |
throw new InstallerInfoFileException("error_reading_installerinfo", |
291 |
e); |
292 |
} |
293 |
} |
294 |
|
295 |
private PackageInfo readInstallInfo(ZipInputStream zipInputStream) |
296 |
throws IOException, InstallerInfoFileException { |
297 |
int count;
|
298 |
byte data[] = new byte[BUFFER]; |
299 |
|
300 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
301 |
|
302 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
303 |
out.write(data, 0, count);
|
304 |
} |
305 |
out.flush(); |
306 |
|
307 |
DefaultPackageInfo installerInfo = new DefaultPackageInfo();
|
308 |
InstallerInfoFileReader installerInfoFileReader = |
309 |
new InstallerInfoFileReader();
|
310 |
installerInfoFileReader.read(installerInfo, new ByteArrayInputStream( |
311 |
out.toByteArray())); |
312 |
|
313 |
return installerInfo;
|
314 |
} |
315 |
} |