代码:
public class test {
private static final Logger logger = LoggerFactory.getLogger(test.class);
// 压缩文件解压缩
public static void main(String[] args) {
String zipPath = "";
String targetPath = "";
byte[] bytes = new byte[1024];
FileInputStream fileInputStream = null;
ZipInputStream zipInputStream = null;
File file = null;
try{
File target = new File(targetPath);
//判断是否存在这个目录,不存在则创建
if(!target.exists()){
target.mkdirs();
}
fileInputStream = new FileInputStream(zipPath);
zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry = null;
while((zipEntry = zipInputStream.getNextEntry()) != null){
String tmpName = targetPath + File.separator + zipEntry.getName();
file = new File(tmpName);
if(zipEntry.isDirectory()){
mkdir(file);
}else{
//判断父级文件夹是否存在,若不在则创建
mkdir(file.getParentFile());
FileOutputStream outputStream = new FileOutputStream(file);
int len;
while((len = zipInputStream.read(bytes)) > 0){
outputStream.write(bytes,0,len);
}
zipInputStream.closeEntry();
try{
outputStream.close();
}catch (Exception e){
logger.error("流关闭异常,异常信息:" + e.getMessage());
}
}
}
}catch (Exception e){
e.printStackTrace();
logger.error("解压缩失败,异常信息:" + e.getMessage());
}finally {
if(zipInputStream != null){
try{
zipInputStream.close();
}catch (Exception e){
logger.error("流关闭异常,异常信息:" + e.getMessage());
}
}
if(fileInputStream != null){
try{
fileInputStream.close();
}catch (Exception e){
logger.error("流关闭异常,异常信息:" + e.getMessage());
}
}
}
}
public static void mkdir(File file){
if(file == null || file.exists()){
return;
}
mkdir(file.getParentFile());
file.mkdir();
}
}
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |