Line | Hits | Source |
---|---|---|
1 | /******************************************************************************* | |
2 | *InternetCafe is a software solution that helps the management of Cybercafes | |
3 | * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005. | |
4 | * Copyright (C) 2006 Guido Angelo Ingenito | |
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, MA 02110-1301, USA. | |
19 | *******************************************************************************/ | |
20 | package base.util; | |
21 | ||
22 | import java.io.BufferedInputStream; | |
23 | import java.io.BufferedOutputStream; | |
24 | import java.io.File; | |
25 | import java.io.FileInputStream; | |
26 | import java.io.FileOutputStream; | |
27 | import java.io.IOException; | |
28 | import java.io.InputStream; | |
29 | import java.util.Enumeration; | |
30 | import java.util.zip.ZipEntry; | |
31 | import java.util.zip.ZipFile; | |
32 | import java.util.zip.ZipOutputStream; | |
33 | ||
34 | import org.apache.log4j.Logger; | |
35 | ||
36 | /** | |
37 | * This class provides some basic methods to compress/uncompress files and | |
38 | * folders in zip compression format. | |
39 | * | |
40 | * @author Guido Angelo Ingenito | |
41 | */ | |
42 | 3 | public class ZipUtil { |
43 | ||
44 | 3 | private static final transient Logger logger = Logger |
45 | .getLogger(ZipUtil.class.getName()); | |
46 | ||
47 | /** | |
48 | * This method is a wrapper for the method the zipDirectory(File, | |
49 | * ZipOutputStream). It provides a more easy interface to be used when must | |
50 | * be zipped a file or a folder. | |
51 | * | |
52 | * @param inputFile | |
53 | * The input file/folder to be zipped. | |
54 | 3 | * @param outputFile |
55 | 3 | * The zipped outputted file. |
56 | 3 | * @throws IOException |
57 | 3 | * If something wrong happpens. |
58 | 3 | */ |
59 | public static void zipDirectory(File inputFile, File outputFile) | |
60 | throws IOException { | |
61 | 0 | logger.debug("zipDirectory(in:" + inputFile.toString() + ", out:" |
62 | + outputFile.toString() + ")"); | |
63 | 0 | ZipOutputStream zipOutputStream = new ZipOutputStream( |
64 | new FileOutputStream(outputFile)); | |
65 | 0 | zipDirectory(inputFile, zipOutputStream); |
66 | 0 | zipOutputStream.close(); |
67 | 3 | } |
68 | 3 | |
69 | 3 | /** |
70 | 303 | * This method provides a simple zip compression for a file or an entire |
71 | 300 | * folder. |
72 | 300 | * |
73 | * @param inputFile | |
74 | * The input file/folder whose content must be zipped. | |
75 | 0 | * @param zipOutputStream |
76 | * The target output stream that points to the location where the | |
77 | 0 | * zip file must be placed. |
78 | * @throws IOException | |
79 | * If something wrong happens. | |
80 | */ | |
81 | 300 | public static void zipDirectory(File inputFile, |
82 | ZipOutputStream zipOutputStream) throws IOException { | |
83 | 300 | String[] dirList = inputFile.list(); |
84 | 0 | byte[] readBuffer = new byte[1024]; |
85 | 0 | int bytesIn = 0; |
86 | 300 | for (int i = 0; i < dirList.length; i++) { |
87 | 0 | File file = new File(inputFile, dirList[i]); |
88 | 2100 | if (file.isDirectory()) { |
89 | 1500 | // if the File object is a directory, call this |
90 | 0 | // function again to add its content recursively |
91 | 0 | zipDirectory(file, zipOutputStream); |
92 | 300 | // loop again |
93 | 0 | continue; |
94 | 3 | } |
95 | // if we reached here, the File object file was not a directory | |
96 | // create a FileInputStream on top of file | |
97 | 0 | FileInputStream fileInputStream = new FileInputStream(file); |
98 | 0 | // create a new zip entry |
99 | 0 | ZipEntry zipEntry = new ZipEntry(file.getPath()); |
100 | 0 | |
101 | 0 | // place the zip entry in the ZipOutputStream object |
102 | 0 | zipOutputStream.putNextEntry(zipEntry); |
103 | // now write the content of the file to the ZipOutputStream | |
104 | 0 | while ((bytesIn = fileInputStream.read(readBuffer)) != -1) { |
105 | 0 | zipOutputStream.write(readBuffer, 0, bytesIn); |
106 | 0 | } |
107 | 0 | // close the Stream |
108 | 0 | fileInputStream.close(); |
109 | } | |
110 | 0 | } |
111 | 0 | |
112 | public static void zipAllFile(File[] inputFile, File outputFile) | |
113 | 0 | throws IOException { |
114 | 0 | logger.debug("zipAllFile(in:" + inputFile.length + ", out:" |
115 | + outputFile.toString() + ")"); | |
116 | 0 | ZipOutputStream zipOutputStream = new ZipOutputStream( |
117 | 0 | new FileOutputStream(outputFile)); |
118 | 0 | zipAllFile(inputFile, zipOutputStream); |
119 | 0 | zipOutputStream.close(); |
120 | 0 | } |
121 | ||
122 | 0 | public static void zipAllFile(File[] inputFile, |
123 | ZipOutputStream zipOutputStream) throws IOException { | |
124 | 0 | |
125 | 0 | byte[] readBuffer = new byte[1024]; |
126 | 0 | int bytesIn = 0; |
127 | 0 | for (int i = 0; i < inputFile.length; i++) { |
128 | 0 | if (inputFile[i].isDirectory()) { |
129 | // if the File object is a directory, call this | |
130 | 0 | // function again to add its content recursively |
131 | 0 | zipDirectory(inputFile[i], zipOutputStream); |
132 | // loop again | |
133 | 0 | continue; |
134 | } | |
135 | // if we reached here, the File object file was not a directory | |
136 | // create a FileInputStream on top of file | |
137 | 0 | FileInputStream fileInputStream = new FileInputStream(inputFile[i]); |
138 | // create a new zip entry | |
139 | 0 | ZipEntry zipEntry = new ZipEntry(inputFile[i].getPath()); |
140 | 0 | |
141 | 0 | // place the zip entry in the ZipOutputStream object |
142 | 0 | zipOutputStream.putNextEntry(zipEntry); |
143 | 0 | // now write the content of the file to the ZipOutputStream |
144 | 0 | while ((bytesIn = fileInputStream.read(readBuffer)) != -1) { |
145 | 0 | zipOutputStream.write(readBuffer, 0, bytesIn); |
146 | 0 | } |
147 | // close the Stream | |
148 | 0 | fileInputStream.close(); |
149 | } | |
150 | 0 | } |
151 | ||
152 | /** | |
153 | * This method is a wrapper for the method the zipFile(File, | |
154 | 0 | * ZipOutputStream). It provides a more easy interface to be used when must |
155 | 0 | * be zipped a file (not a folder). |
156 | 0 | * |
157 | * @param inputFile | |
158 | * The input file (not a folder) to be zipped. | |
159 | 0 | * @param outputFile |
160 | 0 | * The zipped outputted file. |
161 | 0 | * @throws IOException |
162 | * If something wrong happpens. | |
163 | 0 | */ |
164 | public static void zipFile(File inputFile, File outputFile) | |
165 | 0 | throws IOException { |
166 | 0 | logger.debug("zipFile(in:" + inputFile.toString() + ", out:" |
167 | + outputFile.toString() + ")"); | |
168 | 0 | ZipOutputStream zipOutputStream = new ZipOutputStream( |
169 | new FileOutputStream(outputFile)); | |
170 | 0 | zipFile(inputFile, zipOutputStream); |
171 | 0 | zipOutputStream.close(); |
172 | 0 | } |
173 | ||
174 | /** | |
175 | * This method provides a simple zip compression for a file (not a folder). | |
176 | 0 | * |
177 | 0 | * @param inputFile |
178 | 0 | * The file that must be zippede. |
179 | 0 | * @param zipOutputStream |
180 | 0 | * The target output stream that points to the location where the |
181 | 0 | * zip file must be placed. |
182 | * @throws IOException | |
183 | 0 | * If something wrong happens. |
184 | 0 | */ |
185 | 0 | public static void zipFile(File inputFile, ZipOutputStream zipOutputStream) |
186 | 0 | throws IOException { |
187 | 0 | // Create a buffer for reading the file |
188 | 0 | byte[] buf = new byte[1024]; |
189 | 0 | zipOutputStream.putNextEntry(new ZipEntry(inputFile.getName())); |
190 | 0 | FileInputStream fileInputStream = new FileInputStream(inputFile); |
191 | 0 | // Transfer bytes from the file to the ZIP file |
192 | 0 | int length; |
193 | 0 | while ((length = fileInputStream.read(buf)) > 0) { |
194 | 0 | zipOutputStream.write(buf, 0, length); |
195 | 0 | } |
196 | 0 | // close the input stream |
197 | 0 | fileInputStream.close(); |
198 | 0 | // Complete the entry |
199 | 0 | zipOutputStream.closeEntry(); |
200 | 0 | } |
201 | 0 | |
202 | 0 | /** |
203 | 0 | * This method provides a simple unzip facility for a composite zip file (a |
204 | * zip file that contains files and folders). | |
205 | * | |
206 | * @param inputFile | |
207 | * The zip file that must be extracted. | |
208 | * @param outputDirectory | |
209 | * The destination folder where the content of the input zipped | |
210 | * file must be placed. | |
211 | * @throws IOException | |
212 | * If something wrong happens. | |
213 | */ | |
214 | public static void unZipFile(File inputFile, File outputDirectory) | |
215 | throws IOException { | |
216 | 0 | logger.debug("unZipFile(in:" + inputFile.toString() + ", out:" |
217 | + outputDirectory.toString() + ")"); | |
218 | 0 | ZipFile zipFile = new ZipFile(inputFile); |
219 | 0 | Enumeration zipEntries = zipFile.entries(); |
220 | 0 | while (zipEntries.hasMoreElements()) { |
221 | 0 | ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement(); |
222 | 0 | logger.debug("Unpacking: " + zipEntry.getName()); |
223 | ||
224 | 0 | File file = new File(outputDirectory, zipEntry.getName()); |
225 | 0 | if (zipEntry.isDirectory()) { |
226 | 0 | file.mkdirs(); |
227 | 0 | } else { |
228 | 0 | InputStream inputStream = zipFile.getInputStream(zipEntry); |
229 | 0 | BufferedInputStream bis = new BufferedInputStream(inputStream); |
230 | 0 | File dir = new File(file.getParent()); |
231 | 0 | dir.mkdirs(); |
232 | 0 | FileOutputStream fos = new FileOutputStream(file); |
233 | 0 | BufferedOutputStream bos = new BufferedOutputStream(fos); |
234 | ||
235 | int readByte; | |
236 | 0 | while ((readByte = bis.read()) != -1) { |
237 | 0 | bos.write((byte) readByte); |
238 | 0 | } |
239 | 0 | bos.close(); |
240 | 0 | fos.close(); |
241 | } | |
242 | 0 | logger.debug(zipEntry.getName() + " : Unpacked."); |
243 | 0 | } |
244 | 0 | } |
245 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |