字符串解压缩类库(zip、GZIP、QuickLz、snappy、lzf、jzlib)介绍

2 篇文章 0 订阅

1ZIP GZIP  计算机文件压缩算法,JDKjava.util.zip.*中实现。主要包括ZipInputStream/ ZipOutputStreamGZipInputStream/ ZipOutputStream

2QuickLZ是一个号称世界压缩速度最快的压缩库,并且也是个开源的压缩库,其遵守 GPL 1, 2 3协议。

3Snappy是一个 C++的用来压缩和解压缩的开发包,其目标不是最大限度压缩,而且不兼容其他压缩格式。旨在提供高速压缩速度和合理的压缩率。在64位模式的 Core i7 处理器上,可达每秒250~500兆的压缩速度。在 Google 内部被广泛的使用,从 BigTable MapReduce以及内部的RPC 系统。

4LZF采用类似lz77lzss的混合编码,针对字符串压缩算法。 

5JZLIB是纯java的开源解压、压缩包,与JDKZLIB类似。

 

预选解压缩类库使用介绍--ZIP

压缩

String  s = “这是一个用于测试的字符串”;

ByteArrayOutputStream out = new ByteArrayOutputStream();

ZipOutputStreamzout = new ZipOutputStream(out);

zout.putNextEntry(new ZipEntry("0"));

zout.write(s.getBytes());

zout.closeEntry();

byte[] compressed = out.toByteArray();   --返回压缩后的字符串的字节数组

解压

ByteArrayOutputStream out = new ByteArrayOutputStream();

ByteArrayInputStream in = new ByteArrayInputStream(compressed);

ZipInputStreamzin = new ZipInputStream(in);

zin.getNextEntry();

byte[] buffer = new byte[1024];

intoffset = -1;

while ((offset = zin.read(buffer))!= -1) {

       out.write(buffer, 0, offset);

}

byte[] uncompressed = out.toByteArray();   --返回解压缩后的字符串的字节数组

 

预选解压缩类库使用介绍--GZIP

压缩

String  s = “这是一个用于测试的字符串”;

ByteArrayOutputStream out = new ByteArrayOutputStream();

GZipOutputStream gout = new GZipOutputStream(out);

gout.write(s.getBytes());

byte[] compressed = out.toByteArray();   --返回压缩后的字符串的字节数组

解压

ByteArrayOutputStream out = new ByteArrayOutputStream();

ByteArrayInputStream in = new ByteArrayInputStream(compressed);

GZipInputStreamgzin =newGZipInputStream(in);

byte[] buffer = new byte[1024];

intoffset = -1;

while ((offset = gzin.read(buffer)) != -1) {

       out.write(buffer, 0, offset);

}

byte[] uncompressed = out.toByteArray();   --返回解压缩后的字符串的字节数组

 

预选解压缩类库使用介绍--QuickLZ

压缩

String  s = “这是一个用于测试的字符串”;

--Level 1

byte[] compressed =QuickLZ.compress(s.getBytes(), 1);   --返回压缩后的字符串的字节数组

--Level3

byte[] compressed =QuickLZ.compress(s.getBytes(), 3);   --返回压缩后的字符串的字节数组

解压

byte[] uncompressed =QuickLZ.decompress(compressed );   --返回解压缩后的字符串的字节数组

 

预选解压缩类库使用介绍--Snappy

压缩

String  s = “这是一个用于测试的字符串”;

byte[] compressed =Snappy.compress(s.getBytes());   --返回压缩后的字符串的字节数组

解压

byte[] uncompressed =Snappy.uncompress(compressed );   --返回解压缩后的字符串的字节数组

 

预选解压缩类库使用介绍-- LZF

压缩

String  s = “这是一个用于测试的字符串”;

byte[] compressed = LZFEncoder.encode(s.getBytes());   --返回压缩后的字符串的字节数组

解压

byte[] uncompressed = LZFDecoder.decode(compressed );   --返回解压缩后的字符串的字节数组

 

预选解压缩类库使用介绍-- JZLIB

压缩

String  s = “这是一个用于测试的字符串”;

ByteArrayOutputStream out = new ByteArrayOutputStream();

DeflaterOutputStreamdout = new DeflaterOutputStream(out);

dout.write(s.getBytes());

dout.close();         --需要先关闭

byte[] compressed = out.toByteArray();   --返回压缩后的字符串的字节数组

解压

ByteArrayOutputStream out= new ByteArrayOutputStream();

ByteArrayInputStream  in =  new ByteArrayInputStream(compressedStr);

InflaterInputStream input = new InflaterInputStream(in);

byte[] buffer = new byte[1024];

intoffset = -1;

while ((offset = input.read(buffer)) != -1) {

        out.write(buffer, 0, offset);

}

out.close();   --需要先关闭

byte[] uncompressed = out.toByteArray();   --返回解压缩后的字符串的字节数组

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值