Java zlib 解压缩 json 字节数组

  • Post author:
  • Post category:java


本demo实现了:

1. 将字符串压缩,转为字节流。

2. 定义对象,将字节流存入对象中。

2. 使用Gson,将对象转为json字符串。

3. 使用Gson将字符串转为对象。

4. 获取对象中的字节流,并进行解压,得到原始字符串。

备注:

主要是实际业务中,传输协议中需要兼容多种压缩算法,故不能对json串进行整体压缩,需要将压缩类型单独定义,再使用单独的key传输压缩后的字节流。


HelloJsonObj:
public class HelloJsonObj {
    String compressType;
    byte[] compressData;

    public String getCompressType() {
        return compressType;
    }

    public void setCompressType(String compressType) {
        this.compressType = compressType;
    }

    public byte[] getCompressData() {
        return compressData;
    }

    public void setCompressData(byte[] compressData) {
        this.compressData = compressData;
    }

}

ZlibTest:

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.InflaterOutputStream;


public class ZlibTest {

    public static byte[] compress(byte[] data) {
        byte[] output = new byte[0];

        Deflater compresser = new Deflater();

        compresser.reset();
        compresser.setInput(data);
        compresser.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
        try {
            byte[] buf = new byte[1024];
            while (!compresser.finished()) {
                int i = compresser.deflate(buf);
                bos.write(buf, 0, i);
            }
            output = bos.toByteArray();
        } catch (Exception e) {
            output = data;
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compresser.end();
        return output;
    }


    public static byte[] decompress(byte[] compressedTxt) {

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try (OutputStream ios = new InflaterOutputStream(os)) {
            ios.write(compressedTxt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return os.toByteArray();

    }


    public static void main(String[] args) {
        System.out.println("zlib test start...");
        String value = "hello hello hello hello hello hello hello hello";
        System.out.println("原始字符串:" + value);
        System.out.println("压缩前的字节流长度:" + value.length());

        byte[] compressData = compress(value.getBytes());
        System.out.println("压缩后的字节流长度:" + compressData.length);


        HelloJsonObj helloJsonObj = new HelloJsonObj();
        helloJsonObj.setCompressType("1");
        helloJsonObj.setCompressData(compressData);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(helloJsonObj);
        System.out.println("原json:" + jsonStr);


        HelloJsonObj receivedJsonObject = gson.fromJson(jsonStr, HelloJsonObj.class);
        System.out.println("解压缩类型:" + receivedJsonObject.getCompressType());

        byte[] compressDataBytes = receivedJsonObject.getCompressData();
        byte[] decompressData = decompress(compressDataBytes);
        System.out.println("解压缩后的字节流长度:" + decompressData.length);

        String resultValue = new String(decompressData);
        System.out.println("解压缩后的字符串:" + resultValue);


    }
}



版权声明:本文为HelloKitty520123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。