直接上代码:
public static void zipDecompress(String sourcePath,String desPath){
try {
ZipFile zipFile = new ZipFile(sourcePath,Charset.forName("utf-8"));
System.out.println("上传文件路径:"+sourcePath);
Enumeration enumeration = zipFile.entries();
ZipEntry zipEntry;
while(enumeration.hasMoreElements()){
zipEntry = (ZipEntry) enumeration.nextElement();
System.out.println("解压文件名为:"+zipEntry.getName());
if(zipEntry.isDirectory()){
continue;
}
File file = new File(desPath+"\\"+zipEntry.getName());
if(!file.exists()){
file.createNewFile();
}
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
int len;
byte[] bytes = new byte[1024];
while ((len=is.read())!=-1){
fos.write(len);
}
fos.close();
is.close();
}
zipFile.close();
}catch (IOException e){
e.printStackTrace();
}
}
适用于解压文件里面没有文件目录,只有文件的。
有问题欢迎留言,看到我会第一时间回复的。