0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

数据压缩算法的介绍

汽车电子技术 来源:Java烂笔头 作者:小週 2023-02-28 14:25 次阅读

1 前言

在RPC通信数据的传输场景下,当通信报文数据传输较大时,会对数据包进行压缩传输,根据不同传输场景,常用的压缩算法有Zlib、Gzip、Bzip2、Deflater、Lz4、Lzo、Snappy算法等。以下将包括算法的介绍、Java实现代码以及各算法间的模拟性能对比。

2 压缩方案

  • Zlib

bzip2是Julian Seward开发并按照自由软件/开源软件协议发布的数据压缩算法及程序。对于压缩和解压缩,没有数据长度的限制,bzip2比传统的gzip的压缩效率更高,但是它的压缩速度较慢。

核心代码:

 @Override
public byte[] compress(byte[] data) throws IOException {
byte[] output;

Deflater compresser = new Deflater();

compresser.reset();
compresser.setInput(data);
compresser.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!compresser.finished()) {
int i = compresser.deflate(buf);
bos.write(buf, 0, i);
}
output = bos.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
compresser.end();
return output;
}

@Override
public byte[] uncompress(byte[] data) throws IOException {
byte[] output;

Inflater decompresser = new Inflater();
decompresser.reset();
decompresser.setInput(data);

ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
try {
byte[] buf = new byte[1024];
while (!decompresser.finished()) {
int i = decompresser.inflate(buf);
o.write(buf, 0, i);
}
output = o.toByteArray();
} catch (Exception e) {
output = data;
e.printStackTrace();
} finally {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}

decompresser.end();
return output;
}

测试结果:

图片

  • Gzip

gzip的实现算法还是deflate,只是在deflate格式上增加了文件头和文件尾,同样jdk也对gzip提供了支持,分别是GZIPOutputStream和GZIPInputStream类,同样可以发现GZIPOutputStream是继承于DeflaterOutputStream的,GZIPInputStream继承于InflaterInputStream,并且可以在源码中发现writeHeader和writeTrailer方法。

核心代码:

@Override
public byte[] compress(byte[] data) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;

try {
gzip = new GZIPOutputStream(out);
gzip.write(data);
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}

return out.toByteArray();
}

@Override
public byte[] uncompress(byte[] data) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(data);

try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[2048];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
}

return out.toByteArray();
}

测试结果:

图片

  • Bzip2

bzip2是Julian Seward开发并按照自由软件/开源软件协议发布的数据压缩算法及程序。Seward在1996年7月第一次公开发布了bzip2 0.15版,在随后几年中这个压缩工具稳定性得到改善并且日渐流行,Seward在2000年晚些时候发布了1.0版。bzip2比传统的gzip的压缩效率更高,但是它的压缩速度较慢。

核心代码:

