/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.helper;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void zip(String zipFileName, String inputFile) throws IOException {
zip(zipFileName, new File(inputFile));
}
public static void zip(String zipFileName, File inputFile) throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
out.close();
}
public static void zip(String inputFile, OutputStream output) throws IOException {
ZipOutputStream out = new ZipOutputStream(output);
zip(out, new File(inputFile), "");
out.close();
}
/**
* Unzip a zip file to a direcotry
*
* @param zipFileName zip file name, like c:\test.zip
* @param outputDirectory output dir
* @throws IOException
*/
public static void unzip(String zipFileName, String outputDirectory) throws IOException {
unzip(new FileInputStream(zipFileName), outputDirectory);
}
public static void unzip(InputStream input, String outputDirectory) throws IOException {
File outDir = new File(outputDirectory);
if (!outDir.exists()) {
outDir.mkdirs();
}
ZipInputStream in = new ZipInputStream(input);
ZipEntry z;
while ((z = in.getNextEntry()) != null) {
if (z.isDirectory()) {
String name = z.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdirs();
f.setLastModified(z.getTime());
} else {
File f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
int b;
while ((b = in.read()) != -1) out.write(b);
out.close();
f.setLastModified(z.getTime());
}
}
in.close();
}
/**
* Add file to a opened zip file
*
* @param out a ZipOutputStream, you should open it before call this method
* and close if after.
* @param f A file or dir need to add to the zip
* @param base relative path, like "", "1/", "1/binaries/"
* @throws IOException
*/
public static void zip(ZipOutputStream out, File f, String base) throws IOException {
if (f.isDirectory()) {
File[] fl = f.listFiles();
if (!base.equals("")) {
ZipEntry entry = new ZipEntry(base + "/");
entry.setTime(f.lastModified());
out.putNextEntry(entry);
}
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
ZipEntry entry = new ZipEntry(base);
entry.setTime(f.lastModified());
out.putNextEntry(entry);
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
int b;
while ((b = in.read()) != -1) out.write(b);
in.close();
}
}
public static void addEntry(ZipOutputStream out, long time, String entryName) throws IOException {
if (!entryName.equals("")) {
ZipEntry entry = new ZipEntry(entryName);
entry.setTime(time);
out.putNextEntry(entry);
}
}
}