import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
/**
* 压缩解压工具类
* @author zql
* @createTime 2020-11-21 13:23:35
* @version 1.1
* @modifyLog 1.1 优化代码
*
*/
public class ZipUtil {
/**
* 压缩成zip文件
* @author zql
* @createTime 2020-11-21 13:42:07
*
* @param srcDir
* @param out
* @param keepDir
* @throws Exception
*/
public static void toZip(String srcDir, OutputStream out, boolean keepDir) throws Exception {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
ZipUtil.compress(sourceFile, zos, sourceFile.getName(), keepDir);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + "ms");
} catch (Exception e) {
throw new Exception("压缩失败", e);
}
}
/**
* 压缩文件
* @author zql
* @createTime 2020-11-21 13:41:34
*
* @param sourseFile
* @param zos
* @param name
* @param keepDir
* @throws Exception
*/
private static void compress(File sourseFile, ZipOutputStream zos, String name, boolean keepDir) throws Exception {
byte[] buf = new byte[2048];
FileInputStream in = null;
try {
if (sourseFile.isFile()) {
zos.putNextEntry(new ZipEntry(name));
int len;
in = new FileInputStream(sourseFile);
while((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourseFile.listFiles();
if (Objects.isNull(listFiles) || listFiles.length == 0) {
if (keepDir) {
zos.putNextEntry(new ZipEntry(name + "/"));
zos.closeEntry();
}
} else {
for (File file : listFiles) {
if (keepDir) {
ZipUtil.compress(file, zos, name + "/" + file.getName(), keepDir);
} else {
ZipUtil.compress(file, zos, file.getName(), keepDir);
}
}
}
}
} catch (Exception e) {
throw new Exception("系统异常", e);
} finally {
if (Objects.nonNull(in)) {
try {
in.close();
} catch (Exception e2) {
throw new Exception("关闭文化流失败", e2);
}
}
}
}
/**
* zip解压缩
* @author zql
* @createTime:2020-11-21 13:43:51
*
* @param filePath 压缩文件路径
* @param resDirPath 解压位置路径
* @throws Exception
*/
public static void unZip(String filePath, String resDirPath) throws Exception {
ZipUtil.unZip(new File(filePath), resDirPath);
}
/**
* zip解压缩
* @author zql
* @createTime 2020-11-21 13:57:25
*
* @param file 压缩文件
* @param resDirPath 解压位置路径
* @throws Exception
*/
public static void unZip(File file, String resDirPath) throws Exception {
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
ZipEntry ze = zis.getNextEntry();
File parent = new File(resDirPath);
if (!parent.exists() && parent.mkdirs()){
zis.close();
throw new IOException("创建解压目录\"" + parent.getAbsolutePath() + "\"失败");
}
try {
while (Objects.nonNull(ze)) {
String name = ze.getName();
File child = new File(parent, name);
if (ze.isDirectory()) {
child.mkdirs();
} else {
FileOutputStream output = new FileOutputStream(child);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = zis.read(buffer)) > 0) {
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
}
String suffix = ".tar";
if (child.getName().endsWith(suffix)) {
ZipUtil.unTar(child, resDirPath);
}
ze = zis.getNextEntry();
}
} catch (Exception e) {
// 防止最后一次空读时出现异常:Unexpected end of ZLIB input stream
}
zis.close();
}
/**
* tar解压缩
* @author zql
* @createTime 2020-11-21 13:58:59
*
* @param filePath 压缩文件路径
* @param resDirPath 解压位置路径
* @throws Exception
*/
public static void unTar(String filePath, String resDirPath) throws Exception {
ZipUtil.unTar(new File(filePath), resDirPath);
}
/**
* tar解压缩
* @author zql
* @createTime 2020-11-29 23:29:34
*
* @param file 压缩文件
* @param resDirPath 解压位置路径
* @throws Exception
*/
public static void unTar(File file, String resDirPath) throws Exception {
TarArchiveInputStream tais = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
tais = new TarArchiveInputStream(fis);
TarArchiveEntry tae = null;
while (Objects.nonNull((tae = tais.getNextTarEntry()))) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
try {
String dir = resDirPath + "/" + tae.getName();
File dirFile = new File(dir);
if (tae.isDirectory()) {
dirFile.mkdirs();
} else {
fos = new FileOutputStream(dirFile);
bos = new BufferedOutputStream(fos);
int count;
byte[] date = new byte[1024];
while ((count = tais.read(date, 0, 1024)) != -1) {
bos.write(date, 0, count);
}
String suffix = ".zip";
if (dirFile.getName().endsWith(suffix)) {
ZipUtil.unZip(dirFile, dirFile.getParent());
}
}
} finally {
if (Objects.nonNull(bos)) {
bos.close();
}
if (Objects.nonNull(fos)) {
fos.close();
}
}
}
} finally {
if (Objects.nonNull(tais)) {
tais.close();
}
if (Objects.nonNull(fis)) {
fis.close();
}
}
}
}
jar包:commons-compress-1.18.jar
maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>