直接上代码,感觉还是挺高效的。
package com.fenbi.commons.spider.services.utils;
import com.fenbi.commons.core.log.GLog;
import org.xerial.snappy.Snappy;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* @author chaiqx
* 压缩文本
*/
public class CompressUtils {
/**
* 压缩文本
*
* @param text
* @return
*/
public static byte[] compressText(String text) {
byte[] tempBytes = text.getBytes(StandardCharsets.UTF_8);
//GLog.info("压缩前:" + tempBytes.length);
try {
byte[] compressedText = Snappy.compress(tempBytes);
//GLog.info("压缩后:" + compressedText.length);
return compressedText;
} catch (IOException e) {
GLog.error("compress text error, ", e);
}
return null;
}
/**
* 解压文本
*
* @param bytes
* @return
*/
public static String deCompressText(byte[] bytes) {
try {
return Snappy.uncompressString(bytes);
} catch (IOException e) {
GLog.error("uncompress text error, ", e);
}
return null;
}
}