Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / Compress.java @ 34107

History | View | Annotate | Download (5.49 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
            int parentFileLenght = folder.getParentFile().toString().length();
106
            compressPluginFile(folder, parentFileLenght, zos);
107
            zos.flush();
108
            zos.close();
109
        } catch (IOException e) {
110
            throw new MakePluginPackageServiceException(
111
                "Error compressing the plugin folder: " + folder, e);
112
        }
113
    }
114

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

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

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

    
145
            ZipEntry zipEntry = new ZipEntry(fileName);
146
            zosPlugin.putNextEntry(zipEntry);
147

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