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 / Compress.java @ 32500

History | View | Annotate | Download (3.81 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.BufferedInputStream;
31
import java.io.BufferedOutputStream;
32
import java.io.File;
33
import java.io.FileInputStream;
34
import java.io.IOException;
35
import java.io.OutputStream;
36
import java.util.ArrayList;
37
import java.util.List;
38
import java.util.zip.ZipEntry;
39
import java.util.zip.ZipOutputStream;
40

    
41
import org.gvsig.installer.lib.api.creation.InstallerCreationServiceException;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
/**
46
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
47
 */
48
public class Compress {
49
        static final int BUFFER = 2048;
50
        private static final Logger logger = LoggerFactory.getLogger(Compress.class);
51
                
52
        public Compress() {
53
                super();                
54
        }
55

    
56
        public void compressPlugin(File file, String fileName, OutputStream os) throws InstallerCreationServiceException {
57
                ArrayList<File> files = new ArrayList<File>();
58
                ArrayList<String> fileNames = new ArrayList<String>();
59
                files.add(file);
60
                fileNames.add(fileName);
61
                compressPlugins(files, fileNames, os);
62
        }
63

    
64
        public void compressPlugins(List<File> files, List<String> fileNames, OutputStream os) throws InstallerCreationServiceException {
65
                try{
66
                        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
67
                        byte[] buf = new byte[1024];
68
                        int len;
69
                        
70
                        for (int i=0 ; i<files.size() ; i++){
71
                                File file = files.get(i);                                
72
                                
73
                                int parentFileLenght = file.getParentFile().toString().length();
74
                                String pluginName = file.toString().substring(parentFileLenght, file.toString().length());                                
75
                                
76
                                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));                                
77
                                zos.putNextEntry(zipEntry);                                                                
78
                                        
79
                                ZipOutputStream zosPlugin = new ZipOutputStream(zos);                                
80
                                compressPluginFile(file, parentFileLenght, zosPlugin);                                
81
                                zosPlugin.finish();
82
                                
83
                                zos.closeEntry();
84
        
85
                                zos.close();                                
86
                        }
87
                        zos.close();
88
                } catch (Exception e) {
89
                        logger.error("Error compressing the plugin");
90
                        throw new InstallerCreationServiceException("Error writing the plugin", e);
91
                }
92
        }
93

    
94

    
95
        private void compressPluginFile(File file, int parenFileLength, ZipOutputStream zosPlugin) throws IOException  {                
96
                if (file.isDirectory()) {
97
                        String[] fileNames = file.list();
98
                        if (fileNames != null) {
99
                                for (int i=0; i<fileNames.length; i++)  {
100
                                        compressPluginFile(new File(file, fileNames[i]), parenFileLength, zosPlugin);
101
                                }
102
                        }
103
                }else{
104
                        byte[] buf = new byte[1024];
105
                        int len;
106

    
107
                        String fileName = file.toString().substring(parenFileLength, file.toString().length());
108
                        
109
                        ZipEntry zipEntry = new ZipEntry(fileName);      
110
                        zosPlugin.putNextEntry(zipEntry);   
111
                        
112
                        FileInputStream fin = new FileInputStream(file);
113
                        BufferedInputStream in = new BufferedInputStream(fin);                            
114
                        while ((len = in.read(buf)) >= 0) {
115
                                zosPlugin.write(buf, 0, len);
116
                        }        
117
                        in.close();      
118
                        zosPlugin.closeEntry();
119
                }
120
        }
121
}
122