在Java中处理JSON数据时,压缩技术可有效减少网络传输和存储开销。
Java Json压缩工具类
java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.Base64;
public class JsonCompressorUtil {
public static void main(String[] args) {
try {
String originalJson = "[{\"children\":[{\"children\":[{\"children\":[{\"children\":[{\"children\":[]," +
"\"id\":39,\"name\":\"?1\",\"orderNum\":1,\"parentId\":15},{\"children\":[],\"id\":40,\"name\":" +
"\"李2\",\"orderNum\":2,\"parentId\":15}],\"id\":15,\"name\":\"人力部\",\"orderNum\":1,\"parentId\":7}," +
"{\"children\":[{\"children\":[],\"id\":41,\"name\":\"王3\",\"orderNum\":1,\"parentId\":16},{\"children\":[]," +
"\"id\":42,\"name\":\"?4\",\"orderNum\":2,\"parentId\":16}],\"id\":16,\"name\":\"??部\",\"orderNum\":2," +
"\"parentId\":7},{\"children\":[{\"children\":[],\"id\":43,\"name\":\"?5\",\"orderNum\":1,\"parentId\":17}," +
"{\"children\":[],\"id\":44,\"name\":\"周6\",\"orderNum\":2,\"parentId\":17}],\"id\":17,\"name\":\"后勤部\"," +
"\"orderNum\":3,\"parentId\":7}],\"id\":7,\"name\":\"??地区\",\"orderNum\":1,\"parentId\":3},{\"children\":" +
"[{\"children\":[{\"children\":[],\"id\":45,\"name\":\"?7\",\"orderNum\":1,\"parentId\":18},{\"children\":[]," +
"\"id\":46,\"name\":\"?8\",\"orderNum\":2,\"parentId\":18}],\"id\":18,\"name\":\"人力部\",\"orderNum\":1," +
"\"parentId\":8},{\"children\":[{\"children\":[],\"id\":47,\"name\":\"?9\",\"orderNum\":1,\"parentId\":19}," +
"{\"children\":[],\"id\":48,\"name\":\"李10\",\"orderNum\":2,\"parentId\":19}],\"id\":19,\"name\":\"??部\"," +
"\"orderNum\":2,\"parentId\":8},{\"children\":[{\"children\":[],\"id\":49,\"name\":\"王11\",\"orderNum\":1," +
"\"parentId\":20},{\"children\":[],\"id\":50,\"name\":\"?12\",\"orderNum\":2,\"parentId\":20}],\"id\":20," +
"\"name\":\"后勤部\",\"orderNum\":3,\"parentId\":8}],\"id\":8,\"name\":\"?南地区\",\"orderNum\":2,\"parentId\":" +
"3}],\"id\":3,\"name\":\"??\",\"orderNum\":1,\"parentId\":1},{\"children\":[{\"children\":[{\"children\":" +
"[{\"children\":[],\"id\":51,\"name\":\"?13\",\"orderNum\":1,\"parentId\":21},{\"children\":[],\"id\":52," +
"\"name\":\"周14\",\"orderNum\":2,\"parentId\":21}],\"id\":21,\"name\":\"人力部\",\"orderNum\":1,\"parentId\":9}," +
"{\"children\":[{\"children\":[],\"id\":53,\"name\":\"?15\",\"orderNum\":1,\"parentId\":22},{\"children\":" +
"[],\"id\":54,\"name\":\"?16\",\"orderNum\":2,\"parentId\":22}],\"id\":22,\"name\":\"??部\",\"orderNum\":2," +
"\"parentId\":9},{\"children\":[{\"children\":[],\"id\":55,\"name\":\"?17\",\"orderNum\":1,\"parentId\":23}," +
"{\"children\":[],\"id\":56,\"name\":\"李18\",\"orderNum\":2,\"parentId\":23}],\"id\":23,\"name\":\"后勤部\"," +
"\"orderNum\":3,\"parentId\":9}],\"id\":9,\"name\":\"??地区\",\"orderNum\":1,\"parentId\":4},{\"children\":" +
"[{\"children\":[{\"children\":[],\"id\":57,\"name\":\"王19\",\"orderNum\":1,\"parentId\":24},{\"children\":" +
"[],\"id\":58,\"name\":\"?20\",\"orderNum\":2,\"parentId\":24}],\"id\":24,\"name\":\"人力部\",\"orderNum\":1," +
"\"parentId\":10},{\"children\":[{\"children\":[],\"id\":59,\"name\":\"?21\",\"orderNum\":1,\"parentId\":25}," +
"{\"children\":[],\"id\":60,\"name\":\"周22\",\"orderNum\":2,\"parentId\":25}],\"id\":25,\"name\":\"??部\"," +
"\"orderNum\":2,\"parentId\":10},{\"children\":[{\"children\":[],\"id\":61,\"name\":\"?23\",\"orderNum\":1," +
"\"parentId\":26},{\"children\":[],\"id\":62,\"name\":\"?24\",\"orderNum\":2,\"parentId\":26}],\"id\":26," +
"\"name\":\"后勤部\",\"orderNum\":3,\"parentId\":10}],\"id\":10,\"name\":\"?南地区\",\"orderNum\":2,\"parentId\":4}]," +
"\"id\":4,\"name\":\"小米\",\"orderNum\":2,\"parentId\":1}],\"id\":1,\"name\":\"中国\",\"orderNum\":1,\"parentId\":0}," +
"{\"children\":[{\"children\":[{\"children\":[{\"children\":[{\"children\":[],\"id\":63,\"name\":" +
"\"Alice1\",\"orderNum\":1,\"parentId\":27},{\"children\":[],\"id\":64,\"name\":\"Bob2\",\"orderNum\":2," +
"\"parentId\":27}],\"id\":27,\"name\":\"HR\",\"orderNum\":1,\"parentId\":11},{\"children\":[{\"children\":[]," +
"\"id\":65,\"name\":\"Charlie3\",\"orderNum\":1,\"parentId\":28},{\"children\":[],\"id\":66,\"name\":\"David4\"," +
"\"orderNum\":2,\"parentId\":28}],\"id\":28,\"name\":\"Development\",\"orderNum\":2,\"parentId\":11},{\"children\":" +
"[{\"children\":[],\"id\":67,\"name\":\"Eve5\",\"orderNum\":1,\"parentId\":29},{\"children\":[],\"id\":68,\"name\":" +
"\"Frank6\",\"orderNum\":2,\"parentId\":29}],\"id\":29,\"name\":\"Logistics\",\"orderNum\":3,\"parentId\":11}]," +
"\"id\":11,\"name\":\"?盛?地区\",\"orderNum\":1,\"parentId\":5},{\"children\":[{\"children\":[{\"children\":[]," +
"\"id\":69,\"name\":\"Grace7\",\"orderNum\":1,\"parentId\":30},{\"children\":[],\"id\":70,\"name\":\"Helen8\"," +
"\"orderNum\":2,\"parentId\":30}],\"id\":30,\"name\":\"HR\",\"orderNum\":1,\"parentId\":12},{\"children\":" +
"[{\"children\":[],\"id\":71,\"name\":\"Alice9\",\"orderNum\":1,\"parentId\":31},{\"children\":[],\"id\":72," +
"\"name\":\"Bob10\",\"orderNum\":2,\"parentId\":31}],\"id\":31,\"name\":\"Development\",\"orderNum\":2," +
"\"parentId\":12},{\"children\":[{\"children\":[],\"id\":73,\"name\":\"Charlie11\",\"orderNum\":1," +
"\"parentId\":32},{\"children\":[],\"id\":74,\"name\":\"David12\",\"orderNum\":2,\"parentId\":32}]," +
"\"id\":32,\"name\":\"Logistics\",\"orderNum\":3,\"parentId\":12}],\"id\":12,\"name\":\"??地区\",\"orderNum" +
"\":2,\"parentId\":5}],\"id\":5,\"name\":\"谷歌\",\"orderNum\":1,\"parentId\":2},{\"children\":[{\"children" +
"\":[{\"children\":[{\"children\":[],\"id\":75,\"name\":\"Eve13\",\"orderNum\":1,\"parentId\":33},{\"children" +
"\":[],\"id\":76,\"name\":\"Frank14\",\"orderNum\":2,\"parentId\":33}],\"id\":33,\"name\":\"HR\",\"orderNum\":" +
"1,\"parentId\":13},{\"children\":[{\"children\":[],\"id\":77,\"name\":\"Grace15\",\"orderNum\":1,\"parentId\":34}" +
",{\"children\":[],\"id\":78,\"name\":\"Helen16\",\"orderNum\":2,\"parentId\":34}],\"id\":34,\"name\":\"Development\"," +
"\"orderNum\":2,\"parentId\":13},{\"children\":[{\"children\":[],\"id\":79,\"name\":\"Alice17\",\"orderNum\":1," +
"\"parentId\":35},{\"children\":[],\"id\":80,\"name\":\"Bob18\",\"orderNum\":2,\"parentId\":35}],\"id\":35,\"name\"" +
":\"Logistics\",\"orderNum\":3,\"parentId\":13}],\"id\":13,\"name\":\"?盛?地区\",\"orderNum\":1,\"parentId\":6}," +
"{\"children\":[{\"children\":[{\"children\":[],\"id\":81,\"name\":\"Charlie19\",\"orderNum\":1,\"parentId\":36}," +
"{\"children\":[],\"id\":82,\"name\":\"David20\",\"orderNum\":2,\"parentId\":36}],\"id\":36,\"name\":\"HR\",\"" +
"orderNum\":1,\"parentId\":14},{\"children\":[{\"children\":[],\"id\":83,\"name\":\"Eve21\",\"orderNum\":1,\"" +
"parentId\":37},{\"children\":[],\"id\":84,\"name\":\"Frank22\",\"orderNum\":2,\"parentId\":37}],\"id\":37,\"" +
"name\":\"Development\",\"orderNum\":2,\"parentId\":14},{\"children\":[{\"children\":[],\"id\":85,\"name\":\"" +
"Grace23\",\"orderNum\":1,\"parentId\":38},{\"children\":[],\"id\":86,\"name\":\"Helen24\",\"orderNum\":2,\"" +
"parentId\":38}],\"id\":38,\"name\":\"Logistics\",\"orderNum\":3,\"parentId\":14}],\"id\":14,\"name\":\"??地区\"," +
"\"orderNum\":2,\"parentId\":6}],\"id\":6,\"name\":\"微?\",\"orderNum\":2,\"parentId\":2}],\"id\":2,\"name\":\"美国\"," +
"\"orderNum\":2,\"parentId\":0}]\n";
System.out.println("原始JSON长度: " + originalJson.length());
String compressed = JsonCompressorUtil.compressToBase64(originalJson);
System.out.println("压缩后Base64长度: " + compressed.length());
System.out.println("压缩率: " + (100 - (compressed.length() * 100 / originalJson.length())) + "%");
String decompressed = JsonCompressorUtil.decompressFromBase64(compressed);
System.out.println("解压后内容是否一致: " + originalJson.equals(decompressed));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩JSON字符串为Base64编码的GZIP数据
*
* @param jsonString 原始JSON字符串
* @return Base64编码的压缩字符串
* @throws IOException 压缩异常
*/
public static String compressToBase64(String jsonString) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOS = new GZIPOutputStream(bos)) {
gzipOS.write(jsonString.getBytes("UTF-8"));
}
return Base64.getEncoder().encodeToString(bos.toByteArray());
}
/**
* 解压Base64编码的GZIP数据为JSON字符串
*
* @param compressedBase64 压缩后的Base64字符串
* @return 原始JSON字符串
* @throws IOException 解压异常
*/
public static String decompressFromBase64(String compressedBase64) throws IOException {
byte[] compressedBytes = Base64.getDecoder().decode(compressedBase64);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
byte[] buffer = new byte[1024];
int len;
while ((len = gzipIS.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
}
return bos.toString("UTF-8");
}
/**
* 直接压缩JSON字符串为字节数组
*
* @param jsonString 原始JSON字符串
* @return 压缩后的字节数组
* @throws IOException 压缩异常
*/
public static byte[] compress(String jsonString) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOS = new GZIPOutputStream(bos)) {
gzipOS.write(jsonString.getBytes("UTF-8"));
}
return bos.toByteArray();
}
/**
* 直接解压字节数组为JSON字符串
*
* @param compressedBytes 压缩后的字节数组
* @return 原始JSON字符串
* @throws IOException 解压异常
*/
public static String decompress(byte[] compressedBytes) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
byte[] buffer = new byte[1024];
int len;
while ((len = gzipIS.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
}
return bos.toString("UTF-8");
}
}
每天⽤⼼记录⼀点点。内容也许不重要,但习惯很重要!
干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!