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

History | View | Annotate | Download (4.54 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.zip.ZipEntry;
39
import java.util.zip.ZipException;
40
import java.util.zip.ZipInputStream;
41

    
42
import org.gvsig.installer.lib.api.InstallerInfo;
43
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException;
44
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
45
import org.gvsig.installer.lib.impl.creation.DefaultInstallerCreationService;
46
import org.gvsig.installer.lib.impl.info.InstallerInfoFileException;
47
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
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 int BUFFER = 2048;
56
        private static final Logger logger = LoggerFactory.getLogger(Decompress.class);
57
        private List<InstallerInfo> installerInfos = null;
58
        private boolean decompress = false;
59
        private File outputDirectory = null;
60

    
61
        public void decompressPlugins(InputStream is, File outputDirectory) throws InstallerExecutionServiceException {
62
                decompress = true;
63
                this.outputDirectory = outputDirectory;
64
                decompressFolderOfPlugins(is);
65
        }        
66

    
67
        public List<InstallerInfo> readInstallInfo(InputStream is) throws InstallerExecutionServiceException{
68
                decompress = false;
69
                installerInfos = new ArrayList<InstallerInfo>();
70
                decompressFolderOfPlugins(is);
71
                return installerInfos;
72
        }
73

    
74
        private void decompressFolderOfPlugins(InputStream is) throws InstallerExecutionServiceException {
75
                ZipInputStream zipInputStream = new ZipInputStream(is);        
76
                ZipEntry entry = null;
77

    
78
                try {
79
                        while((entry = zipInputStream.getNextEntry()) != null) {                                
80
                                logger.debug("Extracting Plugin: " + entry);
81

    
82
                                decompressPlugin(new ZipInputStream(zipInputStream));
83

    
84
                                zipInputStream.closeEntry();
85
                        }
86
                        zipInputStream.close();                        
87
                } catch (Exception e) {
88
                        throw new InstallerExecutionServiceException("Error reading the plugin", e);
89
                }                         
90
        }        
91

    
92
        private void decompressPlugin(ZipInputStream zipInputStream) throws ZipException, IOException, InstallerInfoFileException{
93
                ZipEntry entry = null;
94
                byte data[] = new byte[BUFFER];                
95
                int count;
96

    
97
                while((entry = zipInputStream.getNextEntry()) != null) {                                
98
                        logger.debug("Extracting: " + entry.getName());        
99
                        if (decompress){
100
                                FileOutputStream fos = new FileOutputStream(outputDirectory.getAbsolutePath() + File.separator + entry.getName());
101

    
102
                                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
103
                                        fos.write(data, 0, count);
104
                                }
105
                                fos.flush();                
106
                                fos.close();        
107
                        }else{
108
                                if (entry.getName().endsWith(File.separator + DefaultInstallerCreationService.INSTALLINFO_FILE_NAME)){
109
                                        installerInfos.add(readInstallInfo(zipInputStream));
110
                                }
111
                        }
112

    
113
                        zipInputStream.closeEntry();
114
                }                
115
        }        
116
        
117
        private InstallerInfo readInstallInfo(ZipInputStream zipInputStream) throws IOException, InstallerInfoFileException{
118
                int count;
119
                byte data[] = new byte[BUFFER];
120
                
121
                ByteArrayOutputStream out = new ByteArrayOutputStream();
122
                                
123
                while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
124
                        out.write(data, 0, count);
125
                }
126
                out.flush();
127
                
128
                InstallerInfo installerInfo = new DefaultInstallerInfo();
129
                InstallerInfoFileReader installerInfoFileReader = new InstallerInfoFileReader();
130
                installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(out.toByteArray()));
131
                
132
                return installerInfo;
133
        }        
134
}
135