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 @ 32516
History | View | Annotate | Download (8.03 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.gvsig.installer.lib.api.InstallerInfo; |
44 |
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException; |
45 |
import org.gvsig.installer.lib.impl.DefaultInstallerInfo; |
46 |
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader; |
47 |
import org.gvsig.installer.lib.spi.InstallerInfoFileException; |
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
/**
|
52 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
53 |
*/
|
54 |
public class Decompress { |
55 |
private static final int OPTION_DECOMPRESS = 1; |
56 |
private static final int OPTION_READ_INSTALLINFO = 2; |
57 |
private static final int OPTION_INSTALL = 3; |
58 |
|
59 |
private int option = 0; |
60 |
|
61 |
private int BUFFER = 2048; |
62 |
|
63 |
private static final Logger logger = LoggerFactory.getLogger(Decompress.class); |
64 |
|
65 |
private List<InstallerInfo> readedIinstallerInfos = null; |
66 |
private File outputDirectory = null; |
67 |
private List<InstallerInfo> selectedInstallerInfos = null; |
68 |
private Map<InstallerInfo, String> zipEntriesMap = null; |
69 |
|
70 |
public Decompress(){
|
71 |
|
72 |
} |
73 |
|
74 |
public void decompressPlugins(InputStream is, File outputDirectory) throws InstallerExecutionServiceException { |
75 |
option = OPTION_DECOMPRESS; |
76 |
this.outputDirectory = outputDirectory;
|
77 |
|
78 |
decompressFolderOfPlugins(is); |
79 |
} |
80 |
|
81 |
public void readInstallInfo(InputStream is, List<InstallerInfo> installerInfos, Map<InstallerInfo, String> zipEntriesMap) throws InstallerExecutionServiceException{ |
82 |
option = OPTION_READ_INSTALLINFO; |
83 |
this.readedIinstallerInfos = installerInfos;
|
84 |
this.zipEntriesMap = zipEntriesMap;
|
85 |
decompressFolderOfPlugins(is); |
86 |
} |
87 |
|
88 |
public void decompressPlugin(InputStream is, File outputDirectory) throws InstallerExecutionServiceException { |
89 |
try{
|
90 |
option = OPTION_DECOMPRESS; |
91 |
this.outputDirectory = outputDirectory;
|
92 |
decompressPlugin(new ZipInputStream(is), ""); |
93 |
} catch (Exception e) { |
94 |
throw new InstallerExecutionServiceException("Error reading the plugin", e); |
95 |
} |
96 |
} |
97 |
|
98 |
public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallerExecutionServiceException{ |
99 |
ZipEntry entry = null; |
100 |
|
101 |
try {
|
102 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
103 |
|
104 |
while((entry = zipInputStream.getNextEntry()) != null) { |
105 |
if (entry.getName().equals(zipEntry)){
|
106 |
return zipInputStream;
|
107 |
} |
108 |
zipInputStream.closeEntry(); |
109 |
} |
110 |
zipInputStream.closeEntry(); |
111 |
|
112 |
zipInputStream.close(); |
113 |
} catch (Exception e) { |
114 |
throw new InstallerExecutionServiceException("Error reading the plugin", e); |
115 |
} |
116 |
return null; |
117 |
} |
118 |
|
119 |
public void installFromStream(InputStream is, InstallerInfo installerInfo) throws InstallerExecutionServiceException{ |
120 |
option = OPTION_INSTALL; |
121 |
this.selectedInstallerInfos = new ArrayList<InstallerInfo>(); |
122 |
this.selectedInstallerInfos.add(installerInfo);
|
123 |
decompressFolderOfPlugins(is); |
124 |
} |
125 |
|
126 |
public void installFromStream(InputStream is, List<InstallerInfo> installerInfos) throws InstallerExecutionServiceException{ |
127 |
option = OPTION_INSTALL; |
128 |
this.selectedInstallerInfos = installerInfos;
|
129 |
decompressFolderOfPlugins(is); |
130 |
} |
131 |
|
132 |
|
133 |
private void decompressFolderOfPlugins(InputStream is) throws InstallerExecutionServiceException { |
134 |
ZipInputStream zipInputStream = new ZipInputStream(is); |
135 |
ZipEntry entry = null; |
136 |
|
137 |
try {
|
138 |
|
139 |
while((entry = zipInputStream.getNextEntry()) != null) { |
140 |
logger.debug("Extracting all Plugins, plugin: " + entry);
|
141 |
if (option == OPTION_INSTALL){
|
142 |
|
143 |
}else if (option == OPTION_DECOMPRESS){ |
144 |
decompressPlugin(new ZipInputStream(zipInputStream), entry.getName()); |
145 |
}else if (option == OPTION_READ_INSTALLINFO){ |
146 |
readPlugin(new ZipInputStream(zipInputStream), entry.getName()); |
147 |
} |
148 |
zipInputStream.closeEntry(); |
149 |
} |
150 |
zipInputStream.close(); |
151 |
|
152 |
} catch (Exception e) { |
153 |
throw new InstallerExecutionServiceException("Error reading the plugin", e); |
154 |
} |
155 |
} |
156 |
|
157 |
private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{ |
158 |
ZipEntry entry = null; |
159 |
int installerInfoNumber = zipEntriesMap.size();
|
160 |
|
161 |
while((entry = zipInputStream.getNextEntry()) != null) { |
162 |
logger.debug("Extracting: " + entry.getName());
|
163 |
|
164 |
if (entry.getName().endsWith(File.separator + "install.info")){ |
165 |
InstallerInfo installerInfo = readInstallInfo(zipInputStream); |
166 |
zipEntriesMap.put(installerInfo, zipEntryName); |
167 |
readedIinstallerInfos.add(installerInfo); |
168 |
} |
169 |
zipInputStream.closeEntry(); |
170 |
} |
171 |
//zipInputStream.close();
|
172 |
|
173 |
if (installerInfoNumber == zipEntriesMap.size()){
|
174 |
InstallerInfo installerInfo = new DefaultInstallerInfo();
|
175 |
installerInfo.setCode(zipEntryName); |
176 |
installerInfo.setName(zipEntryName); |
177 |
zipEntriesMap.put(installerInfo, zipEntryName); |
178 |
readedIinstallerInfos.add(installerInfo); |
179 |
} |
180 |
} |
181 |
|
182 |
|
183 |
private void decompressPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{ |
184 |
ZipEntry entry = null; |
185 |
byte data[] = new byte[BUFFER]; |
186 |
int count;
|
187 |
|
188 |
while((entry = zipInputStream.getNextEntry()) != null) { |
189 |
logger.debug("Extracting: " + entry.getName());
|
190 |
|
191 |
File file = new File(outputDirectory.getAbsolutePath() + File.separator + entry.getName()); |
192 |
if (file.exists()){
|
193 |
delete(file); |
194 |
} |
195 |
if (entry.isDirectory()){
|
196 |
file.mkdir(); |
197 |
}else{
|
198 |
FileOutputStream fos = new FileOutputStream(outputDirectory.getAbsolutePath() + File.separator + entry.getName()); |
199 |
|
200 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
201 |
fos.write(data, 0, count);
|
202 |
} |
203 |
fos.flush(); |
204 |
fos.close(); |
205 |
} |
206 |
//zipInputStream.closeEntry();
|
207 |
} |
208 |
} |
209 |
|
210 |
public boolean delete(File dir) { |
211 |
if (dir.isDirectory()){
|
212 |
String[] children = dir.list(); |
213 |
for (int i=0; i<children.length; i++) { |
214 |
boolean success = delete(new File(dir, children[i])); |
215 |
if (!success) {
|
216 |
return false; |
217 |
} |
218 |
} |
219 |
} |
220 |
return dir.delete();
|
221 |
} |
222 |
|
223 |
public InstallerInfo readInstallerInfo(InputStream is) throws InstallerInfoFileException{ |
224 |
try {
|
225 |
return readInstallInfo(new ZipInputStream(is)); |
226 |
} catch (IOException e) { |
227 |
throw new InstallerInfoFileException("error_reading_installerinfo", e); |
228 |
} |
229 |
} |
230 |
|
231 |
private InstallerInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{ |
232 |
int count;
|
233 |
byte data[] = new byte[BUFFER]; |
234 |
|
235 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
236 |
|
237 |
while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) { |
238 |
out.write(data, 0, count);
|
239 |
} |
240 |
out.flush(); |
241 |
|
242 |
DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
|
243 |
InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
|
244 |
installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray())); |
245 |
|
246 |
return installerInfo;
|
247 |
} |
248 |
} |
249 |
|