提取文件打成压缩包和将压缩包解压成文件

依赖第三方sdk

1
2
3
4
5
6
7
<!-- ant zip解压-->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.7</version>
</dependency>

解压提取压缩包文件路径处理代码整理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//ZipUtils 压缩解压工具类
//解压方法代码:

//web请求流解压方法入口:
//file为web上传的文件流对象,path为解压的临时目录
/**
* 保存zip文件到本地并调用解压方法并返回解压出的文件的路径集合
*
* @param file 文件
* @param path 解压目标目录文件夹
* @return list //解压出的文件的路径合集
*/
public static List<String> batchAdd(MultipartFile file, String path){
/*
*创建临时文件夹
* 解压文件
*/
String fileName = file.getOriginalFilename();

File dir = new File(path);
if(!dir.exists()) {
dir.mkdirs();
}
File saveFile = new File(dir, fileName);//将压缩包解析到指定位置
List<String> list = new ArrayList<>();
try {
file.transferTo(saveFile);
String newFilePath = path + fileName;
File zipFile = new File(newFilePath);
list = unZipFiles(zipFile, path);//解压文件,获取文件路径
} catch (Exception e) {
e.printStackTrace();
log.error("解压执行失败");
}
return list;
}

/**
* zip解压
*
* @param srcFile zip源文件
* @param destDirPath 解压后的目标文件夹
* @return list 解压文件的路径合集
* @throws RuntimeException 解压失败会抛出运行时异常
*/
本地压缩文件解压方法入口:
srcFile为压缩包的文件流,path为解压的临时目录
返回List<String>是解压后全量文件的绝对路径
public static List<String> unZipFiles(File srcFile, String destDirPath) throws RuntimeException {
List<String> list = new ArrayList<>();
long start = System.currentTimeMillis();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
org.apache.tools.zip.ZipEntry entry = (org.apache.tools.zip.ZipEntry) entries.nextElement();
log.info("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = destDirPath + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDirPath + entry.getName());
if (!targetFile.getParentFile().exists()) {
log.info("父文件不存在");
continue;//mac 本地测试多了一个压缩文件,执行跳过
}
// 保证这个文件的父文件夹必须要存在
list.add(destDirPath + entry.getName());
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream( entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
long end = System.currentTimeMillis();
log.info("解压完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}

/**
* @param filePath 临时文件的删除
* 删除文件夹里面子目录
* 再删除文件夹
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if ((!file.exists()) || (!file.isDirectory())) {
log.info("file not exist");
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (filePath.endsWith(File.separator)) {
temp = new File(filePath + tempList[i]);
} else {
temp = new File(filePath + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
deleteFiles(filePath + "\\" + tempList[i]);
}
}
// 空文件的删除
file.delete();
}

//单元测试类 ZipUtilTest
//测试方法:
@Test
public void UnZipFileTest(){
File file = new File(this.getClass().getResource("").getPath()+"20220815_SACJ_REPORT.zip");
List<String> pathList = ZipUtils.unZipFiles(file, this.getClass().getResource("").getPath()+"temp/");
System.out.println(JSONObject.toJSONString(pathList));
}

单元测试方法执行结果如下图:

提取对应文件并打成压缩包代码整理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
ZipUtils 压缩解压工具类
解压方法代码:

//srcFiles为需要压缩的原文件流,zipFilePath为压缩包生成压缩包的绝对路径
//返回压缩文件生成的绝对路径名
public static String zipFiles(List<File> srcFiles, String zipFilePath){
File zipFile = new File(zipFilePath);
FileOutputStream fileOutputStream = null;
ZipOutputStream zipOutputStream = null;
FileInputStream fileInputStream = null;

try {
//创建zip输出流
fileOutputStream = new FileOutputStream(zipFile);
zipOutputStream = new ZipOutputStream(fileOutputStream);

for(File srcFile: srcFiles) {
//创建文件输入流
fileInputStream = new FileInputStream(srcFile);
ZipEntry zipEntry = new ZipEntry(srcFile.getName());
zipOutputStream.putNextEntry(zipEntry);
// 该变量记录每次真正读的字节个数
int len;
// 定义每次读取的字节数组
byte[] buffer = new byte[1024];
while ((len = fileInputStream.read(buffer)) != -1) {
zipOutputStream.write(buffer, 0, len);
}
}

zipOutputStream.closeEntry();
zipOutputStream.close();
fileInputStream.close();
fileOutputStream.close();


} catch (Exception e) {
throw new RuntimeException(e);
}
return zipFile.getAbsolutePath();

}

//单元测试类 ZipUtilTest
//测试方法:

@Test
public void zipFileTest(){
File file1 = new File(this.getClass().getResource("").getPath()+"temp/SACJP9131000071788122081501.xml");
File file2 = new File(this.getClass().getResource("").getPath()+"temp/SACTT9131000071788122081501.xml");
List<File> files = new ArrayList<>();
files.add(file1);
files.add(file2);
System.out.println(ZipUtils.zipFiles(files, this.getClass().getResource("").getPath()+"temp/test.zip"));

}

单元测试方法执行结果如下图:

参考博客:

  1. https://codeantenna.com/a/wmTS85bf5q
  2. https://icode.best/i/79614042295668