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 | 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 | 32269 | jpiera | |
52 | /**
|
||
53 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
||
54 | */
|
||
55 | public class Decompress { |
||
56 | 32400 | jpiera | |
57 | 33729 | cordinyana | 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 | 32400 | jpiera | |
61 | 33729 | cordinyana | private int option = 0; |
62 | 32400 | jpiera | |
63 | 33729 | cordinyana | private int BUFFER = 2048; |
64 | 32287 | jpiera | |
65 | 33729 | cordinyana | private static final Logger logger = LoggerFactory |
66 | .getLogger(Decompress.class); |
||
67 | 32400 | jpiera | |
68 | 33729 | cordinyana | private List<PackageInfo> readedIinstallerInfos = null; |
69 | private File outputDirectory = null; |
||
70 | private List<PackageInfo> selectedInstallerInfos = null; |
||
71 | private Map<PackageInfo, String> zipEntriesMap = null; |
||
72 | 32400 | jpiera | |
73 | 33729 | cordinyana | public Decompress() {
|
74 | 32400 | jpiera | |
75 | 33729 | cordinyana | } |
76 | 32400 | jpiera | |
77 | 33729 | cordinyana | public void decompressPlugins(InputStream is, File outputDirectory) |
78 | throws InstallPackageServiceException {
|
||
79 | option = OPTION_DECOMPRESS; |
||
80 | this.outputDirectory = outputDirectory;
|
||
81 | 32287 | jpiera | |
82 | 33729 | cordinyana | decompressFolderOfPlugins(is); |
83 | } |
||
84 | 32400 | jpiera | |
85 | 34020 | cordinyana | public void readPackageSetInstallInfos(InputStream is, |
86 | 33729 | cordinyana | 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 | 32400 | jpiera | |
94 | 34020 | cordinyana | public void readPackageInstallInfo(InputStream is, |
95 | 34027 | cordinyana | List<PackageInfo> installerInfos,
|
96 | Map<PackageInfo, String> zipEntriesMap, String name) |
||
97 | 34020 | cordinyana | throws InstallPackageServiceException {
|
98 | option = OPTION_READ_INSTALLINFO; |
||
99 | this.readedIinstallerInfos = installerInfos;
|
||
100 | this.zipEntriesMap = zipEntriesMap;
|
||
101 | 34027 | cordinyana | decompressFolderOfPluginFromPackage(is, name); |
102 | 34020 | cordinyana | } |
103 | |||
104 | 33729 | cordinyana | public void decompressPlugin(InputStream is, File outputDirectory) |
105 | throws InstallPackageServiceException {
|
||
106 | try {
|
||
107 | option = OPTION_DECOMPRESS; |
||
108 | this.outputDirectory = outputDirectory;
|
||
109 | 34027 | cordinyana | decompressPlugin(new ZipInputStream(is)); |
110 | 33729 | cordinyana | } catch (Exception e) { |
111 | throw new InstallPackageServiceException( |
||
112 | "Error reading the plugin", e);
|
||
113 | } |
||
114 | } |
||
115 | 32400 | jpiera | |
116 | 33729 | cordinyana | public InputStream searchPlugin(InputStream is, String zipEntry) |
117 | throws InstallPackageServiceException {
|
||
118 | ZipEntry entry = null; |
||
119 | 32400 | jpiera | |
120 | 33729 | cordinyana | try {
|
121 | ZipInputStream zipInputStream = new ZipInputStream(is); |
||
122 | 32400 | jpiera | |
123 | 33729 | cordinyana | while ((entry = zipInputStream.getNextEntry()) != null) { |
124 | if (entry.getName().equals(zipEntry)) {
|
||
125 | return zipInputStream;
|
||
126 | } |
||
127 | zipInputStream.closeEntry(); |
||
128 | } |
||
129 | zipInputStream.closeEntry(); |
||
130 | 32287 | jpiera | |
131 | 33729 | cordinyana | zipInputStream.close(); |
132 | } catch (Exception e) { |
||
133 | throw new InstallPackageServiceException( |
||
134 | "Error reading the plugin", e);
|
||
135 | } |
||
136 | return null; |
||
137 | } |
||
138 | 32400 | jpiera | |
139 | 33729 | cordinyana | 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 | 32400 | jpiera | |
147 | 33729 | cordinyana | public void installFromStream(InputStream is, |
148 | List<PackageInfo> installerInfos) throws InstallPackageServiceException { |
||
149 | option = OPTION_INSTALL; |
||
150 | this.selectedInstallerInfos = installerInfos;
|
||
151 | decompressFolderOfPlugins(is); |
||
152 | } |
||
153 | 32287 | jpiera | |
154 | 33729 | cordinyana | private void decompressFolderOfPlugins(InputStream is) |
155 | throws InstallPackageServiceException {
|
||
156 | ZipInputStream zipInputStream = new ZipInputStream(is); |
||
157 | ZipEntry entry = null; |
||
158 | 32269 | jpiera | |
159 | 33729 | cordinyana | try {
|
160 | 32287 | jpiera | |
161 | 33729 | cordinyana | while ((entry = zipInputStream.getNextEntry()) != null) { |
162 | logger.debug("Extracting all Plugins, plugin: " + entry);
|
||
163 | if (option == OPTION_INSTALL) {
|
||
164 | 32269 | jpiera | |
165 | 33729 | cordinyana | } else
|
166 | if (option == OPTION_DECOMPRESS) {
|
||
167 | 34027 | cordinyana | decompressPlugin(new ZipInputStream(zipInputStream)); |
168 | 33729 | cordinyana | } else
|
169 | if (option == OPTION_READ_INSTALLINFO) {
|
||
170 | readPlugin(new ZipInputStream(zipInputStream), |
||
171 | entry.getName()); |
||
172 | } |
||
173 | zipInputStream.closeEntry(); |
||
174 | } |
||
175 | zipInputStream.close(); |
||
176 | 32411 | jpiera | |
177 | 33729 | cordinyana | } catch (Exception e) { |
178 | throw new InstallPackageServiceException( |
||
179 | "Error reading the plugin", e);
|
||
180 | } |
||
181 | } |
||
182 | 32411 | jpiera | |
183 | 34027 | cordinyana | private void decompressFolderOfPluginFromPackage(InputStream is, String name) |
184 | 34020 | cordinyana | throws InstallPackageServiceException {
|
185 | ZipInputStream zipInputStream = new ZipInputStream(is); |
||
186 | |||
187 | try {
|
||
188 | 34027 | cordinyana | if (option == OPTION_INSTALL) {
|
189 | 34020 | cordinyana | |
190 | 34027 | cordinyana | } else {
|
191 | if (option == OPTION_DECOMPRESS) {
|
||
192 | decompressPlugin(zipInputStream); |
||
193 | } else {
|
||
194 | if (option == OPTION_READ_INSTALLINFO) {
|
||
195 | readPlugin(zipInputStream, name); |
||
196 | 34020 | cordinyana | } |
197 | 34027 | cordinyana | } |
198 | 34020 | cordinyana | } |
199 | zipInputStream.close(); |
||
200 | |||
201 | } catch (Exception e) { |
||
202 | throw new InstallPackageServiceException( |
||
203 | "Error reading the plugin", e);
|
||
204 | } |
||
205 | } |
||
206 | |||
207 | 33729 | cordinyana | private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) |
208 | throws ZipException, IOException, InstallerInfoFileException { |
||
209 | ZipEntry entry = null; |
||
210 | int installerInfoNumber = zipEntriesMap.size();
|
||
211 | 32411 | jpiera | |
212 | 33729 | cordinyana | while ((entry = zipInputStream.getNextEntry()) != null) { |
213 | logger.debug("Extracting: " + entry.getName());
|
||
214 | 32411 | jpiera | |
215 | 33729 | cordinyana | 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 | 32287 | jpiera | |
224 | 33729 | cordinyana | 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 | 32411 | jpiera | |
233 | 34027 | cordinyana | private void decompressPlugin(ZipInputStream zipInputStream) |
234 | throws ZipException, IOException, |
||
235 | 33729 | cordinyana | InstallerInfoFileException { |
236 | ZipEntry entry = null; |
||
237 | byte data[] = new byte[BUFFER]; |
||
238 | int count;
|
||
239 | 32400 | jpiera | |
240 | 33729 | cordinyana | while ((entry = zipInputStream.getNextEntry()) != null) { |
241 | logger.debug("Extracting: " + entry.getName());
|
||
242 | 32585 | jpiera | |
243 | 33729 | cordinyana | 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 | 32400 | jpiera | |
255 | 33729 | cordinyana | 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 | 32400 | jpiera | |
264 | 33729 | cordinyana | private void createParentFolder(File file) { |
265 | File parentFile = file.getParentFile();
|
||
266 | if (!parentFile.exists()) {
|
||
267 | createParentFolder(parentFile); |
||
268 | parentFile.mkdir(); |
||
269 | } |
||
270 | } |
||
271 | 32400 | jpiera | |
272 | 33729 | cordinyana | 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 | 32400 | jpiera | |
285 | 33729 | cordinyana | 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 | 32400 | jpiera | |
295 | 33729 | cordinyana | private PackageInfo readInstallInfo(ZipInputStream zipInputStream) |
296 | throws IOException, InstallerInfoFileException { |
||
297 | int count;
|
||
298 | byte data[] = new byte[BUFFER]; |
||
299 | 32400 | jpiera | |
300 | 33729 | cordinyana | 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 | 32269 | jpiera | } |