java ziputil
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
static final int BUFFER = 2048;
public static void zip(String zipFileName, String sourcePath) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(zipFileName);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
byte data[] = new byte[BUFFER];
File f = new File(sourcePath);
File files[] = f.listFiles();
for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i].getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void unzip(String zipFileName, String targetPath) {
try {
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration emu = zipFile.entries();
int i = 0;
while (emu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) emu.nextElement();
if (entry.isDirectory()) {
new File(targetPath + entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile
.getInputStream(entry));
File file = new File(targetPath +"/"+ entry.getName());
File parent = file.getParentFile();
if (parent != null && (!parent.exists())) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
bos.close();
bis.close();
}
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将一个文件夹下的所有zip文件按照原目录层级全部解压
* @param dir 原文件夹
*/
public static void unzipDir(String dir) {
ArrayList list=FileUtil.getFileList(dir,"zip");
for (Object object : list) {
String fileName=object.toString();
System.out.println(fileName);
String unzipDir=FileUtil.getFilePathWithoutSuffix(fileName);
if(!(new File(unzipDir).isDirectory())) {
new File(unzipDir).mkdir();
}
ZipUtil.unzip(fileName,unzipDir);
}
}
public static void main(String argv[]) {
unzipDir("e:/temp");
}
}