Java压缩/解压缩二进制文件

在Java中提供 Deflater Inflater 工具类来压缩/解压缩数据。 这两个工具类采用 zlib 算法,下面给出一个封装好的工具。

/**
 * util for compress/decompress data
 * 
 * @author lichengwu
 * @version 1.0
 * @created 2013-02-07 10:14 AM
 */
public final class CompressionUtil {

    private static final int BUFFER_SIZE = 4 * 1024;

    /**
     * compress data by {@linkplain Level}
     * 
     * @author lichengwu
     * @created 2013-02-07
     * 
     * @param data
     * @param level
     *            see {@link Level}
     * @return
     * @throws IOException
     */
    public static byte[] compress(byte[] data, Level level) throws IOException {

        Assert.notNull(data);
        Assert.notNull(level);

        Deflater deflater = new Deflater();
        // set compression level
        deflater.setLevel(level.getLevel());
        deflater.setInput(data);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

        deflater.finish();
        byte[] buffer = new byte[BUFFER_SIZE];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated
                                                  // code... index
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
		outputStream.close();
        return output;
    }

    /**
     * decompress data
     * 
     * @author lichengwu
     * @created 2013-02-07
     * 
     * @param data
     * @return
     * @throws IOException
     * @throws DataFormatException
     */
    public static byte[] decompress(byte[] data) throws IOException, DataFormatException {

        Assert.notNull(data);

        Inflater inflater = new Inflater();
        inflater.setInput(data);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[BUFFER_SIZE];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
		outputStream.close();
        return output;
    }

    /**
     * Compression level
     */
    public static enum Level {

        /**
         * Compression level for no compression.
         */
        NO_COMPRESSION(0),

        /**
         * Compression level for fastest compression.
         */
        BEST_SPEED(1),

        /**
         * Compression level for best compression.
         */
        BEST_COMPRESSION(9),

        /**
         * Default compression level.
         */
        DEFAULT_COMPRESSION(-1);

        private int level;

        Level(

        int level) {
            this.level = level;
        }
        public int getLevel() {
            return level;
        }
    }  
}

下面是一个测试:

@Test
public void testCompress() throws Exception {

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(
            "/Users/lichengwu/tmp/out_put.txt.bak"));
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

    byte[] temp = new byte[1024];
    int size = 0;
    while ((size = in.read(temp)) != -1) {
        out.write(temp, 0, size);
    }
    in.close();
    byte[] data = out.toByteArray();
    byte[] output = CompressionUtil.compress(data, CompressionUtil.Level.BEST_COMPRESSION);
    System.out.println("before : " + (data.length / 1024) + "k");
    System.out.println("after : " + (output.length / 1024) + "k");

    FileOutputStream fos = new FileOutputStream("/Users/lichengwu/tmp/out_put.txt.bak.compress");
    fos.write(output);
    out.close();
    fos.close();
}

@Test
public void testDecompress() throws Exception {

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(
            "/Users/lichengwu/tmp/out_put.txt.bak.compress"));
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    byte[] temp = new byte[1024];
    int size = 0;
    while ((size = in.read(temp)) != -1) {
        out.write(temp, 0, size);
    }
    in.close();
    byte[] data = out.toByteArray();
    byte[] output = CompressionUtil.decompress(data);
    System.out.println("before : " + (data.length / 1024) + "k");
    System.out.println("after : " + (output.length / 1024) + "k");

    FileOutputStream fos = new FileOutputStream("/Users/lichengwu/tmp/out_put.txt.bak.decompress");
    fos.write(output);
    out.close();
    fos.close();
}

关于OutOfMemoryError,请参考:http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值