001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2003, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */package org.geotools.graph.util;
017:
018: import java.io.BufferedInputStream;
019: import java.io.BufferedOutputStream;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.util.Collection;
025: import java.util.Enumeration;
026: import java.util.zip.ZipEntry;
027: import java.util.zip.ZipFile;
028: import java.util.zip.ZipOutputStream;
029:
030: public class ZipUtil {
031:
032: public static void zip(String zipFilename, String[] filenames)
033: throws IOException {
034: zip(zipFilename, filenames, filenames);
035: }
036:
037: public static void zip(String zipFilename, String[] filenames,
038: String[] archFilenames) throws IOException {
039:
040: ZipOutputStream zout = new ZipOutputStream(
041: new BufferedOutputStream(new FileOutputStream(
042: zipFilename)));
043:
044: byte[] data = new byte[512];
045: int bc;
046: for (int i = 0; i < filenames.length; i++) {
047: InputStream fin = new BufferedInputStream(
048: new FileInputStream(filenames[i]));
049:
050: ZipEntry entry = new ZipEntry(StringUtil
051: .stripPath(archFilenames[i]));
052: zout.putNextEntry(entry);
053:
054: while ((bc = fin.read(data, 0, 512)) != -1) {
055: zout.write(data, 0, bc);
056: }
057: zout.flush();
058: }
059:
060: zout.close();
061: }
062:
063: public static void unzip(String zipFilename, Collection filenames,
064: String outdir) throws IOException {
065: unzip(zipFilename, (String[]) filenames
066: .toArray(new String[filenames.size()]), outdir);
067: }
068:
069: public static void unzip(String zipFilename, String[] filenames,
070: String outdir) throws IOException {
071:
072: ZipFile zipFile = new ZipFile(zipFilename);
073: Enumeration entries = zipFile.entries();
074:
075: L1: while (entries.hasMoreElements()) {
076: ZipEntry entry = (ZipEntry) entries.nextElement();
077:
078: for (int i = 0; i < filenames.length; i++) {
079: if (entry.getName().equals(filenames[i])) {
080: byte[] buffer = new byte[1024];
081: int len;
082:
083: InputStream zipin = zipFile.getInputStream(entry);
084: BufferedOutputStream fileout = new BufferedOutputStream(
085: new FileOutputStream(outdir + "\\"
086: + filenames[i]));
087:
088: while ((len = zipin.read(buffer)) >= 0)
089: fileout.write(buffer, 0, len);
090:
091: zipin.close();
092: fileout.flush();
093: fileout.close();
094:
095: continue L1;
096: }
097: }
098: }
099: }
100:
101: public static void unzip(String zipFilename, String outdir)
102: throws IOException {
103: ZipFile zipFile = new ZipFile(zipFilename);
104: Enumeration entries = zipFile.entries();
105:
106: while (entries.hasMoreElements()) {
107: ZipEntry entry = (ZipEntry) entries.nextElement();
108: byte[] buffer = new byte[1024];
109: int len;
110:
111: InputStream zipin = zipFile.getInputStream(entry);
112: BufferedOutputStream fileout = new BufferedOutputStream(
113: new FileOutputStream(outdir + "\\"
114: + entry.getName()));
115:
116: while ((len = zipin.read(buffer)) >= 0)
117: fileout.write(buffer, 0, len);
118:
119: zipin.close();
120: fileout.flush();
121: fileout.close();
122: }
123: }
124:
125: }
|