解压ZIP文件工具类
package com.xx;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @author Zongkai.Li
* @version 1.0
* @date 2020/12/1 16:17
*/
public class UnZipUtils {
// 使用此工具类的一般形式为:
// String path = "D:"+ File.separator +"fileupload"+ File.separator;
// List<String> unZipFileList = UnZipUtils.UnZip(multipartFile, path);
// 通过unZipFileList 对解压后的文件进行操作
// boolean isDelete = UnZipUtils.deleteTempFileList(unZipFileList);
// UnZipUtils.deleteFolder(new File(path + UnZipUtils.TEMP + File.separator));
// 如果确保指定目录没有其他不需要删除的文件或文件夹,也可以更直接的使用 UnZipUtils.deleteFolder(new File(path + UnZipUtils.TEMP)); 方法来进行删除.
public static final String TEMP = "temp";
/**
* @Description: 解压zip文件到指定目录,返回解压后的文件路径列表. 解压后的文件在tempDir参数的temp文件夹下
* @Param: multipartFile = zip文件的multipartFile对象; tempDir = 文件将要解压到tempDir目录下 注意:tempDir最后需要加操作系统对应的目录分隔符 /或\
* @return: 返回解压后的文件的路径列表,返回null表示解压失败.
* @Author: Zongkai.Li
*/
public static List<String> UnZip(MultipartFile multipartFile, String tempDir){
List<String> fileList = new ArrayList<>();
try(ZipInputStream zipInputStream = new ZipInputStream(multipartFile.getInputStream(), Charset.forName("GBK"))) {
ZipEntry entry = zipInputStream.getNextEntry();
tempDir = tempDir + TEMP + File.separator;
while (entry != null){
File file = createFileAndAddFileList(tempDir, fileList, entry);
if (entry.getName().contains(".")) {
unZipFile(zipInputStream, file);
}
entry = zipInputStream.getNextEntry();
}
}catch (Throwable throwable){
throwable.printStackTrace();
return null;
}
return fileList;
}
/**
* @Description: 解压zip文件
* @Param: file=解压后文件的存放路径
* @Author: Zongkai.Li
*/
private static void unZipFile(ZipInputStream zipInputStream, File file) throws IOException {
try (FileOutputStream fileOutput = new FileOutputStream(file)){
byte[] buffer = new byte[4096];
for (int bufferSize = zipInputStream.read(buffer); bufferSize > 0; bufferSize = zipInputStream.read(buffer)) {
fileOutput.write(buffer, 0, bufferSize);
}
}
}
/**
* @Description: 解压zip文件到指定目录,返回解压后的文件路径列表. 解压后的文件在tempDir参数的temp文件夹下
* @Param: multipartFile = zip文件的流对象; tempDir = 文件将要解压到tempDir目录下 注意:tempDir最后需要加操作系统对应的目录分隔符 /或\
* @return: 返回解压后的文件的路径列表,返回null表示解压失败.
* @Author: Zongkai.Li
*/
public static List<String> UnZip(InputStream inputStream, String tempDir){
List<String> fileList = new ArrayList<>();
try(ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("GBK"))) {
ZipEntry entry = zipInputStream.getNextEntry();
tempDir = tempDir + TEMP + File.separator;
while (entry != null){
File file = createFileAndAddFileList(tempDir, fileList, entry);
if (entry.getName().contains(".")) {
unZipFile(zipInputStream, file);
}
entry = zipInputStream.getNextEntry();
}
}catch (Throwable throwable){
throwable.printStackTrace();
return null;
}
return fileList;
}
/**
* @Description: 创建解压后的空文件,等待写入.
* @Author: Zongkai.Li
*/
private static File createFileAndAddFileList(String tempDir, List<String> fileList, ZipEntry entry) throws IOException {
File file = new File(tempDir + entry.getName());
if (!file.isDirectory()) {
if (!file.exists()) {
if (file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
fileList.add(file.getCanonicalPath());
}
return file;
}
/**
* @Description: 删除解压后的临时文件
* @Param: 需要删除的文件路径列表 一般是unZipFile方法返回的列表
* @return: true表示删除成功,false表示删除失败
* @Author: Zongkai.Li
*/
public static boolean deleteTempFileList(List<String> fileList){
try {
for (String fileStr : fileList) {
File file = new File(fileStr);
if (file.exists()) {
file.delete();
}
}
}catch (Throwable throwable){
throwable.printStackTrace();
return false;
}
return true;
}
/**
* @Description: 删除解压后的文件
* @Param: 需要删除的文件路径 一般是unZipFile方法返回的列表
* @return: true表示删除成功,false表示删除失败
* @Author: Zongkai.Li
*/
public static boolean deleteTempFile(String filePath){
try {
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
}catch (Throwable throwable){
throwable.printStackTrace();
return false;
}
return true;
}
/**
* @Description: 删除解压后产生的临时文件夹
* @Param: 需要删除的目录 如果要删除之前解压产生的临时文件夹,需要传入 new File(folderPath + UnZipUtils.TEMP) 形式的参数
* @Author: Zongkai.Li
*/
public static void deleteFolder(File folder) {
if (!folder.exists()) {
return;
}
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteFolder(file);
} else {
file.delete();
}
}
}
folder.delete();
}
}
// 使用此工具类的一般形式为:
String path = "D:"+ File.separator +"fileupload"+ File.separator;
List<String> unZipFileList = UnZipUtils.UnZip(multipartFile, path);
// TODO 通过 unZipFileList 对解压后的文件进行操作
// boolean isDelete = UnZipUtils.deleteTempFileList(unZipFileList);
// 确保指定目录下没有其他不需要删除的文件或文件夹
UnZipUtils.deleteFolder(new File(path + UnZipUtils.TEMP + File.separator));