root / 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 @ 38600
History | View | Annotate | Download (6.05 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á</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 = new ZipOutputStream(new BufferedOutputStream( |
77 |
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: "
|
89 |
+ files, 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,
|
104 |
e); |
105 |
} |
106 |
} |
107 |
|
108 |
public void compressPluginAsPackageIndex(File folder, OutputStream os) |
109 |
throws MakePluginPackageServiceException {
|
110 |
ZipOutputStream zos = new ZipOutputStream(os); |
111 |
try {
|
112 |
int parentFileLength = folder.getParentFile().toString().length();
|
113 |
String folderName = folder.toString().substring(
|
114 |
parentFileLength + 1, folder.toString().length());
|
115 |
|
116 |
File infoFile = new File(folder, getPackageInfoFileName() |
117 |
+ ".index");
|
118 |
if (!infoFile.exists()) {
|
119 |
infoFile = new File(folder, getPackageInfoFileName()); |
120 |
} |
121 |
|
122 |
byte[] buf = new byte[1024]; |
123 |
int len;
|
124 |
|
125 |
// No usar File.separator hace cosas raras en G?indous
|
126 |
ZipEntry zipEntry = new ZipEntry(folderName + "/" + getPackageInfoFileName()); |
127 |
zos.putNextEntry(zipEntry); |
128 |
|
129 |
FileInputStream fin = new FileInputStream(infoFile); |
130 |
BufferedInputStream in = new BufferedInputStream(fin); |
131 |
while ((len = in.read(buf)) >= 0) { |
132 |
zos.write(buf, 0, len);
|
133 |
} |
134 |
in.close(); |
135 |
zos.closeEntry(); |
136 |
zos.flush(); |
137 |
zos.close(); |
138 |
} catch (IOException e) { |
139 |
throw new MakePluginPackageServiceException( |
140 |
"Error compressing as package index the plugin folder: "
|
141 |
+ folder, e); |
142 |
} |
143 |
} |
144 |
|
145 |
private void compressPluginFiles(File fileOrFolder, ZipOutputStream zos) |
146 |
throws IOException { |
147 |
int parentFileLenght = fileOrFolder.getParentFile().toString().length();
|
148 |
ZipOutputStream zosPlugin = new ZipOutputStream(zos); |
149 |
compressPluginFile(fileOrFolder, parentFileLenght, zosPlugin); |
150 |
zosPlugin.finish(); |
151 |
} |
152 |
|
153 |
private void compressPluginFile(File file, int parenFileLength, |
154 |
ZipOutputStream zosPlugin) throws IOException { |
155 |
String fileName = file.toString().substring(parenFileLength,
|
156 |
file.toString().length()); |
157 |
if (File.separatorChar != '/') { |
158 |
fileName = fileName.replace(File.separatorChar, '/'); |
159 |
} |
160 |
|
161 |
if (file.isDirectory()) {
|
162 |
// Remove the subversion folders
|
163 |
if (!file.getName().toUpperCase().equals(".SVN")) { |
164 |
// Adding the files
|
165 |
String[] fileNames = file.list(); |
166 |
if (fileNames != null) { |
167 |
for (int i = 0; i < fileNames.length; i++) { |
168 |
compressPluginFile(new File(file, fileNames[i]), |
169 |
parenFileLength, zosPlugin); |
170 |
} |
171 |
} |
172 |
} |
173 |
} else {
|
174 |
byte[] buf = new byte[1024]; |
175 |
int len;
|
176 |
|
177 |
ZipEntry zipEntry = new ZipEntry(fileName); |
178 |
zosPlugin.putNextEntry(zipEntry); |
179 |
|
180 |
FileInputStream fin = new FileInputStream(file); |
181 |
BufferedInputStream in = new BufferedInputStream(fin); |
182 |
while ((len = in.read(buf)) >= 0) { |
183 |
zosPlugin.write(buf, 0, len);
|
184 |
} |
185 |
in.close(); |
186 |
zosPlugin.closeEntry(); |
187 |
} |
188 |
} |
189 |
|
190 |
private String getPackageInfoFileName() { |
191 |
return InstallerProviderLocator.getProviderManager()
|
192 |
.getPackageInfoFileName(); |
193 |
} |
194 |
} |