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

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

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();   --返回解压缩后的字符串的字节数组

猜你喜欢

转载自blog.csdn.net/kwame211/article/details/80365589