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

History | View | Annotate | Download (4.18 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.MakePluginPackageServiceException;
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 MakePluginPackageServiceException {
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(File directory, OutputStream os) throws MakePluginPackageServiceException{
65
                File[] files = directory.listFiles();
66
                List<File> filesArray = new ArrayList<File>();
67
                List<String> fileNamesArray = new ArrayList<String>();
68
                for (int i=0 ; i<files.length ; i++){
69
                        filesArray.add(files[i]);
70
                        fileNamesArray.add(files[i].getName());
71
                }
72
                compressPlugins(filesArray, fileNamesArray, os);
73
        }
74

    
75

    
76
        public void compressPlugins(List<File> files, List<String> fileNames, OutputStream os) throws MakePluginPackageServiceException {
77
                try{
78
                        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
79

    
80
                        for (int i=0 ; i<files.size() ; i++){
81
                                File file = files.get(i);                                
82

    
83
                                int parentFileLenght = file.getParentFile().toString().length();                                
84
                                
85
                                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));                
86
                                zos.putNextEntry(zipEntry);                                                                
87

    
88
                                ZipOutputStream zosPlugin = new ZipOutputStream(zos);                                
89
                                compressPluginFile(file, parentFileLenght, zosPlugin);                                
90
                                zosPlugin.finish();
91

    
92
                                zos.closeEntry();                                                        
93
                        }
94
                        zos.close();
95
                } catch (Exception e) {
96
                        logger.error("Error compressing the plugin");
97
                        throw new MakePluginPackageServiceException("Error writing the plugin", e);
98
                }
99
        }
100

    
101

    
102
        private void compressPluginFile(File file, int parenFileLength, ZipOutputStream zosPlugin) throws IOException  {                
103
                String fileName = file.toString().substring(parenFileLength, file.toString().length());
104
                
105
                if (file.isDirectory()) {
106
                        //Remove the subversion folders
107
                        if (!file.getName().toUpperCase().equals(".SVN")){                                
108
                                //Adding the files 
109
                                String[] fileNames = file.list();
110
                                if (fileNames != null) {
111
                                        for (int i=0; i<fileNames.length; i++)  {
112
                                                compressPluginFile(new File(file, fileNames[i]), parenFileLength, zosPlugin);
113
                                        }
114
                                }
115
                        }
116
                }else{
117
                        byte[] buf = new byte[1024];
118
                        int len;                
119

    
120
                        ZipEntry zipEntry = new ZipEntry(fileName);      
121
                        zosPlugin.putNextEntry(zipEntry);   
122

    
123
                        FileInputStream fin = new FileInputStream(file);
124
                        BufferedInputStream in = new BufferedInputStream(fin);                            
125
                        while ((len = in.read(buf)) >= 0) {
126
                                zosPlugin.write(buf, 0, len);
127
                        }        
128
                        in.close();      
129
                        zosPlugin.closeEntry();
130
                }
131
        }
132
}
133