/**
* 递归解压
* @param zipFile 目标文件
*
* @param destination 指定解压目录
*
* @throws IOException
* @return 解压后的文件路径
*/
public static List<String> decompression(File zipFile ,String destination) throws IOException {
ZipFile zip = null;
List<String> urlLs = new ArrayList<String>();
try {
zip = new ZipFile(zipFile,Charset.forName("UTF-8"));
Enumeration en=zip.entries();
ZipEntry entry=null;
byte[] buffer=new byte[1024];
int length=-1;
InputStream input=null;
BufferedOutputStream bos=null;
File file=null;
while(en.hasMoreElements()) {
entry=(ZipEntry)en.nextElement();
if(entry.isDirectory()) {
continue;
}
input=zip.getInputStream(entry);
file=new File(destination,entry.getName());
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
urlLs.add(file.getPath());
bos=new BufferedOutputStream(new FileOutputStream(file));
while(true) {
length=input.read(buffer);
if(length==-1) break;
bos.write(buffer,0,length);
}
bos.flush();
bos.close();
input.close();
}
return urlLs;
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
if (zip != null) {
try {
zip.close();
} catch (IOException e) {
zip = null;
}
}
}
}
java递归解压
最新推荐文章于 2024-05-01 23:03:25 发布