Statistics
| Revision:

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 / execution / Decompress.java @ 32451

History | View | Annotate | Download (8.55 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.execution;
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.creation.DefaultInstallerCreationService;
47
import org.gvsig.installer.lib.impl.info.InstallerInfoFileException;
48
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
49
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
50
import org.gvsig.installer.lib.spi.InstallerProviderManager;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
/**
55
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
56
 */
57
public class Decompress {
58
        private static final int OPTION_DECOMPRESS = 1;
59
        private static final int OPTION_READ_INSTALLINFO = 2;
60
        private static final int OPTION_INSTALL = 3;
61

    
62
        private int option = 0;
63

    
64
        private int BUFFER = 2048;
65

    
66
        private static final Logger logger = LoggerFactory.getLogger(Decompress.class);
67

    
68
        private List<InstallerInfo> readedIinstallerInfos = null;        
69
        private File outputDirectory = null;        
70
        private List<InstallerInfo> selectedInstallerInfos = null;
71
        private Map<InstallerInfo, String> zipEntriesMap = null;
72

    
73
        private static final InstallerProviderManager installerProviderManager = InstallerProviderLocator.getProviderManager();
74

    
75
        public Decompress(){                
76

    
77
        }
78

    
79
        public void decompressPlugins(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
80
                option = OPTION_DECOMPRESS;
81
                this.outputDirectory = outputDirectory;
82
                
83
                decompressFolderOfPlugins(is);
84
        }
85

    
86
        public void readInstallInfo(InputStream is, List<InstallerInfo> installerInfos, Map<InstallerInfo, String> zipEntriesMap) throws InstallerExecutionServiceException{
87
                option = OPTION_READ_INSTALLINFO;
88
                this.readedIinstallerInfos = installerInfos;
89
                this.zipEntriesMap = zipEntriesMap;        
90
                decompressFolderOfPlugins(is);                        
91
        }        
92

    
93
        public void decompressPlugin(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
94
                try{
95
                        option = OPTION_DECOMPRESS;
96
                        this.outputDirectory = outputDirectory;                                
97
                        decompressPlugin(new ZipInputStream(is), "");
98
                } catch (Exception e) {
99
                        throw new InstallerExecutionServiceException("Error reading the plugin", e);
100
                }         
101
        }        
102

    
103
        public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallerExecutionServiceException{
104
                ZipEntry entry = null;                
105

    
106
                try {                
107
                        ZipInputStream zipInputStream = new ZipInputStream(is);        
108

    
109
                        while((entry = zipInputStream.getNextEntry()) != null) {        
110
                                if (entry.getName().equals(zipEntry)){
111
                                        return zipInputStream;
112
                                }                                
113
                                zipInputStream.closeEntry();
114
                        }
115
                        zipInputStream.closeEntry();
116

    
117
                        zipInputStream.close();                        
118
                } catch (Exception e) {
119
                        throw new InstallerExecutionServiceException("Error reading the plugin", e);
120
                }         
121
                return null;        
122
        }
123

    
124
        public void installFromStream(InputStream is, InstallerInfo installerInfo) throws InstallerExecutionServiceException{
125
                option = OPTION_INSTALL;
126
                this.selectedInstallerInfos = new ArrayList<InstallerInfo>();
127
                this.selectedInstallerInfos.add(installerInfo);
128
                decompressFolderOfPlugins(is);
129
        }
130

    
131
        public void installFromStream(InputStream is, List<InstallerInfo> installerInfos) throws InstallerExecutionServiceException{
132
                option = OPTION_INSTALL;
133
                this.selectedInstallerInfos = installerInfos;                
134
                decompressFolderOfPlugins(is);
135
        }        
136

    
137

    
138
        private void decompressFolderOfPlugins(InputStream is) throws InstallerExecutionServiceException {
139
                ZipInputStream zipInputStream = new ZipInputStream(is);        
140
                ZipEntry entry = null;                
141

    
142
                try {                        
143

    
144
                        while((entry = zipInputStream.getNextEntry()) != null) {        
145
                                logger.debug("Extracting all Plugins, plugin: " + entry);
146
                                if (option == OPTION_INSTALL){
147

    
148
                                }else if (option == OPTION_DECOMPRESS){                                
149
                                        decompressPlugin(new ZipInputStream(zipInputStream), entry.getName());
150
                                }else if (option == OPTION_READ_INSTALLINFO){                        
151
                                        readPlugin(new ZipInputStream(zipInputStream), entry.getName());
152
                                }
153
                                zipInputStream.closeEntry();
154
                        }                        
155
                        zipInputStream.close();        
156
                        
157
                } catch (Exception e) {
158
                        throw new InstallerExecutionServiceException("Error reading the plugin", e);
159
                }                         
160
        }
161

    
162
        private void readPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
163
                ZipEntry entry = null;
164
                byte data[] = new byte[BUFFER];                
165
                int count;
166
                int installerInfoNumber = zipEntriesMap.size();
167

    
168
                while((entry = zipInputStream.getNextEntry()) != null) {                                
169
                        logger.debug("Extracting: " + entry.getName());        
170

    
171
                        if (entry.getName().endsWith(File.separator + DefaultInstallerCreationService.INSTALLINFO_FILE_NAME)){
172
                                InstallerInfo installerInfo = readInstallInfo(zipInputStream);
173
                                zipEntriesMap.put(installerInfo, zipEntryName);                        
174
                                readedIinstallerInfos.add(installerInfo);                                        
175
                        }
176
                        zipInputStream.closeEntry();
177
                }                        
178
                //zipInputStream.close();        
179
                
180
                if (installerInfoNumber == zipEntriesMap.size()){
181
                        InstallerInfo installerInfo = new DefaultInstallerInfo();
182
                        installerInfo.setCode(zipEntryName);
183
                        installerInfo.setName(zipEntryName);
184
                        zipEntriesMap.put(installerInfo, zipEntryName);
185
                        readedIinstallerInfos.add(installerInfo);                
186
                }
187
        }
188

    
189

    
190
        private void decompressPlugin(ZipInputStream zipInputStream, String zipEntryName) throws ZipException, IOException, InstallerInfoFileException{
191
                ZipEntry entry = null;
192
                byte data[] = new byte[BUFFER];                
193
                int count;
194
                int installerInfoNumber = 0;
195

    
196
                if (zipEntriesMap != null){
197
                        installerInfoNumber = zipEntriesMap.size();
198
                }
199

    
200
                while((entry = zipInputStream.getNextEntry()) != null) {                                
201
                        logger.debug("Extracting: " + entry.getName());        
202

    
203
                        File file = new File(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
204
                        if (file.exists()){
205
                                delete(file);
206
                        }
207
                        if (entry.isDirectory()){
208
                                file.mkdir();
209
                        }else{
210
                                FileOutputStream fos = new FileOutputStream(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
211

    
212
                                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
213
                                        fos.write(data, 0, count);
214
                                }
215
                                fos.flush();                
216
                                fos.close();        
217
                        }
218
                        //zipInputStream.closeEntry();
219
                }
220
        }        
221

    
222
        public boolean delete(File dir) { 
223
                if (dir.isDirectory()){
224
                        String[] children = dir.list(); 
225
                        for (int i=0; i<children.length; i++) { 
226
                                boolean success = delete(new File(dir, children[i]));
227
                                if (!success) { 
228
                                        return false; 
229
                                }
230
                        } 
231
                }
232
                return dir.delete(); 
233
        }
234

    
235
        public InstallerInfo readInstallerInfo(InputStream is) throws InstallerInfoFileException{
236
                try {
237
                        return readInstallInfo(new ZipInputStream(is));
238
                } catch (IOException e) {
239
                        throw new InstallerInfoFileException("error_reading_installerinfo", e);
240
                }
241
        }
242

    
243
        private InstallerInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{
244
                int count;
245
                byte data[] = new byte[BUFFER];
246

    
247
                ByteArrayOutputStream out = new ByteArrayOutputStream();
248

    
249
                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
250
                        out.write(data, 0, count);
251
                }
252
                out.flush();
253

    
254
                DefaultInstallerInfo installerInfo = new DefaultInstallerInfo();
255
                InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
256
                installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray()));
257

    
258
                return installerInfo;
259
        }        
260
}
261