一、前言
关于apache的commons-jcs-core源码包中的org.apache.commons.jcs.utils.zip.CompressionUtil数据压缩工具类,基于java.util.zip.GZIPInputStream输入压缩流并对java.io.ByteArrayInputStream、java.io.ByteArrayOutputStream输入输出流进行封装转换。
二、源码说明
package org.apache.commons.jcs.utils.zip;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.IOException;@b@import java.util.zip.DataFormatException;@b@import java.util.zip.Deflater;@b@import java.util.zip.GZIPInputStream;@b@import java.util.zip.Inflater;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public final class CompressionUtil@b@{@b@ private static final Log log = LogFactory.getLog(CompressionUtil.class);@b@@b@ public static byte[] decompressByteArray(byte[] input)@b@ {@b@ return decompressByteArray(input, 1024);@b@ }@b@@b@ public static byte[] decompressByteArray(byte[] input, int bufferLength)@b@ {@b@ if (null == input)@b@ {@b@ throw new IllegalArgumentException("Input was null");@b@ }@b@@b@ Inflater decompressor = new Inflater();@b@@b@ decompressor.setInput(input);@b@@b@ ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);@b@@b@ byte[] buf = new byte[bufferLength];@b@ try@b@ {@b@ while (!(decompressor.finished()))@b@ {@b@ int count = decompressor.inflate(buf);@b@ baos.write(buf, 0, count);@b@ }@b@ }@b@ catch (DataFormatException ex)@b@ {@b@ log.error("Problem decompressing.", ex);@b@ }@b@@b@ decompressor.end();@b@ try@b@ {@b@ baos.close();@b@ }@b@ catch (IOException ex)@b@ {@b@ log.error("Problem closing stream.", ex);@b@ }@b@@b@ return baos.toByteArray();@b@ }@b@@b@ public static byte[] compressByteArray(byte[] input)@b@ throws IOException@b@ {@b@ return compressByteArray(input, 1024);@b@ }@b@@b@ public static byte[] compressByteArray(byte[] input, int bufferLength)@b@ throws IOException@b@ {@b@ Deflater compressor = new Deflater();@b@ compressor.setLevel(9);@b@@b@ compressor.setInput(input);@b@ compressor.finish();@b@@b@ ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);@b@@b@ byte[] buf = new byte[bufferLength];@b@ while (!(compressor.finished()))@b@ {@b@ int count = compressor.deflate(buf);@b@ bos.write(buf, 0, count);@b@ }@b@@b@ compressor.end();@b@ bos.close();@b@@b@ return bos.toByteArray();@b@ }@b@@b@ public static byte[] decompressGzipByteArray(byte[] compressedByteArray)@b@ throws IOException@b@ {@b@ return decompressGzipByteArray(compressedByteArray, 1024);@b@ }@b@@b@ public static byte[] decompressGzipByteArray(byte[] compressedByteArray, int bufferlength)@b@ throws IOException@b@ {@b@ ByteArrayOutputStream uncompressedStream = new ByteArrayOutputStream();@b@@b@ GZIPInputStream compressedStream = new GZIPInputStream(new ByteArrayInputStream(compressedByteArray));@b@@b@ byte[] buffer = new byte[bufferlength];@b@@b@ int index = -1;@b@@b@ while ((index = compressedStream.read(buffer)) != -1)@b@ {@b@ uncompressedStream.write(buffer, 0, index);@b@ }@b@@b@ return uncompressedStream.toByteArray();@b@ }@b@}