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

History | View | Annotate | Download (3.6 KB)

1 32269 jpiera
/* 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.creation;
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 void compressPlugin(File file, OutputStream os) throws InstallerCreationServiceException {
53
                ArrayList<File> files = new ArrayList<File>();
54
                files.add(file);
55
                compressPlugins(files, os);
56
        }
57
58
        public void compressPlugins(List<File> files, OutputStream os) throws InstallerCreationServiceException {
59
                try{
60
                        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
61
                        byte[] buf = new byte[1024];
62
                        int len;
63
64
                        for (int i=0 ; i<files.size() ; i++){
65
                                File file = files.get(i);
66
67
                                int parentFileLenght = file.getParentFile().toString().length();
68
                                String pluginName = file.toString().substring(parentFileLenght, file.toString().length());
69
70
                                //ZipEntry zipEntry = new ZipEntry(pluginName + "kk");
71
                                //zos.putNextEntry(zipEntry);
72
73
74
                                //ZipOutputStream zosPlugin = new ZipOutputStream(zos);
75
                                compressPluginFile(file, parentFileLenght, new ZipOutputStream(zos));
76
77
                                //zos.closeEntry();
78
79
80
                        }
81
                        zos.close();
82
                } catch (Exception e) {
83
                        logger.error("Error compressing the plugin");
84
                        throw new InstallerCreationServiceException("Error writing the plugin", e);
85
                }
86
        }
87
88
89
        private void compressPluginFile(File file, int parenFileLength, ZipOutputStream zos) throws IOException  {
90
                if (file.isDirectory()) {
91
                        String[] fileNames = file.list();
92
                        if (fileNames != null) {
93
                                for (int i=0; i<fileNames.length; i++)  {
94
                                        compressPluginFile(new File(file, fileNames[i]), parenFileLength, zos);
95
                                }
96
                        }
97
                }else{
98
                        byte[] buf = new byte[1024];
99
                        int len;
100
101
                        String fileName = file.toString().substring(parenFileLength, file.toString().length());
102
                        ZipEntry zipEntry = new ZipEntry(fileName);
103
                        FileInputStream fin = new FileInputStream(file);
104
                        BufferedInputStream in = new BufferedInputStream(fin);
105
                        zos.putNextEntry(zipEntry);
106
                        while ((len = in.read(buf)) >= 0) {
107
                                zos.write(buf, 0, len);
108
                        }
109
                        in.close();
110
                        zos.closeEntry();
111
                }
112
        }
113
}