svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / Compress.java @ 40560
History | View | Annotate | Download (6.09 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40560 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40560 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | /*
|
||
25 | * AUTHORS (In addition to CIT):
|
||
26 | * 2010 {Prodevelop} {Task}
|
||
27 | */
|
||
28 | |||
29 | package org.gvsig.installer.lib.impl.utils; |
||
30 | |||
31 | import java.io.BufferedInputStream; |
||
32 | import java.io.BufferedOutputStream; |
||
33 | import java.io.File; |
||
34 | import java.io.FileInputStream; |
||
35 | import java.io.IOException; |
||
36 | import java.io.OutputStream; |
||
37 | import java.util.ArrayList; |
||
38 | import java.util.List; |
||
39 | import java.util.zip.ZipEntry; |
||
40 | import java.util.zip.ZipOutputStream; |
||
41 | |||
42 | import org.gvsig.installer.lib.api.creation.MakePluginPackageServiceException; |
||
43 | import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
||
44 | |||
45 | /**
|
||
46 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
||
47 | */
|
||
48 | public class Compress { |
||
49 | |||
50 | // private static final int BUFFER = 2048;
|
||
51 | |||
52 | public void compressPluginAsPackageSet(File file, String fileName, |
||
53 | OutputStream os) throws MakePluginPackageServiceException { |
||
54 | List<File> files = new ArrayList<File>(); |
||
55 | List<String> fileNames = new ArrayList<String>(); |
||
56 | files.add(file); |
||
57 | fileNames.add(fileName); |
||
58 | compressPluginsAsPackageSet(files, fileNames, os); |
||
59 | } |
||
60 | |||
61 | public void compressPluginsAsPackageSet(File directory, OutputStream os) |
||
62 | throws MakePluginPackageServiceException {
|
||
63 | File[] files = directory.listFiles(); |
||
64 | List<File> filesArray = new ArrayList<File>(); |
||
65 | List<String> fileNamesArray = new ArrayList<String>(); |
||
66 | for (int i = 0; i < files.length; i++) { |
||
67 | filesArray.add(files[i]); |
||
68 | fileNamesArray.add(files[i].getName()); |
||
69 | } |
||
70 | compressPluginsAsPackageSet(filesArray, fileNamesArray, os); |
||
71 | } |
||
72 | |||
73 | public void compressPluginsAsPackageSet(List<File> files, |
||
74 | List<String> fileNames, OutputStream os) |
||
75 | throws MakePluginPackageServiceException {
|
||
76 | try {
|
||
77 | ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream( |
||
78 | os)); |
||
79 | |||
80 | for (int i = 0; i < files.size(); i++) { |
||
81 | ZipEntry zipEntry = new ZipEntry(fileNames.get(i)); |
||
82 | zos.putNextEntry(zipEntry); |
||
83 | compressPluginFiles(files.get(i), zos); |
||
84 | zos.closeEntry(); |
||
85 | } |
||
86 | zos.close(); |
||
87 | } catch (Exception e) { |
||
88 | throw new MakePluginPackageServiceException( |
||
89 | "Error compressing as package set the plugin files: "
|
||
90 | + files, e); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | public void compressPluginAsPackage(File folder, OutputStream os) |
||
95 | throws MakePluginPackageServiceException {
|
||
96 | ZipOutputStream zos = new ZipOutputStream(os); |
||
97 | try {
|
||
98 | int parentFileLenght = folder.getParentFile().toString().length();
|
||
99 | compressPluginFile(folder, parentFileLenght, zos); |
||
100 | zos.flush(); |
||
101 | zos.close(); |
||
102 | } catch (IOException e) { |
||
103 | throw new MakePluginPackageServiceException( |
||
104 | "Error compressing as package the plugin folder: " + folder,
|
||
105 | e); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | public void compressPluginAsPackageIndex(File folder, OutputStream os) |
||
110 | throws MakePluginPackageServiceException {
|
||
111 | ZipOutputStream zos = new ZipOutputStream(os); |
||
112 | try {
|
||
113 | int parentFileLength = folder.getParentFile().toString().length();
|
||
114 | String folderName = folder.toString().substring(
|
||
115 | parentFileLength + 1, folder.toString().length());
|
||
116 | |||
117 | File infoFile = new File(folder, getPackageInfoFileName() |
||
118 | + ".index");
|
||
119 | if (!infoFile.exists()) {
|
||
120 | infoFile = new File(folder, getPackageInfoFileName()); |
||
121 | } |
||
122 | |||
123 | byte[] buf = new byte[1024]; |
||
124 | int len;
|
||
125 | |||
126 | // No usar File.separator hace cosas raras en G?indous
|
||
127 | ZipEntry zipEntry = new ZipEntry(folderName + "/" + getPackageInfoFileName()); |
||
128 | zos.putNextEntry(zipEntry); |
||
129 | |||
130 | FileInputStream fin = new FileInputStream(infoFile); |
||
131 | BufferedInputStream in = new BufferedInputStream(fin); |
||
132 | while ((len = in.read(buf)) >= 0) { |
||
133 | zos.write(buf, 0, len);
|
||
134 | } |
||
135 | in.close(); |
||
136 | zos.closeEntry(); |
||
137 | zos.flush(); |
||
138 | zos.close(); |
||
139 | } catch (IOException e) { |
||
140 | throw new MakePluginPackageServiceException( |
||
141 | "Error compressing as package index the plugin folder: "
|
||
142 | + folder, e); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | private void compressPluginFiles(File fileOrFolder, ZipOutputStream zos) |
||
147 | throws IOException { |
||
148 | int parentFileLenght = fileOrFolder.getParentFile().toString().length();
|
||
149 | ZipOutputStream zosPlugin = new ZipOutputStream(zos); |
||
150 | compressPluginFile(fileOrFolder, parentFileLenght, zosPlugin); |
||
151 | zosPlugin.finish(); |
||
152 | } |
||
153 | |||
154 | private void compressPluginFile(File file, int parenFileLength, |
||
155 | ZipOutputStream zosPlugin) throws IOException { |
||
156 | String fileName = file.toString().substring(parenFileLength,
|
||
157 | file.toString().length()); |
||
158 | if (File.separatorChar != '/') { |
||
159 | fileName = fileName.replace(File.separatorChar, '/'); |
||
160 | } |
||
161 | |||
162 | if (file.isDirectory()) {
|
||
163 | // Remove the subversion folders
|
||
164 | if (!file.getName().toUpperCase().equals(".SVN")) { |
||
165 | // Adding the files
|
||
166 | String[] fileNames = file.list(); |
||
167 | if (fileNames != null) { |
||
168 | for (int i = 0; i < fileNames.length; i++) { |
||
169 | compressPluginFile(new File(file, fileNames[i]), |
||
170 | parenFileLength, zosPlugin); |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | } else {
|
||
175 | byte[] buf = new byte[1024]; |
||
176 | int len;
|
||
177 | |||
178 | ZipEntry zipEntry = new ZipEntry(fileName); |
||
179 | zosPlugin.putNextEntry(zipEntry); |
||
180 | |||
181 | FileInputStream fin = new FileInputStream(file); |
||
182 | BufferedInputStream in = new BufferedInputStream(fin); |
||
183 | while ((len = in.read(buf)) >= 0) { |
||
184 | zosPlugin.write(buf, 0, len);
|
||
185 | } |
||
186 | in.close(); |
||
187 | zosPlugin.closeEntry(); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | private String getPackageInfoFileName() { |
||
192 | return InstallerProviderLocator.getProviderManager()
|
||
193 | .getPackageInfoFileName(); |
||
194 | } |
||
195 | } |