当前位置 : 主页 > 编程语言 > c++ >

节省存储空间,对需要存储的字符串、json串压缩处理后再存储

来源:互联网 收集:自由互联 发布时间:2021-06-30
节省存储空间,对需要存储的字符串、json串压缩处理后再存储 package com.example.biodemo.bio;import org.springframework.util.StringUtils;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import j
节省存储空间,对需要存储的字符串、json串压缩处理后再存储
package com.example.biodemo.bio;
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author mengqingyi
 * @create 2017-10-31 13:49
 **/
public class GZIPUtils {
    public static final int BUFFER = 2048;

    private static void compress(String str) {
        if (StringUtils.isEmpty(str)) {
            return;
        }
        try {
            compress(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }


    private static void compress(InputStream inputStream, OutputStream outputStream) throws IOException {
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
        int count;
        byte[] data = new byte[BUFFER];
        while ((count = inputStream.read(data, 0, BUFFER)) != -1) {
            gzipOutputStream.write(data, 0, count);
        }
        gzipOutputStream.finish();
        gzipOutputStream.flush();
        gzipOutputStream.close();
    }

    private static byte[] compress(byte[] data) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //压缩
        compress(byteArrayInputStream, byteArrayOutputStream);
        byte[] output = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();
        byteArrayInputStream.close();
        return output;
    }

    private static byte[] decompress(byte[] data) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //解压缩
        decompress(byteArrayInputStream, byteArrayOutputStream);
        data = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();
        byteArrayInputStream.close();
        return data;
    }

    private static void decompress(ByteArrayInputStream byteArrayInputStream,
            ByteArrayOutputStream byteArrayOutputStream) throws IOException {
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
        int count;
        byte[] data = new byte[BUFFER];
        while ((count = gzipInputStream.read(data, 0, BUFFER)) != -1) {
            byteArrayOutputStream.write(data, 0, count);
        }
        gzipInputStream.close();
    }

    /**
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        String str = "[{\"contact_number\":\"13387768903\",\"contact_seq_no\":\"1\"," + "\"contact_area\":\"广西壮族自治区"
                + ".百色市\"," + "" + "" + "" + "" + "\"call_count_1week\":\"6\"," + "\"call_count_1month\":\"53\"," +
                "\"call_count_3month\":\"141\"," + "\"call_count_active_3month\":\"127\"," +
                "\"call_count_passive_3month\":\"14\"," + "\"call_count_late_night_3month\":\"4\"," +
                "\"call_count_work_time_3month\":\"78\"," + "\"call_count_offwork_time_3month\":\"59\"," +
                "\"call_count_workday_3month\":\"103\"," + "\"call_count_holiday_3month\":\"38\"," +
                "\"call_count_6month\":\"309\"," + "\"call_count_active_6month\":\"281\"," +
                "\"call_count_passive_6month\":\"28\"," + "\"call_count_late_night_6month\":\"5\"," +
                "\"call_count_work_time_6month\":\"170\"," + "\"call_count_offwork_time_6month\":\"134\"," +
                "\"call_count_workday_6month\":\"216\"," + "\"call_count_holiday_6month\":\"93\"," +
                "\"call_time_1month\":\"2219\"," + "\"call_time_3month\":\"6156\"," +
                "\"call_time_active_3month\":\"5623\"," + "\"call_time_passive_3month\":\"533\"," +
                "\"call_time_6month\":\"25778\"," + "\"call_time_active_6month\":\"24660\"," +
                "\"call_time_passive_6month\":\"1118\"," + "\"msg_count_1month\":\"0\",\"msg_count_3month\":\"0\"," +
                "\"msg_count_6month\":\"0\"," + "\"is_whole_day_call_3month\":\"是\"," +
                "\"is_whole_day_call_6month\":\"是\"," + "\"first_time_call_6month\":\"2017-04-01 08:53:59\"," +
                "\"last_time_call_6month\":\"2017-09-14 " + "17:37:42\"}]";
        byte[] input = str.getBytes();
        System.out.println(input.length);
        byte[] data = compress(input);
        byte[] out = decompress(data);
        System.out.println(out.length);
        for (int i = 0; i < out.length; i++) {
            System.out.println(input[i] == out[i]);
        }
    }
}
网友评论