一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

數(shù)據(jù)壓縮算法的介紹

汽車電子技術(shù) ? 來源:Java爛筆頭 ? 作者:小週 ? 2023-02-28 14:25 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

1 前言

在RPC通信數(shù)據(jù)的傳輸場景下,當(dāng)通信報(bào)文數(shù)據(jù)傳輸較大時(shí),會對數(shù)據(jù)包進(jìn)行壓縮傳輸,根據(jù)不同傳輸場景,常用的壓縮算法有Zlib、Gzip、Bzip2、Deflater、Lz4、Lzo、Snappy算法等。以下將包括算法的介紹、Java實(shí)現(xiàn)代碼以及各算法間的模擬性能對比。

2 壓縮方案

  • Zlib

bzip2是Julian Seward開發(fā)并按照自由軟件/開源軟件協(xié)議發(fā)布的數(shù)據(jù)壓縮算法及程序。對于壓縮和解壓縮,沒有數(shù)據(jù)長度的限制,bzip2比傳統(tǒng)的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;
}

測試結(jié)果:

圖片

  • Gzip

gzip的實(shí)現(xiàn)算法還是deflate,只是在deflate格式上增加了文件頭和文件尾,同樣jdk也對gzip提供了支持,分別是GZIPOutputStream和GZIPInputStream類,同樣可以發(fā)現(xiàn)GZIPOutputStream是繼承于DeflaterOutputStream的,GZIPInputStream繼承于InflaterInputStream,并且可以在源碼中發(fā)現(xiàn)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();
}

測試結(jié)果:

圖片

  • Bzip2

bzip2是Julian Seward開發(fā)并按照自由軟件/開源軟件協(xié)議發(fā)布的數(shù)據(jù)壓縮算法及程序。Seward在1996年7月第一次公開發(fā)布了bzip2 0.15版,在隨后幾年中這個(gè)壓縮工具穩(wěn)定性得到改善并且日漸流行,Seward在2000年晚些時(shí)候發(fā)布了1.0版。bzip2比傳統(tǒng)的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();
  }

測試結(jié)果:

圖片

  • Deflater

DEFLATE是同時(shí)使用了LZ77算法與哈夫曼編碼(Huffman Coding)的一個(gè)無損數(shù)據(jù)壓縮算法,DEFLATE壓縮與解壓的源代碼可以在自由、通用的壓縮庫zlib上找到,zlib官網(wǎng):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();
}

測試結(jié)果:

圖片

  • Lz4

LZ4是一種無損數(shù)據(jù)壓縮算法,著重于壓縮和解壓縮速度。

核心代碼:

@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();
}

測試結(jié)果:

圖片

  • Lzo

LZO是致力于解壓速度的一種數(shù)據(jù)壓縮算法,LZO是Lempel-Ziv-Oberhumer的縮寫,這個(gè)算法是無損算法。

核心代碼:

@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();
}

測試結(jié)果:

圖片

  • Snappy

Snappy(以前稱Zippy)是Google基于LZ77的思路用C++語言編寫的快速數(shù)據(jù)壓縮與解壓程序庫,并在2011年開源。它的目標(biāo)并非最大壓縮率或與其他壓縮程序庫的兼容性,而是非常高的速度和合理的壓縮率。

核心代碼:

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

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

測試結(jié)果:

圖片

3 性能對比

ENV:JDK:11/CPU:4C/

圖片

不同大小文件壓縮效率及質(zhì)量有差異,性能對比僅供參考;

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

源碼地址

上傳至Gitee倉庫:

