001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine.util;
034:
035: import java.io.BufferedInputStream;
036: import java.io.BufferedOutputStream;
037: import java.io.File;
038: import java.io.FileInputStream;
039: import java.io.FileOutputStream;
040: import java.io.InputStream;
041:
042: import java.util.Enumeration;
043: import java.util.Vector;
044: import java.util.zip.Deflater;
045: import java.util.zip.ZipEntry;
046: import java.util.zip.ZipFile;
047: import java.util.zip.ZipInputStream;
048: import java.util.zip.ZipOutputStream;
049:
050: public class ZipUtil {
051: static final int BUFFER = 2048;
052:
053: public static void unzip(String inFileName,
054: String destinationDirectory) throws Exception {
055: File sourceZipFile = new File(inFileName);
056: File unzipDestinationDirectory = new File(destinationDirectory);
057: ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
058: Enumeration zipFileEntries = zipFile.entries();
059:
060: while (zipFileEntries.hasMoreElements()) {
061: ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
062: String currentEntry = entry.getName();
063: File destFile = new File(unzipDestinationDirectory,
064: currentEntry);
065: File destinationParent = destFile.getParentFile();
066: destinationParent.mkdirs();
067:
068: if (entry.isDirectory()) {
069: destFile.mkdirs();
070: } else {
071: BufferedInputStream is = new BufferedInputStream(
072: zipFile.getInputStream(entry));
073: int currentByte;
074: byte[] data = new byte[BUFFER];
075: FileOutputStream fos = new FileOutputStream(destFile);
076: BufferedOutputStream dest = new BufferedOutputStream(
077: fos, BUFFER);
078:
079: while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
080: dest.write(data, 0, currentByte);
081: }
082:
083: dest.flush();
084: dest.close();
085: is.close();
086: }
087: }
088:
089: zipFile.close();
090: }
091:
092: public static void unzip(ZipInputStream zin,
093: String destinationDirectory) throws Exception {
094: File unzipDestinationDirectory = new File(destinationDirectory);
095: ZipEntry entry = null;
096:
097: while ((entry = zin.getNextEntry()) != null) {
098: String currentEntry = entry.getName();
099: File destFile = new File(unzipDestinationDirectory,
100: currentEntry);
101: File destinationParent = destFile.getParentFile();
102: destinationParent.mkdirs();
103:
104: if (entry.isDirectory()) {
105: destFile.mkdirs();
106: } else {
107: FileOutputStream out = new FileOutputStream(destFile);
108: byte[] b = new byte[BUFFER];
109: int len = 0;
110:
111: while ((len = zin.read(b)) != -1) {
112: out.write(b, 0, len);
113: }
114:
115: out.close();
116: }
117: }
118: }
119:
120: private static Object[] getContent(String dir) {
121: Vector v = new Vector();
122: FileUtils.walk(dir, v);
123:
124: return v.toArray();
125: }
126:
127: public static void zip(ZipOutputStream zos, String dir)
128: throws Exception {
129: File d = new File(dir);
130: zos.setMethod(ZipOutputStream.DEFLATED); //indicate deflated
131: zos.setLevel(Deflater.DEFAULT_COMPRESSION);
132:
133: Object[] contents = getContent(d.getPath());
134:
135: for (int i = 0; i < contents.length; i++) {
136: if (d.getPath().equals((String) contents[i])) {
137: continue;
138: }
139:
140: String name = ((String) contents[i]).substring(d.getPath()
141: .length() + 1);
142: File fin = new File((String) contents[i]);
143:
144: if (fin.isDirectory()) {
145: ZipEntry ze = new ZipEntry(name + '/');
146: zos.putNextEntry(ze);
147: }
148:
149: if (fin.isFile()) {
150: ZipEntry ze = new ZipEntry(name);
151: zos.putNextEntry(ze);
152:
153: InputStream ins = new FileInputStream(fin);
154: int bread;
155: byte[] bin = new byte[BUFFER];
156:
157: while ((bread = ins.read(bin, 0, BUFFER)) != -1) {
158: zos.write(bin, 0, bread);
159: }
160: }
161: }
162:
163: zos.flush();
164: }
165: }
|