Zstd-数据压缩组件

Zstandard 简称Zstd,是一款快速实时的开源数据压缩程序,由Facebook开发,源码是用C语言编写的。相比业内其他压缩算法(如Gzip、Snappy、Zlib)它的特点是:当需要时,它可以将压缩速度交换为更高的压缩比率(压缩速度与压缩比率的权衡可以通过小增量来配置),反之亦

Zstd-jni

Zstd拥有丰富的API,几乎支持所有流行的编程语言,Zstd-jni 是Java中提供的API然。 它具有小数据压缩的特殊模式,称为字典压缩,可以从任何提供的样本集中构建字典。

<dependency>

    <groupId>com.github.luben</groupId>

    <artifactId>zstd-jni</artifactId>

    <version>VERSION</version>

</dependency>

/*
    序列化
 */
public static <T> byte[] serialize(T result) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(512);
    try (OutputStream outputStream = new ZstdOutputStream(byteArrayOutputStream)) {
        // protostuff serialize
        ProtostuffSerializer.serialize(result, outputStream);
        return byteArrayOutputStream.toByteArray();
    }
}
 
/*
    反序列化
 */
public static <T> T deserialize(byte[] bytes, Class<T> clazz) throws IOException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    try (InputStream inputStream = new ZstdInputStream(byteArrayInputStream)) {
        // protostuff deserialize
        return ProtostuffSerializer.deserialize(clazz, inputStream);
    }
}
ZstdDictTrainer zstdDictTrainer = new ZstdDictTrainer(1024 * 1024, 32 * 1024);
// fileInput is a sample file
zstdDictTrainer.addSample(fileInput);
byte[] dic = zstdDictTrainer.trainSamples(true);
  
/*
    Zstd's training model
 */
public static <T> byte[] serialize(T result) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(512);
    try (ZstdOutputStream outputStream = new ZstdOutputStream(byteArrayOutputStream)) {
        if (dic != null) {
            outputStream.setDict(dic);
        }
        // protostuff serialize
        ProtostuffSerializer.serialize(result, outputStream);
        return byteArrayOutputStream.toByteArray();
    }
}
  
/*
    Zstd's training model
*/
public static <T> T deserialize(byte[] bytes, Class<T> clazz) throws IOException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    try (ZstdInputStream inputStream = new ZstdInputStream(byteArrayInputStream)) {
        if (dic != null) {
            inputStream.setDict(dic);
        }
        // protostuff deserialize
        return ProtostuffSerializer.deserialize(clazz, inputStream);
    }
}

服务器:VM/4CPU i5-6500/CentOS 6

exp1: 4KB

 

version

compress

decompress

Ratio

Gzipjdk1.80.162ms0.059ms2.31
Snappy1.1.7.20.005ms0.002ms1.83
Zstd1.3.7-30.033ms0.012ms2.27
Zstd_Dic1.3.7-30.020ms0.029ms3.81

exp2: 16KB

 

version

compress

decompress

Ratio

Gzipjdk1.80.279ms0.126ms4.29
Snappy1.1.7.20.022ms0.007ms3.18
Zstd1.3.7-30.049ms0.002ms4.39
Zstd_Dic1.3.7-30.062ms0.017ms5.70

exp3: 43KB

 

version

compress

decompress

Ratio

Gzipjdk1.80.767ms0.339ms6.49
Snappy1.1.7.20.111ms0.044ms4.36
Zstd1.3.7-30.257ms0.018ms6.67
Zstd_Dic1.3.7-30.199ms0.061ms8.12

exp4: 134KB

 

version

compress

decompress

Ratio

Gzipjdk1.81.786ms1.026ms13.34
Snappy1.1.7.20.894ms0.595ms6.53
Zstd1.3.7-30.411ms0.198ms14.74
Zstd_Dic1.3.7-30.220ms0.089ms16.48

exp5:  654KB

 

version

compress

decompress

Ratio

Gzipjdk1.84.587ms1.865ms33.64
Snappy1.1.7.22.069ms1.430ms8.86
Zstd1.3.7-32.864ms0.116ms45.57
Zstd_Dic1.3.7-30.426ms0.218ms47.38

 

åºååçæ§.png

引用链接:

https://github.com/facebook/zstd

https://github.com/luben/zstd-jni

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值