解压Zip
/**
* 解压Zip文件
* @param path 文件目录
* @author wsj
*/
public static UnZipFilePaths unZip(String path) {
String savePath;
File file;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
//保存要解压的文件目录
savePath = path.substring(0, path.lastIndexOf(".")) + File.separator;
//创建保存 解压的总目录
new File(savePath).mkdir();
ZipFile zipFile = null;
UnZipFilePaths unZipFilePaths = new UnZipFilePaths();
UnZipFilePaths unZipFilePaths1 = new UnZipFilePaths();
List<String> firstFolderPaths = new ArrayList<>();
List<String> secondFolderPaths = new ArrayList<>();
try {
//解决中文乱码问题
zipFile = new ZipFile(path,"gbk");
Enumeration<?> entries = zipFile.getEntries();
while(entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
//解压的一个内容的名称
String oneFilename = entry.getName();
boolean ismkdir = false;
//检查此文件是否带有文件夹
if(oneFilename.lastIndexOf("/") != -1){
ismkdir = true;
}
//将要存的绝对文件路径
String oneFilePath = savePath + oneFilename;
//如果是文件夹先创建
if(entry.isDirectory()){
file = new File(oneFilePath);
file.mkdirs();
continue;
}
//如果是压缩包 调用头方法
// if(entry.getName().matches(".*.zip")){
// System.out.println("是zip文件!");
// throw new ServiceException("是zip文件");
// }
file = new File(oneFilePath);
//如果是目录先创建,如果不是目录而是文件则
if(!file.exists()){
if(ismkdir){
//目录先创建
new File(oneFilePath.substring(0, oneFilePath.lastIndexOf("/"))).mkdirs();
}
}
//创建文件 的空壳
file.createNewFile();
//流
is = zipFile.getInputStream(entry);
//往空壳里填流
FileUtils.copyInputStreamToFile(is, file);
is.close();
//如果解压的还是zip文件继续解压,之后删除源文件。
if (entry.toString().toLowerCase().endsWith(".zip")) {
//二级文件的一级文件路径集合
unZipFilePaths1 = ZipUtils.unZip(oneFilePath);
file.delete();
}else {
//一级文件路径集合
firstFolderPaths.add(oneFilePath);
}
}
zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
throw new ServiceException("解压失败!");
}finally{
try{
if(is != null){
is.close();
}
if(zipFile != null){
zipFile.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
unZipFilePaths.setFirstFolderPaths(firstFolderPaths);
if (unZipFilePaths1.getFirstFolderPaths() != null) {
unZipFilePaths.setSecondFolderPaths(unZipFilePaths1.getFirstFolderPaths());
}
return unZipFilePaths;
}
@Data
public static class UnZipFilePaths{
UnZipFilePaths(){}
private List<String> firstFolderPaths;
private List<String> secondFolderPaths;
}
UnZipFilePaths 为返回的类中包装的类