Statistics
| Revision:

svn-gvsig-desktop / 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 @ 32585

History | View | Annotate | Download (8.07 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.PackageInfo;
44
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
45
import org.gvsig.installer.lib.impl.DefaultPackageInfo;
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&aacute;</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<PackageInfo> readedIinstallerInfos = null;        
66
        private File outputDirectory = null;        
67
        private List<PackageInfo> selectedInstallerInfos = null;
68
        private Map<PackageInfo, String> zipEntriesMap = null;
69

    
70
        public Decompress(){                
71

    
72
        }
73

    
74
        public void decompressPlugins(InputStream is, File outputDirectory) throws InstallPackageServiceException {
75
                option = OPTION_DECOMPRESS;
76
                this.outputDirectory = outputDirectory;
77
                
78
                decompressFolderOfPlugins(is);
79
        }
80

    
81
        public void readInstallInfo(InputStream is, List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap) throws InstallPackageServiceException{
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 InstallPackageServiceException {
89
                try{
90
                        option = OPTION_DECOMPRESS;
91
                        this.outputDirectory = outputDirectory;                                
92
                        decompressPlugin(new ZipInputStream(is), "");
93
                } catch (Exception e) {
94
                        throw new InstallPackageServiceException("Error reading the plugin", e);
95
                }         
96
        }        
97

    
98
        public InputStream searchPlugin(InputStream is, String zipEntry) throws InstallPackageServiceException{
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 InstallPackageServiceException("Error reading the plugin", e);
115
                }         
116
                return null;        
117
        }
118

    
119
        public void installFromStream(InputStream is, PackageInfo installerInfo) throws InstallPackageServiceException{
120
                option = OPTION_INSTALL;
121
                this.selectedInstallerInfos = new ArrayList<PackageInfo>();
122
                this.selectedInstallerInfos.add(installerInfo);
123
                decompressFolderOfPlugins(is);
124
        }
125

    
126
        public void installFromStream(InputStream is, List<PackageInfo> installerInfos) throws InstallPackageServiceException{
127
                option = OPTION_INSTALL;
128
                this.selectedInstallerInfos = installerInfos;                
129
                decompressFolderOfPlugins(is);
130
        }        
131

    
132

    
133
        private void decompressFolderOfPlugins(InputStream is) throws InstallPackageServiceException {
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 InstallPackageServiceException("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 + "package.info")){
165
                                PackageInfo 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
                        PackageInfo installerInfo = new DefaultPackageInfo();
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
                                createParentFolder(file);
199
                                FileOutputStream fos = new FileOutputStream(file);
200

    
201
                                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
202
                                        fos.write(data, 0, count);
203
                                }
204
                                fos.flush();                
205
                                fos.close();        
206
                        }                        
207
                }
208
        }        
209

    
210
        private void createParentFolder(File file) {
211
                File parentFile = file.getParentFile();
212
                if (!parentFile.exists()){
213
                        createParentFolder(parentFile);
214
                        parentFile.mkdir();
215
                }
216
        }
217

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

    
231
        public PackageInfo readInstallerInfo(InputStream is) throws InstallerInfoFileException{
232
                try {
233
                        return readInstallInfo(new ZipInputStream(is));
234
                } catch (IOException e) {
235
                        throw new InstallerInfoFileException("error_reading_installerinfo", e);
236
                }
237
        }
238

    
239
        private PackageInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{
240
                int count;
241
                byte data[] = new byte[BUFFER];
242

    
243
                ByteArrayOutputStream out = new ByteArrayOutputStream();
244

    
245
                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
246
                        out.write(data, 0, count);
247
                }
248
                out.flush();
249

    
250
                DefaultPackageInfo installerInfo = new DefaultPackageInfo();
251
                InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
252
                installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray()));
253

    
254
                return installerInfo;
255
        }        
256
}
257