//import basic classes
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Vector;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;//import other user defined classes
import com.hsbc.cos.stf.exception.ZipUtilException;
import com.hsbc.cos.stf.co.util.FileUtil;/*
 * ZipUtil class provide methods to zip/unzip files.
 * Creation date :  31/07/2003
 * @version :  v1.0
 * @author  :  Happyegg
 */public class ZipUtil {
/**
 * unzip file, and return list of unziped files(Without directory).
 * Creation date: 31/07/2003
 * @param zipFile The file need to be unzip
 * @return String[] if there are any files in the zip file, or null if the zip file 
 * has only directory or it is not zip file.
 */
public static String[] unzipFile(String zipFile) throws ZipUtilException {
return unzipFile(zipFile,"");
}
/**
 * unzip file, and return list of unziped files(Without directory).
 * Creation date: 31/07/2003
 * @param zipFile The file need to be unzip
 * @param unzipPath The path with files should be unzip to, if null or blank, unzip to zipFile folder.
 * @return String[] if there are any files in the zip file, or null if the zip file 
 * has only directory or it is not zip file.
 */
public static String[] unzipFile(String zipFile, String unzipPath) throws ZipUtilException {
int BUFFER = 2048;
Vector fileList = new Vector();
try {
BufferedOutputStream destinationFile = null;
FileInputStream fileInput = new FileInputStream(zipFile);
ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream(fileInput));
ZipEntry entry;
if(unzipPath==null || unzipPath.trim().equals("")) {
File zFile = new File(zipFile);
unzipPath = zFile.getParent() + "/";
}
while((entry = zipInput.getNextEntry()) != null) {
if(entry.isDirectory())
continue;
String unzipFilePath = unzipPath + entry.getName();
FileUtil.makeDir(unzipFilePath);
fileList.add(unzipFilePath);
//System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
File f2=new File(unzipFilePath);
FileOutputStream fileOutput = new FileOutputStream(f2);
destinationFile = new  BufferedOutputStream(fileOutput, BUFFER);
while ((count = zipInput.read(data, 0, BUFFER))!= -1) {
destinationFile.write(data, 0, count);
}
destinationFile.flush();
destinationFile.close();
}
zipInput.close();
}
catch(java.io.FileNotFoundException e) {
throw new ZipUtilException(e.toString());
}
catch(java.io.IOException e) {
throw new ZipUtilException(e.toString());
}
return (String[]) fileList.toArray(new String[0]);
} /**
 * Compress input file(folder) to zip file. 
 * Creation date: 31/07/2003
 * @param zipFile The destination
 * @param inputFile The path of file or folder need to be compressed
 * @return String[] if there are any files in the zip file, or null if the zip file 
 * has only directory or it is not zip file.
 */
public static void zip(String zipFile,String inputFile) throws ZipUtilException {
int BUFFER = 2048;
FileOutputStream  fos = null;
ZipOutputStream zFile = null;
String rootPath = null;
try{
fos = new FileOutputStream(zipFile);
zFile = new ZipOutputStream(new BufferedOutputStream(fos));
zFile.setMethod(ZipOutputStream.DEFLATED);
File f = new File(inputFile);
rootPath = f.getParent();
if(f.isDirectory()) {
File files[]=f.listFiles();
for(int i=0; i<files.length; i++) {
zipDir(zFile,files[i].toString(),rootPath);
}
}
else {
zipFiles(zFile,inputFile,rootPath);
} String files[] = f.list();
//int num=files.length;
int count;

}
catch(Exception e) {
System.out.println("[zip] : " + e.toString());
}
finally {
try {
zFile.close();
}
catch(Exception e2) {
}
}
}
private static void zipDir(ZipOutputStream zFile, String inputFile, String rootPath) throws ZipUtilException {
File f = new File(inputFile);
if(f.isDirectory()) {
File files[] = f.listFiles();
for(int i=0; i<files.length; i++) {
zipDir(zFile,files[i].toString(),rootPath);
}
}
else {
zipFiles(zFile,inputFile,rootPath);
}
}
private static void zipFiles(ZipOutputStream zFile, String inputFile, String rootPath) throws ZipUtilException {
int BUFFER = 2048;
int count;
try {
String zEntryFile = inputFile.substring(rootPath.length() + 1);
ZipEntry entry = new ZipEntry(zEntryFile);
zFile.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile),BUFFER);
byte data[] = new byte[BUFFER];
while((count = bis.read(data,0,BUFFER))!=-1){
zFile.write(data,0,count);
}
bis.close();
}
catch(Exception e) {
System.out.println("[zipFiles] : " + e.toString());
}
}}