001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: ZipUtil.java$
021: * $FileID: 4327$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:23 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.util;
032:
033: /**
034: * Title: SourceJammer Library 1.0
035: * Description:
036: * Copyright: Copyright (c) 2001
037: * Company: SourceJammer Project
038: * @author Robert MacGrogan
039: * @version 1.0
040: */
041:
042: import java.util.HashMap;
043: import java.util.zip.*;
044: import java.io.ByteArrayOutputStream;
045: import java.io.IOException;
046: import java.io.*;
047:
048: /**
049: * Static utility methods for zipping data.
050: */
051: public class ZipUtil {
052:
053: public static final int ZIP_BUFFER_SIZE = 50;
054: private static final int STREAM_BUFFER_SIZE = 1024;
055:
056: private ZipUtil() {
057: }
058:
059: /**
060: * Deflates the file and returns the deflated file.
061: */
062: public static byte[] zipByteArray(byte[] file) throws IOException {
063: byte[] byReturn = null;
064: Deflater oDeflate = new Deflater(Deflater.DEFLATED, false);
065: oDeflate.setInput(file);
066: oDeflate.finish();
067: ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
068: try {
069: while (!oDeflate.finished()) {
070: byte[] byRead = new byte[ZIP_BUFFER_SIZE];
071: int iBytesRead = oDeflate.deflate(byRead);
072: if (iBytesRead == byRead.length) {
073: oZipStream.write(byRead);
074: } else {
075: oZipStream.write(byRead, 0, iBytesRead);
076: }
077: }
078: oDeflate.end();
079: byReturn = oZipStream.toByteArray();
080: } finally {
081: oZipStream.close();
082: }
083: return byReturn;
084: }
085:
086: /**
087: * Inflates a previously deflated file.
088: */
089: public static byte[] unzipByteArray(byte[] file) throws IOException {
090: byte[] byReturn = null;
091:
092: Inflater oInflate = new Inflater(false);
093: oInflate.setInput(file);
094:
095: ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
096: try {
097: while (!oInflate.finished()) {
098: byte[] byRead = new byte[ZIP_BUFFER_SIZE];
099: int iBytesRead = oInflate.inflate(byRead);
100: if (iBytesRead == byRead.length) {
101: oZipStream.write(byRead);
102: } else {
103: oZipStream.write(byRead, 0, iBytesRead);
104: }
105: }
106: byReturn = oZipStream.toByteArray();
107: } catch (DataFormatException ex) {
108: throw new IOException(
109: "Attempting to unzip file that is not zipped.");
110: } finally {
111: oZipStream.close();
112: }
113: return byReturn;
114: }
115:
116: /*
117: public static void zipFileToFile(File flSource, File flTarget)
118: throws IOException{
119: Deflater oDeflate = new Deflater(Deflater.DEFLATED, false);
120:
121: FileInputStream stmFileIn = new FileInputStream(flSource);
122: FileOutputStream stmFileOut = new FileOutputStream(flTarget);
123: DeflaterOutputStream stmDeflateOut = new DeflaterOutputStream(stmFileOut, oDeflate);
124: byte[] buffer = null;
125: int iBufferSize = STREAM_BUFFER_SIZE;
126:
127: boolean bKeepZipping = true;
128: try{
129: while (bKeepZipping){
130: buffer = new byte[iBufferSize];
131: int iBytes = stmFileIn.read(buffer);
132: if (iBytes == -1){
133: bKeepZipping = false;
134: }
135: else{
136: if (iBytes < iBufferSize){
137: bKeepZipping = false;
138: byte[] tmp = new byte[iBytes];
139: for (int i = 0; i < iBytes; i++){
140: tmp[i] = buffer[i];
141: }
142: buffer = tmp;
143: }
144: stmDeflateOut.write(buffer);
145: }//end else some bytes returned.
146: }//end while
147: }//end try
148: finally{
149: stmDeflateOut.finish();
150: stmDeflateOut.flush();
151: stmDeflateOut.close();
152: stmFileOut.close();
153: stmFileIn.close();
154: }
155: }
156: */
157:
158: public static void zipFileToFile(File flSource, File flTarget)
159: throws IOException {
160: Deflater oDeflate = new Deflater(Deflater.DEFLATED, false);
161:
162: FileInputStream stmFileIn = new FileInputStream(flSource);
163: FileOutputStream stmFileOut = new FileOutputStream(flTarget);
164: DeflaterOutputStream stmDeflateOut = new DeflaterOutputStream(
165: stmFileOut, oDeflate);
166: try {
167: FileUtil
168: .inputStreamToOutputStream(stmFileIn, stmDeflateOut);
169: }//end try
170: finally {
171: stmDeflateOut.finish();
172: stmDeflateOut.flush();
173: stmDeflateOut.close();
174: stmFileOut.close();
175: stmFileIn.close();
176: }
177: }
178:
179: public static void unzipFileToFile(File flSource, File flTarget)
180: throws IOException {
181: Inflater oInflate = new Inflater(false);
182: FileInputStream stmFileIn = new FileInputStream(flSource);
183: FileOutputStream stmFileOut = new FileOutputStream(flTarget);
184: InflaterInputStream stmInflateIn = new InflaterInputStream(
185: stmFileIn, oInflate);
186:
187: try {
188: inflaterInputStmToFileOutputStm(stmInflateIn, stmFileOut);
189: }//end try
190: finally {
191: stmFileOut.flush();
192: stmFileOut.close();
193: stmInflateIn.close();
194: stmFileIn.close();
195: }
196: }
197:
198: private static void inflaterInputStmToFileOutputStm(
199: InflaterInputStream stmIn, FileOutputStream stmOut)
200: throws IOException {
201: byte[] buffer = null;
202: int iBufferSize = STREAM_BUFFER_SIZE;
203:
204: boolean bKeepStreaming = true;
205: while (bKeepStreaming) {
206: buffer = new byte[iBufferSize];
207: int iBytes = stmIn.read(buffer);
208: if (iBytes == -1) {
209: bKeepStreaming = false;
210: } else {
211: if (iBytes < iBufferSize) {
212: bKeepStreaming = false;
213: byte[] tmp = new byte[iBytes];
214: for (int i = 0; i < iBytes; i++) {
215: tmp[i] = buffer[i];
216: }
217: buffer = tmp;
218: }
219: stmOut.write(buffer);
220: //Override above test if available returns 1
221: if (stmIn.available() == 1) {
222: bKeepStreaming = true;
223: }
224: }//end else some bytes returned.
225: }//end while
226: }
227:
228: /**
229: * Checks passed-in file name against list of extensions not to zip. Returns
230: * true if no matches, false if a match to file extension is found.
231: */
232: public static boolean canZip(String fileName) {
233: boolean bCanZip = true;
234: int iLastDot = fileName.lastIndexOf(".");
235: String sExtension = fileName.substring(iLastDot + 1);
236: HashMap map = AppConfig.getInstance().getDontZipExtensions();
237: if (map.containsKey(sExtension)) {
238: bCanZip = false;
239: }
240: return bCanZip;
241: }
242:
243: public static void main(String[] args) {
244: try {
245: File flSource = new File(args[0]);
246: File flTarget = new File(args[1]);
247: System.out.println("Unzipping file");
248:
249: unZipZipFileToLocation(flSource, flTarget);
250: System.out.println("Done");
251:
252: } catch (Exception ex) {
253: ex.printStackTrace();
254: }
255: }
256:
257: public static void unZipZipFileToLocation(File zipFile,
258: File targetDir) throws IOException {
259: if (!targetDir.isDirectory()) {
260: throw new BadMethodArgumentException(
261: "Target is not a directory.");
262: }
263: FileInputStream flInStr = new FileInputStream(zipFile);
264: try {
265: ZipInputStream zis = new ZipInputStream(flInStr);
266: try {
267: ZipEntry entry = null;
268: while ((entry = zis.getNextEntry()) != null) {
269: String name = entry.getName();
270: File newFile = new File(targetDir, name);
271: if (entry.isDirectory() && !newFile.exists()) {
272: newFile.mkdirs();
273: } else if (!entry.isDirectory()) {
274: if (newFile.exists()) {
275: newFile.delete();
276: }
277: File parentDir = newFile.getParentFile();
278: if (!parentDir.exists()) {
279: parentDir.mkdirs();
280: }
281: FileOutputStream stmOut = new FileOutputStream(
282: newFile);
283: try {
284: simpleInputStreamToOutputStream(zis, stmOut);
285: } finally {
286: stmOut.close();
287: }
288: }
289: }//end while.
290: } finally {
291: zis.close();
292: }
293: } finally {
294: flInStr.close();
295: }
296: }
297:
298: private static void simpleInputStreamToOutputStream(
299: InputStream stmIn, OutputStream stmOut) throws IOException {
300:
301: byte[] buffer = null;
302: int iBufferSize = 4096;
303: buffer = new byte[iBufferSize];
304:
305: boolean bKeepStreaming = true;
306: while (bKeepStreaming) {
307: int iBytes = stmIn.read(buffer);
308: if (iBytes == -1) {
309: bKeepStreaming = false;
310: } else {
311: stmOut.write(buffer, 0, iBytes);
312: }//end else some bytes returned.
313: }//end while
314: }
315:
316: }
|