|
@@ -1,115 +0,0 @@
|
|
|
-package com.tzld.piaoquan.recommend.feature.produce.util;
|
|
|
|
|
-
|
|
|
|
|
-import net.jpountz.lz4.LZ4Compressor;
|
|
|
|
|
-import net.jpountz.lz4.LZ4Factory;
|
|
|
|
|
-import net.jpountz.lz4.LZ4SafeDecompressor;
|
|
|
|
|
-import org.xerial.snappy.Snappy;
|
|
|
|
|
-
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.nio.ByteBuffer;
|
|
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
|
|
-import java.util.Base64;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * @author dyp
|
|
|
|
|
- */
|
|
|
|
|
-public class CompressionUtil {
|
|
|
|
|
- public static String lz4Compress(String input) {
|
|
|
|
|
- byte[] data = input.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
- LZ4Factory factory = LZ4Factory.fastestInstance();
|
|
|
|
|
- LZ4Compressor compressor = factory.fastCompressor();
|
|
|
|
|
- int maxCompressedLength = compressor.maxCompressedLength(data.length);
|
|
|
|
|
- byte[] compressed = new byte[maxCompressedLength];
|
|
|
|
|
- int compressedLength = compressor.compress(data, 0, data.length, compressed, 0, maxCompressedLength);
|
|
|
|
|
- byte[] result = new byte[compressedLength];
|
|
|
|
|
- System.arraycopy(compressed, 0, result, 0, compressedLength);
|
|
|
|
|
- return Base64.getEncoder().encodeToString(result);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String lz4Decompress(String input) {
|
|
|
|
|
- // 将Base64编码的字符串解码为字节数组
|
|
|
|
|
- byte[] data = Base64.getDecoder().decode(input);
|
|
|
|
|
- LZ4Factory factory = LZ4Factory.fastestInstance();
|
|
|
|
|
- LZ4SafeDecompressor decompressor = factory.safeDecompressor();
|
|
|
|
|
-
|
|
|
|
|
- // 创建一个缓冲区来存储解压缩后的数据
|
|
|
|
|
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
-
|
|
|
|
|
- // 使用ByteBuffer来处理压缩数据
|
|
|
|
|
- ByteBuffer buffer = ByteBuffer.wrap(data);
|
|
|
|
|
-
|
|
|
|
|
- // 假设每次读取的块大小为4KB(可以根据实际情况调整)
|
|
|
|
|
- byte[] chunk = new byte[4096];
|
|
|
|
|
- byte[] decompressedChunk = new byte[4096 * 2]; // 解压缩后的块可能会更大
|
|
|
|
|
-
|
|
|
|
|
- while (buffer.hasRemaining()) {
|
|
|
|
|
- int remaining = Math.min(buffer.remaining(), chunk.length);
|
|
|
|
|
- buffer.get(chunk, 0, remaining);
|
|
|
|
|
-
|
|
|
|
|
- // 解压缩当前块
|
|
|
|
|
- int decompressedLength = decompressor.decompress(chunk, 0, remaining, decompressedChunk, 0);
|
|
|
|
|
-
|
|
|
|
|
- // 将解压缩后的数据写入输出流
|
|
|
|
|
- outputStream.write(decompressedChunk, 0, decompressedLength);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 返回解压后的数据
|
|
|
|
|
- return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String snappyCompress(String input) throws IOException {
|
|
|
|
|
- byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
- byte[] compressedBytes = Snappy.compress(inputBytes);
|
|
|
|
|
- return Base64.getEncoder().encodeToString(compressedBytes);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static byte[] snappyCompressV2(String input) throws IOException{
|
|
|
|
|
- byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
- return Snappy.compress(inputBytes);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 将Snappy压缩后的String解压缩回String
|
|
|
|
|
- public static String snappyDecompress(String compressedInput) throws IOException {
|
|
|
|
|
- byte[] compressedBytes = Base64.getDecoder().decode(compressedInput);
|
|
|
|
|
- byte[] decompressedBytes = Snappy.uncompress(compressedBytes);
|
|
|
|
|
- return new String(decompressedBytes, StandardCharsets.UTF_8);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
|
|
- String str = "gBR8eyJmZWF0dXJlIjoie1wicm9zXzFoXCI6XCIwLjBcIiwJEwAyRhMAADNGEwAANEYTAAA2RhMABDdkOl8AAHZGcgAAdkZyAAB2RnIAAHZGcgAAdkZyAAB2PnIACHN0cj5yAAETOuQAARM65AABEzrkAAETOuQAARM65AAEdm9CcgABEzpyAAETOnIAARM6cgABEzpyAAETQnIAQlYBAHZGVgEBE0JyAEJWAQEmQnIAQlYBRTo60gABFAAyOsAAAHIBmkIoAAB2QigAIUc+KAABFD5QAAx2aWV3Ps8BBRQ6ZAAFFD7tAAE8OqAABRQ+7wABKEJiAQByQqAAIYlCoABK8AABFD4oABByX2NudD7JAAkVOsoACRU6ywAJFTrMAAkVOs0ACRU6zgAQc2hhcmU+fgAJFTp+AAkVOn4ACRU6fgAJFTp+AAkVOn4AAHYhYD7tAQUVWiYBOpQACdNGLAAIcmF0QtUAAHIJFkJCAAUsOtcADSwANDrFBAksOtkADRY62gAUcmV0dXJuPlkBDRY+hAAJLD6EAAkWADQ6hAAJFj6EAAkWOoQAKXM+NAEJFkY0AQxjbnQ0RgQGDRc+sgANFz6zAA0XPrQADRc+tQANFzq2AC1QPrcADRdCuAAJ+kIuAAkXQi4ADXNCLwANGEIwAAlHAHZFp1KJAQUbPh4BHTY+IgEdGwA0ZlEAPioBHTY+LgEdG0LWAB0cPtoAAHYB0whoYXMNJDrdADoeADrgADoeADrjAAB2NloAOuYAOh4AOukAOh4AOuwASbpitQA+HwA6tgA+HwA6twA+HwA6uAA+HwA6uQA+HwA6ugAAdjYUAT6pATofAD6sAT5dAD4/AD4gADJAAFx9IiwidmlkZW9pZCI6IjEwMDAwMTYwIn0=";
|
|
|
|
|
- System.out.println(CompressionUtil.snappyDecompress(str));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void testSnappyDecompress(List<String> strs, int num) {
|
|
|
|
|
- try {
|
|
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
|
|
- for (int i = num; i > 0; i--) {
|
|
|
|
|
- for (String str : strs) {
|
|
|
|
|
- System.out.println(CompressionUtil.snappyDecompress(str));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- System.out.println(num + " : snappy decompress cost: " + (System.currentTimeMillis() - startTime) + "ms");
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void testLz4Decompress(List<String> strs, int num) {
|
|
|
|
|
- try {
|
|
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
|
|
- for (int i = num; i > 0; i--) {
|
|
|
|
|
- for (String str : strs) {
|
|
|
|
|
- CompressionUtil.lz4Decompress(str);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- System.out.println(num + " : lz4 decompress cost: " + (System.currentTimeMillis() - startTime) + "ms");
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|