以下是最简单的一个例子
package zip;
import java.util.zip.*;
import java.io.*;
public class TestZip {
public TestZip() {
}
//把zipFile 解压到dir目录中
public static void unZip(File zipFile,File dir){
try {
String zipdirname = zipFile.getName();
int off = zipdirname.indexOf(".zip");
zipdirname = zipdirname.substring(0,off);
dir = new File(dir,zipdirname);
if (!dir.exists()) {
dir.mkdirs();
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry ze ;
for(ze = zis.getNextEntry() ; ze != null ; ze = zis.getNextEntry()){
File f = new File(dir,ze.getName());
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
for (int i = 0; i < ze.getSize(); i++) {
int c = zis.read();
fo.write(c);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[]args) throws Exception{
File zipFile = new File("c://123.zip");
File zipdir = new File("c://");
unZip(zipFile,zipdir);
// ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
// ZipEntry ze = zis.getNextEntry();
// while(ze!=null){
// System.out.println(ze);
// for (int i = 0; i < ze.getSize(); i++) {
// System.out.print((char) zis.read());
// }
// ze = zis.getNextEntry();
// }
// System.out.println("end");
}
}