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 @ 32287

History | View | Annotate | Download (4.59 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.Iterator;
38
import java.util.List;
39
import java.util.Properties;
40
import java.util.zip.ZipEntry;
41
import java.util.zip.ZipException;
42
import java.util.zip.ZipInputStream;
43

    
44
import org.gvsig.installer.lib.api.InstallerInfo;
45
import org.gvsig.installer.lib.api.execution.InstallerExecutionServiceException;
46
import org.gvsig.installer.lib.impl.DefaultInstallerInfo;
47
import org.gvsig.installer.lib.impl.creation.DefaultInstallerCreationService;
48
import org.gvsig.installer.lib.impl.info.InstallerInfoFileException;
49
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
/**
54
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
55
 */
56
public class Decompress {
57
        private int BUFFER = 2048;
58
        private static final Logger logger = LoggerFactory.getLogger(Decompress.class);
59
        private List<InstallerInfo> installerInfos = null;
60
        private boolean decompress = false;
61
        private File outputDirectory = null;
62

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

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

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

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

    
84
                                decompressPlugin(new ZipInputStream(zipInputStream));
85

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

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

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

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

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