View Javadoc

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  public class ZipUtil {
43  
44  	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  	 * @param outputFile
55  	 *            The zipped outputted file.
56  	 * @throws IOException
57  	 *             If something wrong happpens.
58  	 */
59  	public static void zipDirectory(File inputFile, File outputFile)
60  			throws IOException {
61  		logger.debug("zipDirectory(in:" + inputFile.toString() + ", out:"
62  				+ outputFile.toString() + ")");
63  		ZipOutputStream zipOutputStream = new ZipOutputStream(
64  				new FileOutputStream(outputFile));
65  		zipDirectory(inputFile, zipOutputStream);
66  		zipOutputStream.close();
67  	}
68  
69  	/***
70  	 * This method provides a simple zip compression for a file or an entire
71  	 * folder.
72  	 * 
73  	 * @param inputFile
74  	 *            The input file/folder whose content must be zipped.
75  	 * @param zipOutputStream
76  	 *            The target output stream that points to the location where the
77  	 *            zip file must be placed.
78  	 * @throws IOException
79  	 *             If something wrong happens.
80  	 */
81  	public static void zipDirectory(File inputFile,
82  			ZipOutputStream zipOutputStream) throws IOException {
83  		String[] dirList = inputFile.list();
84  		byte[] readBuffer = new byte[1024];
85  		int bytesIn = 0;
86  		for (int i = 0; i < dirList.length; i++) {
87  			File file = new File(inputFile, dirList[i]);
88  			if (file.isDirectory()) {
89  				// if the File object is a directory, call this
90  				// function again to add its content recursively
91  				zipDirectory(file, zipOutputStream);
92  				// loop again
93  				continue;
94  			}
95  			// if we reached here, the File object file was not a directory
96  			// create a FileInputStream on top of file
97  			FileInputStream fileInputStream = new FileInputStream(file);
98  			// create a new zip entry
99  			ZipEntry zipEntry = new ZipEntry(file.getPath());
100 
101 			// place the zip entry in the ZipOutputStream object
102 			zipOutputStream.putNextEntry(zipEntry);
103 			// now write the content of the file to the ZipOutputStream
104 			while ((bytesIn = fileInputStream.read(readBuffer)) != -1) {
105 				zipOutputStream.write(readBuffer, 0, bytesIn);
106 			}
107 			// close the Stream
108 			fileInputStream.close();
109 		}
110 	}
111 
112 	public static void zipAllFile(File[] inputFile, File outputFile)
113 			throws IOException {
114 		logger.debug("zipAllFile(in:" + inputFile.length + ", out:"
115 				+ outputFile.toString() + ")");
116 		ZipOutputStream zipOutputStream = new ZipOutputStream(
117 				new FileOutputStream(outputFile));
118 		zipAllFile(inputFile, zipOutputStream);
119 		zipOutputStream.close();
120 	}
121 
122 	public static void zipAllFile(File[] inputFile,
123 			ZipOutputStream zipOutputStream) throws IOException {
124 
125 		byte[] readBuffer = new byte[1024];
126 		int bytesIn = 0;
127 		for (int i = 0; i < inputFile.length; i++) {
128 			if (inputFile[i].isDirectory()) {
129 				// if the File object is a directory, call this
130 				// function again to add its content recursively
131 				zipDirectory(inputFile[i], zipOutputStream);
132 				// loop again
133 				continue;
134 			}
135 			// if we reached here, the File object file was not a directory
136 			// create a FileInputStream on top of file
137 			FileInputStream fileInputStream = new FileInputStream(inputFile[i]);
138 			// create a new zip entry
139 			ZipEntry zipEntry = new ZipEntry(inputFile[i].getPath());
140 
141 			// place the zip entry in the ZipOutputStream object
142 			zipOutputStream.putNextEntry(zipEntry);
143 			// now write the content of the file to the ZipOutputStream
144 			while ((bytesIn = fileInputStream.read(readBuffer)) != -1) {
145 				zipOutputStream.write(readBuffer, 0, bytesIn);
146 			}
147 			// close the Stream
148 			fileInputStream.close();
149 		}
150 	}
151 
152 	/***
153 	 * This method is a wrapper for the method the zipFile(File,
154 	 * ZipOutputStream). It provides a more easy interface to be used when must
155 	 * be zipped a file (not a folder).
156 	 * 
157 	 * @param inputFile
158 	 *            The input file (not a folder) to be zipped.
159 	 * @param outputFile
160 	 *            The zipped outputted file.
161 	 * @throws IOException
162 	 *             If something wrong happpens.
163 	 */
164 	public static void zipFile(File inputFile, File outputFile)
165 			throws IOException {
166 		logger.debug("zipFile(in:" + inputFile.toString() + ", out:"
167 				+ outputFile.toString() + ")");
168 		ZipOutputStream zipOutputStream = new ZipOutputStream(
169 				new FileOutputStream(outputFile));
170 		zipFile(inputFile, zipOutputStream);
171 		zipOutputStream.close();
172 	}
173 
174 	/***
175 	 * This method provides a simple zip compression for a file (not a folder).
176 	 * 
177 	 * @param inputFile
178 	 *            The file that must be zippede.
179 	 * @param zipOutputStream
180 	 *            The target output stream that points to the location where the
181 	 *            zip file must be placed.
182 	 * @throws IOException
183 	 *             If something wrong happens.
184 	 */
185 	public static void zipFile(File inputFile, ZipOutputStream zipOutputStream)
186 			throws IOException {
187 		// Create a buffer for reading the file
188 		byte[] buf = new byte[1024];
189 		zipOutputStream.putNextEntry(new ZipEntry(inputFile.getName()));
190 		FileInputStream fileInputStream = new FileInputStream(inputFile);
191 		// Transfer bytes from the file to the ZIP file
192 		int length;
193 		while ((length = fileInputStream.read(buf)) > 0) {
194 			zipOutputStream.write(buf, 0, length);
195 		}
196 		// close the input stream
197 		fileInputStream.close();
198 		// Complete the entry
199 		zipOutputStream.closeEntry();
200 	}
201 
202 	/***
203 	 * 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 		logger.debug("unZipFile(in:" + inputFile.toString() + ", out:"
217 				+ outputDirectory.toString() + ")");
218 		ZipFile zipFile = new ZipFile(inputFile);
219 		Enumeration zipEntries = zipFile.entries();
220 		while (zipEntries.hasMoreElements()) {
221 			ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement();
222 			logger.debug("Unpacking: " + zipEntry.getName());
223 
224 			File file = new File(outputDirectory, zipEntry.getName());
225 			if (zipEntry.isDirectory()) {
226 				file.mkdirs();
227 			} else {
228 				InputStream inputStream = zipFile.getInputStream(zipEntry);
229 				BufferedInputStream bis = new BufferedInputStream(inputStream);
230 				File dir = new File(file.getParent());
231 				dir.mkdirs();
232 				FileOutputStream fos = new FileOutputStream(file);
233 				BufferedOutputStream bos = new BufferedOutputStream(fos);
234 
235 				int readByte;
236 				while ((readByte = bis.read()) != -1) {
237 					bos.write((byte) readByte);
238 				}
239 				bos.close();
240 				fos.close();
241 			}
242 			logger.debug(zipEntry.getName() + " : Unpacked.");
243 		}
244 	}
245 }