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

History | View | Annotate | Download (5.53 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.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException;
45

    
46
/**
47
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
48
 */
49
public class Compress {
50

    
51
    static final int BUFFER = 2048;
52
    private static final Logger logger = LoggerFactory
53
        .getLogger(Compress.class);
54

    
55
    public Compress() {
56
        super();
57
    }
58

    
59
    public void compressPluginAsPackageSet(File file, String fileName,
60
        OutputStream os) throws MakePluginPackageServiceException {
61
        ArrayList<File> files = new ArrayList<File>();
62
        ArrayList<String> fileNames = new ArrayList<String>();
63
        files.add(file);
64
        fileNames.add(fileName);
65
        compressPluginsAsPackageSet(files, fileNames, os);
66
    }
67

    
68
    public void compressPluginsAsPackageSet(File directory, OutputStream os)
69
        throws MakePluginPackageServiceException {
70
        File[] files = directory.listFiles();
71
        List<File> filesArray = new ArrayList<File>();
72
        List<String> fileNamesArray = new ArrayList<String>();
73
        for (int i = 0; i < files.length; i++) {
74
            filesArray.add(files[i]);
75
            fileNamesArray.add(files[i].getName());
76
        }
77
        compressPluginsAsPackageSet(filesArray, fileNamesArray, os);
78
    }
79

    
80
    public void compressPluginsAsPackageSet(List<File> files,
81
        List<String> fileNames, OutputStream os)
82
        throws MakePluginPackageServiceException {
83
        try {
84
            ZipOutputStream zos =
85
                new ZipOutputStream(new BufferedOutputStream(os));
86

    
87
            for (int i = 0; i < files.size(); i++) {
88
                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
89
                zos.putNextEntry(zipEntry);
90
                compressPluginFiles(files.get(i), zos);
91
                zos.closeEntry();
92
            }
93
            zos.close();
94
        } catch (Exception e) {
95
            logger.error("Error compressing the plugin");
96
            throw new MakePluginPackageServiceException(
97
                "Error writing the plugin", e);
98
        }
99
    }
100

    
101
    public void compressPluginAsPackage(File folder, OutputStream os)
102
        throws MakePluginPackageServiceException {
103
        ZipOutputStream zos = new ZipOutputStream(os);
104
        try {
105
            ZipEntry zipEntry = new ZipEntry(folder.getName());
106
            zos.putNextEntry(zipEntry);
107
            compressPluginFiles(folder, zos);
108
            zos.closeEntry();
109
            zos.flush();
110
            zos.close();
111
        } catch (IOException e) {
112
            throw new MakePluginPackageServiceException(
113
                "Error compressing the plugin folder: " + folder, e);
114
        }
115
    }
116

    
117
    private void compressPluginFiles(File fileOrFolder, ZipOutputStream zos)
118
        throws IOException {
119
        int parentFileLenght = fileOrFolder.getParentFile().toString().length();
120
        ZipOutputStream zosPlugin = new ZipOutputStream(zos);
121
        compressPluginFile(fileOrFolder, parentFileLenght, zosPlugin);
122
        zosPlugin.finish();
123
    }
124

    
125
    private void compressPluginFile(File file, int parenFileLength,
126
        ZipOutputStream zosPlugin) throws IOException {
127
        String fileName =
128
            file.toString()
129
                .substring(parenFileLength, file.toString().length());
130

    
131
        if (file.isDirectory()) {
132
            // Remove the subversion folders
133
            if (!file.getName().toUpperCase().equals(".SVN")) {
134
                // Adding the files
135
                String[] fileNames = file.list();
136
                if (fileNames != null) {
137
                    for (int i = 0; i < fileNames.length; i++) {
138
                        compressPluginFile(new File(file, fileNames[i]),
139
                            parenFileLength, zosPlugin);
140
                    }
141
                }
142
            }
143
        } else {
144
            byte[] buf = new byte[1024];
145
            int len;
146

    
147
            ZipEntry zipEntry = new ZipEntry(fileName);
148
            zosPlugin.putNextEntry(zipEntry);
149

    
150
            FileInputStream fin = new FileInputStream(file);
151
            BufferedInputStream in = new BufferedInputStream(fin);
152
            while ((len = in.read(buf)) >= 0) {
153
                zosPlugin.write(buf, 0, len);
154
            }
155
            in.close();
156
            zosPlugin.closeEntry();
157
        }
158
    }
159
}