今天需要把指定压缩包解压为独立的文件夹,压缩包太多,故想着用代码解决。记录一下所用代码:
public class test1 {
public static void main(String[] args) {
File file = new File("F:\\测试_decode\\解压包\\zipdata");
File[] files = file.listFiles();
for (File detailFile : files) {
String filePath = detailFile.getPath();
String fileName = detailFile.getName();
// String path = filePath.replace(".zip", “”);
String path = “F:\测试_decode\解压包\zipdata\unzip\” + fileName.replace(".zip","");
try {
unzip(filePath, path);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void unzip(String sourceZip, String destDir) throws Exception {
try {
Project p = new Project();
Expand e = new Expand();
e.setProject(p);
e.setSrc(new File(sourceZip));
e.setOverwrite(false);
e.setDest(new File(destDir));
/*
ant下的zip工具默认压缩编码为UTF-8编码,
而winRAR软件压缩是用的windows默认的GBK或者GB2312编码
所以解压缩时要制定编码格式
*/
e.setEncoding("gbk");
e.execute();
} catch (Exception e) {
throw e;
}
}
}