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

History | View | Annotate | Download (6.92 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.gvsig.installer.lib.spi.InstallerProviderLocator;
43

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

    
49
    private static final int BUFFER = 2048;
50

    
51
    public void compressPluginAsPackageSet(File file, String fileName,
52
        OutputStream os) throws MakePluginPackageServiceException {
53
        List<File> files = new ArrayList<File>();
54
        List<String> fileNames = new ArrayList<String>();
55
        files.add(file);
56
        fileNames.add(fileName);
57
        compressPluginsAsPackageSet(files, fileNames, os);
58
    }
59

    
60
    public void compressPluginsAsPackageSet(File directory, OutputStream os)
61
        throws MakePluginPackageServiceException {
62
        File[] files = directory.listFiles();
63
        List<File> filesArray = new ArrayList<File>();
64
        List<String> fileNamesArray = new ArrayList<String>();
65
        for (int i = 0; i < files.length; i++) {
66
            filesArray.add(files[i]);
67
            fileNamesArray.add(files[i].getName());
68
        }
69
        compressPluginsAsPackageSet(filesArray, fileNamesArray, os);
70
    }
71

    
72
    public void compressPluginsAsPackageSet(List<File> files,
73
        List<String> fileNames, OutputStream os)
74
        throws MakePluginPackageServiceException {
75
        try {
76
            ZipOutputStream zos =
77
                new ZipOutputStream(new BufferedOutputStream(os));
78

    
79
            for (int i = 0; i < files.size(); i++) {
80
                ZipEntry zipEntry = new ZipEntry(fileNames.get(i));
81
                zos.putNextEntry(zipEntry);
82
                compressPluginFiles(files.get(i), zos);
83
                zos.closeEntry();
84
            }
85
            zos.close();
86
        } catch (Exception e) {
87
            throw new MakePluginPackageServiceException(
88
                "Error compressing as package set the plugin files: " + files,
89
                e);
90
        }
91
    }
92

    
93
    public void compressPluginAsPackage(File folder, OutputStream os)
94
        throws MakePluginPackageServiceException {
95
        ZipOutputStream zos = new ZipOutputStream(os);
96
        try {
97
            int parentFileLenght = folder.getParentFile().toString().length();
98
            compressPluginFile(folder, parentFileLenght, zos);
99
            zos.flush();
100
            zos.close();
101
        } catch (IOException e) {
102
            throw new MakePluginPackageServiceException(
103
                "Error compressing as package the plugin folder: " + folder, e);
104
        }
105
    }
106

    
107
    public void compressPluginAsPackageIndex(File folder, OutputStream os)
108
        throws MakePluginPackageServiceException {
109
        ZipOutputStream zos = new ZipOutputStream(os);
110
        try {
111
            int parentFileLength = folder.getParentFile().toString().length();
112
            String folderName =
113
                folder.toString()
114
                    .substring(parentFileLength + 1, folder.toString().length());
115
            
116
            File infoFile =
117
                new File(folder, getPackageInfoFileName() + ".index");
118

    
119
            byte[] buf = new byte[1024];
120
            int len;
121

    
122
            ZipEntry zipEntry =
123
                new ZipEntry(folderName + File.separator
124
                    + getPackageInfoFileName());
125
            zos.putNextEntry(zipEntry);
126

    
127
            FileInputStream fin = new FileInputStream(infoFile);
128
            BufferedInputStream in = new BufferedInputStream(fin);
129
            while ((len = in.read(buf)) >= 0) {
130
                zos.write(buf, 0, len);
131
            }
132
            in.close();
133
            zos.closeEntry();
134
            zos.flush();
135
            zos.close();
136
        } catch (IOException e) {
137
            throw new MakePluginPackageServiceException(
138
                "Error compressing as package index the plugin folder: "
139
                    + folder, e);
140
        }
141
    }
142

    
143
    private void compressPluginFiles(File fileOrFolder, ZipOutputStream zos)
144
        throws IOException {
145
        int parentFileLenght = fileOrFolder.getParentFile().toString().length();
146
        ZipOutputStream zosPlugin = new ZipOutputStream(zos);
147
        compressPluginFile(fileOrFolder, parentFileLenght, zosPlugin);
148
        zosPlugin.finish();
149
    }
150

    
151
    private void compressPluginFile(File file, int parenFileLength,
152
        ZipOutputStream zosPlugin) throws IOException {
153
        String fileName =
154
            file.toString()
155
                .substring(parenFileLength, file.toString().length());
156
        if (File.separatorChar != '/') {
157
            fileName = fileName.replace(File.separatorChar, '/');
158
        }
159

    
160
        if (file.isDirectory()) {
161
            // Remove the subversion folders
162
            if (!file.getName().toUpperCase().equals(".SVN")) {
163
                // Adding the files
164
                String[] fileNames = file.list();
165
                if (fileNames != null) {
166
                    for (int i = 0; i < fileNames.length; i++) {
167
                        compressPluginFile(new File(file, fileNames[i]),
168
                            parenFileLength, zosPlugin);
169
                    }
170
                }
171
            }
172
        } else {
173
            byte[] buf = new byte[1024];
174
            int len;
175

    
176
            ZipEntry zipEntry = new ZipEntry(fileName);
177
            zosPlugin.putNextEntry(zipEntry);
178

    
179
            FileInputStream fin = new FileInputStream(file);
180
            BufferedInputStream in = new BufferedInputStream(fin);
181
            while ((len = in.read(buf)) >= 0) {
182
                zosPlugin.write(buf, 0, len);
183
            }
184
            in.close();
185
            zosPlugin.closeEntry();
186
        }
187
    }
188

    
189
    private String getPackageInfoFileName() {
190
        return InstallerProviderLocator.getProviderManager()
191
            .getPackageInfoFileName();
192
    }
193
}