(转载)--->作者未知:
这个东西是我以前收藏的,也没有仔细的看,不知道对你有没有帮助.
package com.webeasy.portal.common.utils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
 * @author zhouzp
 * 
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class ZipFileUtil {
/**
 *  
 */
public ZipFileUtil() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws Exception {
String[] source = {"e:\\m2combo_cn.exe", "e:\\WinDVD5.exe"};
String zipFile = "c:\\test.zip";
long start = System.currentTimeMillis();
compress(source, zipFile);
System.out.println("Compress  Total Time is :"
+ (System.currentTimeMillis() - start));
String[] names=getZipFileContent("c:\\test.zip");
System.out.println("---------------------------");
for (int i = 0; i < names.length; i++) {
System.out.println("Name in Content is "+names[i]);
}
System.out.println("----------------------------");
start = System.currentTimeMillis();
unzip("c:\\test.zip","d:\\test");
System.out.println("Decompress Total Time is :"
+ (System.currentTimeMillis() - start));
System.out.println("-------------------- OK -----------------------");
}
/**
 * 将多个文件压缩成一个zip文件
 * @param sourceFiles 数组,源文件的全路径
 * @param zipFile 压缩后的文件的路径
 * @throws IOException
 */
public static void compress(String[] sourceFiles, String zipFile)
throws IOException {
FileOutputStream f = new FileOutputStream(zipFile);
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream out = new ZipOutputStream(csum);
byte[] buf = new byte[4096];
for (int i = 0; i < sourceFiles.length; i++) {
FileInputStream in1 = new FileInputStream(sourceFiles[i]);
BufferedInputStream in = new BufferedInputStream(in1);
out.putNextEntry(new ZipEntry(getFileNameFromPath(sourceFiles[i])));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
}
/**
 * 从路径中读取文件名
 * @param path 包含文件名的路径
 * @return 文件名
 */
public static String getFileNameFromPath(String path) {
return path.substring(path.lastIndexOf(File.separator) + 1);
}
/**
 * 解压缩文件
 * @param zipFile 要解压的zip文件
 * @param unzipPath  解压后文件的存放路径
 * @throws IOException
 */
public static void unzip(String zipFile, String unzipPath)throws IOException {
ZipInputStream in=new ZipInputStream(new FileInputStream(zipFile));
ZipEntry entry;
byte[] buf=new byte[4096];
while((entry=in.getNextEntry())!=null){
String outFile=entry.getName();
String fileName=unzipPath+File.separator+outFile;
System.out.println(fileName);
OutputStream out=new FileOutputStream(fileName);
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.close();
}
in.close();
}
/**
 * 读取一个zip压缩文件中的目录
 * @param zipFile 压缩文件的路径
 * @return  返回包含文件名的数组
 * @throws IOException
 */
public static String[] getZipFileContent(String zipFile) throws IOException {
ArrayList list = new ArrayList();
ZipFile zf = new ZipFile(zipFile);
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println(entry.getName());
list.add(entry.getName());
}
String[] names=new String[list.size()];
int i=0;
for (Iterator iter = list.iterator(); iter.hasNext();) {
String element = (String) iter.next();
names[i]=element;
i++;
}
return names;
}
}