package com.dsp.rpc.metricelf;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
/**
* Created by @Author bianqh on 2019/4/30
* 使用apache.ant 解决解压后中文乱码问题
*/
public class ZipUtil {
private static final int BUFFER_SIZE = 512 * 1024;
/**
*
* @param zipSavePath :压缩包路径
* @param unZipSavePath :解压后存储路径
* @return :是否解压成功
* @throws Exception :异常
*/
public static String unZip(String zipSavePath, String unZipSavePath) throws Exception {
String result ;
File file = new File(unZipSavePath);
if (!file.exists()) {
if (!file.exists()) {
throw new Exception("mkdirs: '" + unZipSavePath + "'fail");
}
}
File zFile = new File(zipSavePath);
if (zFile.exists()) {
ZipFile zipFile = null;
OutputStream os = null;
BufferedOutputStream bos = null;
InputStream is = null;
BufferedInputStream bis = null;
CheckedInputStream cos = null;
try {
zipFile = new ZipFile(zipSavePath, "utf-8");
for (Enumeration<ZipEntry> enumeration = zipFile.getEntries(); enumeration.hasMoreElements(); ) {
ZipEntry zipEntry = enumeration.nextElement();
//排除空文件
if (!zipEntry.getName().endsWith(File.separator)) {
if (zipEntry.isDirectory()) {
makeDir(unZipSavePath + zipEntry.getName());
} else {
makeFile(unZipSavePath, zipEntry.getName());
String jarPath = unZipSavePath + zipEntry.getName();
os = new FileOutputStream(jarPath);
bos = new BufferedOutputStream(os);
is = zipFile.getInputStream(zipEntry);
bis = new BufferedInputStream(is);
//采用CRC32算法,保证文件的一致性
cos = new CheckedInputStream(bis, new CRC32());
int len;
byte[] bytes = new byte[BUFFER_SIZE];
while ((len = cos.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.flush();
cos.close();
bis.close();
is.close();
bos.close();
os.close();
}
}
}
zipFile.close();
result = "success";
} finally {
if (cos != null) {
cos.close();
}
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
if (bos != null) {
bos.flush();
bos.close();
}
if (os != null) {
os.close();
}
if (zipFile != null) {
zipFile.close();
}
}
} else {
throw new Exception("zip file can not find");
}
return result;
}
/**
* 创建文件夹
*
* @param dirPath :文件夹路径
* @throws Exception :异常
*/
private static void makeDir(String dirPath) throws Exception {
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
if (!dirFile.mkdirs()) {
throw new Exception("mkdirs : '" + dirPath + "'fail");
}
}
}
/**
* 创建文件
*
* @param dirPath : 文件夹路径
* @param filePath 文件路径
* @throws Exception :异常
*/
private static void makeFile(String dirPath, String filePath) throws Exception {
String[] strArr = filePath.split("\\/");
int i;
for (i = 0; i < strArr.length - 1; i++) {
dirPath = dirPath + strArr[i] + File.separator;
makeDir(dirPath);
}
dirPath = dirPath + strArr[i];
File file = new File(dirPath);
if (!file.exists()) {
if (!file.createNewFile()) {
throw new Exception("createNewFile: '" + dirPath + "'fail");
}
}
}
}
maven 引入ant包
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>