@Override
  public byte[] compress(byte[] data) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bcos = new BZip2CompressorOutputStream(out);
    bcos.write(data);
    bcos.close();


    return out.toByteArray();
  }


  @Override
  public byte[] uncompress(byte[] data) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(data);


    try {
      @SuppressWarnings("resource")
      BZip2CompressorInputStream ungzip = new BZip2CompressorInputStream(in);
      byte[] buffer = new byte[2048];
      int n;
      while ((n = ungzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }


    return out.toByteArray();
  }

测试结果:

图片

  • Deflater

DEFLATE是同时使用了LZ77算法与哈夫曼编码(Huffman Coding)的一个无损数据压缩算法,DEFLATE压缩与解压的源代码可以在自由、通用的压缩库zlib上找到,zlib官网:http://www.zlib.net/ jdk中对zlib压缩库提供了支持,压缩类Deflater和解压类Inflater,Deflater和Inflater都提供了native方法。

核心代码:

@Override
public byte[] compress(byte[] data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Deflater compressor = new Deflater(1);

try {
compressor.setInput(data);
compressor.finish();
final byte[] buf = new byte[2048];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
} finally {
compressor.end();
}

return bos.toByteArray();
}

@Override
public byte[] uncompress(byte[] data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater();

try {
decompressor.setInput(data);
final byte[] buf = new byte[2048];
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
} catch (DataFormatException e) {
e.printStackTrace();
} finally {
decompressor.end();
}

return bos.toByteArray();
}

测试结果:

图片

  • Lz4

LZ4是一种无损数据压缩算法,着重于压缩和解压缩速度。

核心代码:

@Override
public byte[] compress(byte[] data) throws IOException {
LZ4Factory factory = LZ4Factory.fastestInstance();
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
LZ4Compressor compressor = factory.fastCompressor();
LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(byteOutput, 2048, compressor);
compressedOutput.write(data);
compressedOutput.close();

return byteOutput.toByteArray();
}

@Override
public byte[] uncompress(byte[] data) throws IOException {
LZ4Factory factory = LZ4Factory.fastestInstance();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
LZ4FastDecompressor decompresser = factory.fastDecompressor();
LZ4BlockInputStream lzis = new LZ4BlockInputStream(new ByteArrayInputStream(data), decompresser);

int count;
byte[] buffer = new byte[2048];
while ((count = lzis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
lzis.close();

return baos.toByteArray();
}

测试结果:

图片

  • Lzo

LZO是致力于解压速度的一种数据压缩算法,LZO是Lempel-Ziv-Oberhumer的缩写,这个算法是无损算法。

核心代码:

@Override
public byte[] compress(byte[] data) throws IOException {
LzoCompressor compressor = LzoLibrary.getInstance().newCompressor(LzoAlgorithm.LZO1X, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
LzoOutputStream cs = new LzoOutputStream(os, compressor);
cs.write(data);
cs.close();

return os.toByteArray();
}

@Override
public byte[] uncompress(byte[] data) throws IOException {
LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(LzoAlgorithm.LZO1X, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream is = new ByteArrayInputStream(data);
@SuppressWarnings("resource")
LzoInputStream us = new LzoInputStream(is, decompressor);

int count;
byte[] buffer = new byte[2048];
while ((count = us.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}

return baos.toByteArray();
}

测试结果:

图片

  • Snappy

Snappy(以前称Zippy)是Google基于LZ77的思路用C++语言编写的快速数据压缩与解压程序库,并在2011年开源。它的目标并非最大压缩率或与其他压缩程序库的兼容性,而是非常高的速度和合理的压缩率。

核心代码:

@Override
public byte[] compress(byte[] data) throws IOException {
return Snappy.compress(data);
}

@Override
public byte[] uncompress(byte[] data) throws IOException {
return Snappy.uncompress(data);
}

测试结果:

图片

3 性能对比

ENV:JDK:11/CPU:4C/

图片

不同大小文件压缩效率及质量有差异,性能对比仅供参考;

Compress Rate(%) = Size Before(byte) / Size After(byte) * 100%

源码地址

上传至Gitee仓库:

https://gitee.com/javanoteany/compress.git
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 通信数据
    +关注

    关注

    0

    文章

    13

    浏览量

    9933
  • RPC
    RPC
    +关注

    关注

    0

    文章

    102

    浏览量

    11423
  • 数据包
    +关注

    关注

    0

    文章

    229

    浏览量

    24095
收藏 人收藏

    评论

    相关推荐

    FPGA实现滑动平均滤波算法和LZW压缩算法

    采集数据中的量化噪声,在进行数据压缩前采用滤波的预处理技术。介绍LZW算法和滑动滤波算法的基本理论,详细阐述用单片FPGA实现两种
    发表于 04-24 09:05

    【TL6748 DSP申请】井下数据压缩技术

    申请理由:我是中石油渤海钻探工程公司定向井分公司的仪器工程师,目前我在研发一项科研项目,主要是关于数据压缩算法以及数据编解码方面技术研究。需要利用数据处理芯片来实现井下
    发表于 09-10 11:09

    请问有没有32可用的数据压缩算法

    了40M大小,手贱用rar压缩了一下,3.2M!!!,为了传输这40M的数据更改了工具的波特率和buffer,这样就和公司老产品不兼容了,如果STM32上能实现类似rar的数据压缩算法
    发表于 12-19 08:57

    MapReduce数据压缩的基本原则

    黑猴子的家:MapReduce数据压缩
    发表于 05-24 12:45

    数据压缩技术

    一、数据压缩的必要性二、多媒体数据压缩的可能性三、压缩方案应满足的要求四、编码方案分类五、数据压缩(编码)的主要步骤六、一些基本的压缩技术七
    发表于 03-25 13:19 35次下载

    基于DCT-TCQ的SAR原始数据压缩算法

    该文提出了一种基于离散余弦变换(DCT)和网格编码量化(TCQ)的SAR 原始数据压缩算法。SAR 原始数据可以看成是距离向和方位向的2 维线性调频信号的线性平移叠加,因而含有丰富的
    发表于 06-23 14:29 0次下载

    GPS定位数据压缩算法的设计与实现

    为了解决嵌入式GPS车载系统中存储空间小!GPS定位数据量大的矛盾" 根据GPS定位数据的特点"提出了专用于GPS定位数据压缩的改进型半字节压缩算法
    发表于 07-22 15:54 16次下载

    GPS定位数据压缩算法的设计与实现

    摘要:为了解决嵌入式GPS车载系统存储空间小、GPS定位数据量大的矛盾,根据GPS定位数据的特点,提出了专用于GPS定全数据压缩的改进型半字节压缩
    发表于 03-11 13:38 803次阅读
    GPS定位<b class='flag-5'>数据压缩</b><b class='flag-5'>算法</b>的设计与实现

    基于矢量量化编码的数据压缩算法的研究与实现

    基于矢量量化编码的数据压缩算法的研究与实现 As the rapid development of communications and information technology, data
    发表于 06-16 08:32 1639次阅读

    基于实时数据库的数据压缩算法

    本内容提出了基于实时数据库的数据压缩算法,希望对大家学习上有所帮助
    发表于 05-26 16:07 20次下载
    基于实时<b class='flag-5'>数据</b>库的<b class='flag-5'>数据压缩</b><b class='flag-5'>算法</b>

    小波算法在监测数据压缩中的应用

    小波算法在监测数据压缩中的应用
    发表于 02-07 18:22 16次下载

    基于DCT的阵列声波测井数据压缩算法_林博

    基于DCT的阵列声波测井数据压缩算法_林博
    发表于 03-19 19:07 1次下载

    基于运动状态改变的GPS轨迹数据压缩算法

    针对基于偏移量计算的轨迹数据压缩算法中对于关键点的评估不足以及基于在线轨迹数据压缩算法中累积误差和对偏移量考虑不足的问题,提出一种基于运动状态改变的在线全球定位系统( GPS)轨迹
    发表于 12-26 18:55 1次下载

    数据压缩的重要性

    数据压缩是指在不丢失有用信息的前提下,缩减数据量以减少存储空间,提高其传输、存储和处理效率,或按照一定的算法数据进行重新组织,减少数据的冗
    的头像 发表于 02-28 10:45 1.4w次阅读

    有趣!史记:数据压缩算法列传

    简单地说,如果没有数据压缩技术,我们就没法用 WinRAR 为 Email 中的附件瘦身;如果没有数据压缩技术,市场上的数码录音笔就只能记录不到20 分钟的语音;如果没有数据压缩技术
    的头像 发表于 11-11 15:21 541次阅读