更换测试方法 添加ByteArrayOutputStream将写入中间缓冲层
解压代码如下
/**
* 压缩测试
* 解压
*/
@Test
public void testDeCompresss() throws IOException {
//31, -117, 8, 0, 0, 0, 0, 0, 0, -1, -29, -45, -74, -16, -47, 22, 55, -29, -31, 81, -32, -61, -62, 2, 0, 92, -1, -110, 110, 30, 0, 0, 0
byte[] buf=new byte[]{31, -117, 8, 0, 0, 0, 0, 0, 0, -1, -29, -45, -74, -16, -47, 22, 55, -29, -31, 81, -32, -61, -62, 2, 0, 92, -1, -110, 110, 30, 0, 0, 0};
//中间缓存
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ByteArrayInputStream byteArrayinputStream=new ByteArrayInputStream(buf);
GZIPInputStream gzipInputStream=new GZIPInputStream(byteArrayinputStream);
//readAllBytes 的版本是9
// byte[] bytes = gzipInputStream.readAllBytes(buf.length);
int len;
while ((len = gzipInputStream.read(buf)) != -1) {
byteArrayOutputStream.write(buf, 0, len);
}
byte[] bytes = byteArrayOutputStream.toByteArray();
//数据越多 压缩更好
System.out.println(bytes.length+"--->"+buf.length);
System.out.println(Arrays.toString(bytes));
}
压缩代码如下
/**
* 压缩测试
* 压缩
*/
@Test
public void testCompresss() throws IOException {
byte[] buf=new byte[]{14,43,56,76,43,23,54,12,12,32,14,43,56,76,43,23,54,12,12,32,14,43,56,76,43,23,54,12,12,32};
//中间缓存
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream=new GZIPOutputStream(byteArrayOutputStream);
gzipOutputStream.write(buf);
gzipOutputStream.finish();
//获得新的字节对象
byte[] bytes=byteArrayOutputStream.toByteArray();
//数据越多 压缩更好
System.out.println(bytes.length+"--->"+buf.length);
System.out.println(Arrays.toString(bytes));
}