Snappy
不是压缩比最高的方法,但是性能很好,API 简单。另外如果压缩的数据特别少的话(比如几十个字符),反而增大数据体积,测试5000+字符下有50%的压缩比。
参考
maven
<!-- https://mvnrepository.com/artifact/org.xerial.snappy/snappy-java -->
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.7.2</version>
</dependency>
use
import org.xerial.snappy.Snappy
val testStr = "嘿嘿嘿嘿呵呵嘿哈嘿"
// 压缩
val compressedBytes:Array[Byte] = Snappy.compress(testStr.getBytes("UTF-8"))
// 解压缩
val uncompressedBytes:Array[Byte] = Snappy.uncompress(compressedBytes)
// 解码
println(new String(uncompressed, "UTF-8"))