目录

[TOC]

依赖

hutool官网:https://www.hutool.cn/

1
2
3
4
5
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</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
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
* 压缩工具类
*
* @author XiaoFei
*/
public class CompressionUtils {

/**
* 压缩并生成base64字符串
*
* @param jsons json数组
* @return base64
*/
public static String zipBase64(String... jsons) {
//将JSON数组批量压缩
byte[] bytes = zips(jsons);
//生成base64
return Base64.encode(bytes);
}

/**
* 解压缩base64
*
* @param base64 base64字符串
* @return JSON字符串数组
*/
public static List<String> unZipBase64(String base64) {
//将base64 解码
byte[] bytes = Base64.decode(base64);
//批量解压
return unzips(bytes);
}

/**
* 批量压缩byte数组
*
* @param jsons json数组
* @return 压缩后的byte数组
*/
public static byte[] zips(String[] jsons) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos, StandardCharsets.UTF_8);
try {
for (String json : jsons) {
ZipEntry ze = new ZipEntry(IdUtil.randomUUID());
zos.putNextEntry(ze);
byte[] data = json.getBytes(StandardCharsets.UTF_8);
zos.write(data, 0, data.length);
}
zos.flush();
zos.close();
baos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(baos);
IOUtils.closeQuietly(zos);
}

return baos.toByteArray();
}

/**
* 批量解压缩
*
* @param data 压缩的byte数组
* @return 返回json数组字符串
*/
public static List<String> unzips(byte[] data) {
//创建结果集
List<String> strings = new ArrayList<>();
ByteArrayInputStream bais = null;
ZipInputStream zis = null;
try {
bais = new ByteArrayInputStream(data);
zis = new ZipInputStream(bais, StandardCharsets.UTF_8);
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
// 解析文件夹
boolean directory = ze.isDirectory();
if (directory)
continue;
InputStreamReader inputStreamReader = new InputStreamReader(zis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
while ((line = reader.readLine()) != null) {
strings.add(line);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != zis) {
try {
zis.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bais) {
try {
bais.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != zis) {
try {
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return strings;
}

}

使用

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
/**
* 查询全部并压缩
*
* @return 全部数据
*/
@ApiOperation(value = "查询全部并压缩", httpMethod = "GET", response = ResponseUtils.class, produces = "application/json")
@GetMapping("list/zip")
public ResponseUtils<String> selectListByZip() {
List<UserEntity> items = userService.selectList();

//使用工具类将集合进行压缩
String base64Str = CompressionUtils.zipBase64(JSON.toJSONString(items));

return new ResponseUtils<String>().success(base64Str);
}

/**
* 解压数据
*
* @return 全部数据
*/
@ApiOperation(value = "解压数据", httpMethod = "POST", response = ResponseUtils.class, produces = "application/json")
@PostMapping("list/unzip")
public ResponseUtils<List<String>> selectListByUnZip(@RequestBody String base64Str) {
//解压
List<String> strings = CompressionUtils.unZipBase64(base64Str);

//需要再进一步将syrings中的每一列字符串解析为对应的实体类
strings.forEach(System.out::println);

return new ResponseUtils<List<String>>().success(strings);
}