目录
[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;
public class CompressionUtils {
public static String zipBase64(String... jsons) { byte[] bytes = zips(jsons); return Base64.encode(bytes); }
public static List<String> unZipBase64(String base64) { byte[] bytes = Base64.decode(base64); return unzips(bytes); }
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(); }
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
|
@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); }
@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);
strings.forEach(System.out::println);
return new ResponseUtils<List<String>>().success(strings); }
|
SpringBoot使用Hutool将数据压缩成BASE64编码格式