https://gitee.com/javanoteany/compress.git
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 通信數(shù)據(jù)
    +關(guān)注

    關(guān)注

    0

    文章

    13

    瀏覽量

    10043
  • RPC
    RPC
    +關(guān)注

    關(guān)注

    0

    文章

    111

    瀏覽量

    11842
  • 數(shù)據(jù)包
    +關(guān)注

    關(guān)注

    0

    文章

    269

    瀏覽量

    24944
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點(diǎn)推薦

    FPGA實(shí)現(xiàn)滑動平均濾波算法和LZW壓縮算法

    采集數(shù)據(jù)中的量化噪聲,在進(jìn)行數(shù)據(jù)壓縮前采用濾波的預(yù)處理技術(shù)。介紹LZW算法和滑動濾波算法的基本理論,詳細(xì)闡述用單片F(xiàn)PGA實(shí)現(xiàn)兩種
    發(fā)表于 04-24 09:05

    【TL6748 DSP申請】井下數(shù)據(jù)壓縮技術(shù)

    申請理由:我是中石油渤海鉆探工程公司定向井分公司的儀器工程師,目前我在研發(fā)一項(xiàng)科研項(xiàng)目,主要是關(guān)于數(shù)據(jù)壓縮算法以及數(shù)據(jù)編解碼方面技術(shù)研究。需要利用數(shù)據(jù)處理芯片來實(shí)現(xiàn)井下
    發(fā)表于 09-10 11:09

    請問有沒有32可用的數(shù)據(jù)壓縮算法?

    了40M大小,手賤用rar壓縮了一下,3.2M?。?!,為了傳輸這40M的數(shù)據(jù)更改了工具的波特率和buffer,這樣就和公司老產(chǎn)品不兼容了,如果STM32上能實(shí)現(xiàn)類似rar的數(shù)據(jù)壓縮算法
    發(fā)表于 12-19 08:57

    數(shù)據(jù)壓縮技術(shù)

    一、數(shù)據(jù)壓縮的必要性二、多媒體數(shù)據(jù)壓縮的可能性三、壓縮方案應(yīng)滿足的要求四、編碼方案分類五、數(shù)據(jù)壓縮(編碼)的主要步驟六、一些基本的壓縮技術(shù)七
    發(fā)表于 03-25 13:19 ?35次下載

    GPS定位數(shù)據(jù)壓縮算法的設(shè)計(jì)與實(shí)現(xiàn)

    為了解決嵌入式GPS車載系統(tǒng)中存儲空間小!GPS定位數(shù)據(jù)量大的矛盾" 根據(jù)GPS定位數(shù)據(jù)的特點(diǎn)"提出了專用于GPS定位數(shù)據(jù)壓縮的改進(jìn)型半字節(jié)壓縮算法
    發(fā)表于 07-22 15:54 ?16次下載

    GPS定位數(shù)據(jù)壓縮算法的設(shè)計(jì)與實(shí)現(xiàn)

    摘要:為了解決嵌入式GPS車載系統(tǒng)存儲空間小、GPS定位數(shù)據(jù)量大的矛盾,根據(jù)GPS定位數(shù)據(jù)的特點(diǎn),提出了專用于GPS定全數(shù)據(jù)壓縮的改進(jìn)型半字節(jié)壓縮
    發(fā)表于 03-11 13:38 ?946次閱讀
    GPS定位<b class='flag-5'>數(shù)據(jù)壓縮</b><b class='flag-5'>算法</b>的設(shè)計(jì)與實(shí)現(xiàn)

    基于矢量量化編碼的數(shù)據(jù)壓縮算法的研究與實(shí)現(xiàn)

    基于矢量量化編碼的數(shù)據(jù)壓縮算法的研究與實(shí)現(xiàn) As the rapid development of communications and information technology, data
    發(fā)表于 06-16 08:32 ?1881次閱讀

    基于實(shí)時(shí)數(shù)據(jù)庫的數(shù)據(jù)壓縮算法

    本內(nèi)容提出了基于實(shí)時(shí)數(shù)據(jù)庫的數(shù)據(jù)壓縮算法,希望對大家學(xué)習(xí)上有所幫助
    發(fā)表于 05-26 16:07 ?20次下載
    基于實(shí)時(shí)<b class='flag-5'>數(shù)據(jù)</b>庫的<b class='flag-5'>數(shù)據(jù)壓縮</b><b class='flag-5'>算法</b>

    小波算法在監(jiān)測數(shù)據(jù)壓縮中的應(yīng)用

    小波算法在監(jiān)測數(shù)據(jù)壓縮中的應(yīng)用
    發(fā)表于 02-07 18:22 ?16次下載

    基于DCT的陣列聲波測井數(shù)據(jù)壓縮算法_林博

    基于DCT的陣列聲波測井數(shù)據(jù)壓縮算法_林博
    發(fā)表于 03-19 19:07 ?1次下載

    使用遺傳算法對時(shí)空數(shù)據(jù)壓縮策略優(yōu)化

    針對時(shí)空數(shù)據(jù)因?yàn)橛袚p壓縮導(dǎo)致的還原精度不高的問題,提出了使用遺傳算法對時(shí)空數(shù)據(jù)壓縮策略進(jìn)行優(yōu)化。算法模擬生物進(jìn)化過程,首先,在初始階段,根據(jù)
    發(fā)表于 12-05 14:18 ?0次下載
    使用遺傳<b class='flag-5'>算法</b>對時(shí)空<b class='flag-5'>數(shù)據(jù)壓縮</b>策略優(yōu)化

    基于運(yùn)動狀態(tài)改變的GPS軌跡數(shù)據(jù)壓縮算法

    針對基于偏移量計(jì)算的軌跡數(shù)據(jù)壓縮算法中對于關(guān)鍵點(diǎn)的評估不足以及基于在線軌跡數(shù)據(jù)壓縮算法中累積誤差和對偏移量考慮不足的問題,提出一種基于運(yùn)動狀態(tài)改變的在線全球定位系統(tǒng)( GPS)軌跡
    發(fā)表于 12-26 18:55 ?1次下載

    數(shù)據(jù)壓縮的重要性

    數(shù)據(jù)壓縮是指在不丟失有用信息的前提下,縮減數(shù)據(jù)量以減少存儲空間,提高其傳輸、存儲和處理效率,或按照一定的算法數(shù)據(jù)進(jìn)行重新組織,減少數(shù)據(jù)的冗
    的頭像 發(fā)表于 02-28 10:45 ?1.5w次閱讀

    數(shù)據(jù)壓縮算法計(jì)算步驟及過程

    一種非常簡單的壓縮方法是行程長度編碼,這種方法使用數(shù)據(jù)數(shù)據(jù)長度這樣簡單的編碼代替同樣的連續(xù)數(shù)據(jù),這是無損數(shù)據(jù)壓縮的一個(gè)實(shí)例。這種方法經(jīng)常用
    的頭像 發(fā)表于 02-28 10:51 ?1.2w次閱讀
    <b class='flag-5'>數(shù)據(jù)壓縮</b><b class='flag-5'>算法</b>計(jì)算步驟及過程

    有趣!史記:數(shù)據(jù)壓縮算法列傳

    簡單地說,如果沒有數(shù)據(jù)壓縮技術(shù),我們就沒法用 WinRAR 為 Email 中的附件瘦身;如果沒有數(shù)據(jù)壓縮技術(shù),市場上的數(shù)碼錄音筆就只能記錄不到20 分鐘的語音;如果沒有數(shù)據(jù)壓縮技術(shù)
    的頭像 發(fā)表于 11-11 15:21 ?1073次閱讀