常用的压缩/解压缩算法

发表信息: by

常用的压缩/解压缩算法

LZ4

<dependency>
    <groupId>org.lz4</groupId>
    <artifactId>lz4-java</artifactId>
</dependency>		
public class RedisCompressUtil {

    public static String compress(String data) {
        if (StringUtils.isEmpty(data)) {
            return "";
        }
        return Base64.getEncoder().encodeToString(Lz4Util.compress(data.getBytes()));
    }

    public static String decompress(String data) {
        if (StringUtils.isEmpty(data)) {
            return "";
        }
        return new String(Lz4Util.decompress(Base64.getDecoder().decode(data)));
    }

}

public class Lz4Util {

    private final static LZ4Factory factory = LZ4Factory.fastestInstance();
    private final static int BLOCK_SIZE = 2048;


    public static byte[] compress(byte[] data) {
        LZ4Compressor compressor = factory.fastCompressor();

        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(bos, BLOCK_SIZE, compressor)) {
            compressedOutput.write(data);
            compressedOutput.finish();
            return bos.toByteArray();
        } catch (IOException e) {
            LogUtil.error("compress data error: {}", StackTraceUtil.getStackTrace(e), e);
            throw new IllegalStateException("lz4 compress error");
        }
    }

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

        LZ4FastDecompressor decompressor = factory.fastDecompressor();

        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ByteArrayInputStream bis = new ByteArrayInputStream(data);
             LZ4BlockInputStream lzis = new LZ4BlockInputStream(bis, decompressor)) {

            int count;
            final byte[] buffer = new byte[BLOCK_SIZE];

            while ((count = lzis.read(buffer)) != -1) {
                bos.write(buffer, 0, count);
            }
            return bos.toByteArray();

        } catch (IOException e) {
            LogUtil.error("compress data error: {}", StackTraceUtil.getStackTrace(e), e);
            throw new IllegalStateException("lz4 decompress error");
        }
    }

}

GZIP


public class CompressUtil {

    private static final Logger logger = LoggerExchanger.getLogger(CompressUtil.class);

    private static String encode = "utf-8";

    public static void setEncode(String encode) {
        CompressUtil.encode = encode;
    }

    public static String gzip(String str) {
        String result = null;

        if (str == null || str.length() == 0) {
            return result;
        }

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzipOut = new GZIPOutputStream(out);

            gzipOut.write(str.getBytes(encode));

            out.close();
            gzipOut.close();

            // gzipOut 在toByteArray之前一定要先close,因为close会flush
            byte[] array = out.toByteArray();

            result = Base64.getEncoder().encodeToString(array);
        } catch (IOException e) {
            logger.error(String.format("gzip error %s", str), e);
        }

        return result;
    }

    public static String unGzip(String str) {
        String result = null;

        if (str == null || str.length() == 0) {
            return result;
        }

        try {
            byte[] bytes = Base64.getDecoder().decode(str);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);

            GZIPInputStream gzipIn = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int n = 0;
            while ((n = gzipIn.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }

            result = out.toString();

            out.close();
            in.close();
            gzipIn.close();
        } catch (IOException e) {
            logger.error(String.format("un gzip error %s", str), e);
        }

        return result;
    }

}