|
@@ -0,0 +1,86 @@
|
|
|
+package com.tzld.piaoquan.recommend.feature.util;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.jpountz.lz4.LZ4Compressor;
|
|
|
+import net.jpountz.lz4.LZ4Factory;
|
|
|
+import net.jpountz.lz4.LZ4SafeDecompressor;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+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;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author dyp
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+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);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将Snappy压缩后的String解压缩回String
|
|
|
+ public static String snappyDecompress(String compressedInput) {
|
|
|
+ if (StringUtils.isBlank(compressedInput)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ byte[] compressedBytes = Base64.getDecoder().decode(compressedInput);
|
|
|
+ byte[] decompressedBytes = Snappy.uncompress(compressedBytes);
|
|
|
+ return new String(decompressedBytes, StandardCharsets.UTF_8);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("snappyDecompress error compressedInput {}", compressedInput, e);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|