使用zlib压缩解压实体对象并使用base64编码解码工具类

package com.shungkon.common.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shungkon.skycloud.modules.task.ScoreTask;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * @description: 使用zlib压缩、解压用使用base64编码解码工具类
 * @author: wqs
 * @create: 2024-05-29 15:42
 */
public class VOCompressionExample<T> {

    @Data
    public static class MyVO {
        private String name;
        private int value;
    }

    public static void main(String[] args) throws IOException, DataFormatException {
        // 1. 将VO序列化为JSON字节数组
        MyVO vo = new MyVO();
        vo.setName("zhangshan");
        vo.setValue(1);
        // 1. 将VO序列化为JSON字节数组

        VOCompressionExample voCompressionExample = new VOCompressionExample();
        String encodedString = voCompressionExample.voGetEncoderStr(vo);
        // 4.输出编码后的字符串
        System.out.println(encodedString);

        System.out.println("===================================================");
        MyVO myVO = voCompressionExample.strDecodedVo(encodedString);
        System.out.println(JSON.toJSONString(myVO));


    }

    /**
     * 实体通过zlib压缩后,在通过base64进行编码后 得出字符串信息
     * @param t 实体
     * @Return: {@link String}
     * @Author: wuqingsong
     * @Date: 2024/5/29 15:48
     */
    public String voGetEncoderStr(T t) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        byte[] jsonBytes = mapper.writeValueAsBytes(t);
        // 2. 使用zlib压缩字节数组
        byte[] compressedBytes = compress(jsonBytes);

        // 3. 使用Base64对压缩后的字节数组进行编码
        String encodedString = Base64.getEncoder().encodeToString(compressedBytes);
        return encodedString;
    }

    public MyVO strDecodedVo(String encodedString) throws IOException, DataFormatException {
        // 5. 对Base64编码的字符串进行解码,得到压缩后的字节数组
        byte[] decodedCompressedBytes = Base64.getDecoder().decode(encodedString);

        // 6. 使用zlib对压缩的字节数组进行解压
        byte[] decompressedBytes = decompress(decodedCompressedBytes);

        ObjectMapper mapper = new ObjectMapper();
        // 7. 将解压后的字节数组反序列化为实体(VO)
        MyVO deserializedVO = mapper.readValue(decompressedBytes, MyVO.class);

        // 输出反序列化后的VO
        return deserializedVO;
    }


    // 使用zlib解压字节数组
    public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
        Inflater inflater = new Inflater();
        inflater.setInput(data);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] decompressedData = outputStream.toByteArray();
        inflater.end();
        return decompressedData;
    }

    // 使用zlib压缩字节数组
    public static byte[] compress(byte[] data) throws IOException {
        Deflater deflater = new Deflater();
        deflater.setInput(data);
        deflater.finish();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }

        outputStream.close();
        byte[] compressedData = outputStream.toByteArray();
        deflater.end();
        return compressedData;
    }

}

效果:

压缩后的base64编码:eJyrVspLzE1VslIqLyxW0lEqS8wpBfIMawFhNAev
===================================================
解码后的实体数据:{"name":"zhangshan","value":1}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值