Java压缩/解压缩二进制文件
来源:互联网 发布:js导入导出表格插件 编辑:程序博客网 时间:2024/06/03 02:09
在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;
- }
- }
- }
/** * 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();
- }
@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/
- Java压缩/解压缩二进制文件
- Java压缩/解压缩二进制文件
- Java压缩/解压缩二进制文件--http中content-encoding:deflate也可以使用
- java zip压缩。解压缩
- JAVA 字符串压缩解压缩
- java实现压缩解压缩
- java图片压缩、解压缩
- java 压缩解压缩文件
- java压缩、解压缩文件
- Java String 压缩、解压缩
- java压缩解压缩
- Java 压缩/解压缩 文件
- java实现压缩与解压缩!
- java 文件压缩与解压缩
- Java压缩解压缩zip文件
- java 压缩&解压缩 工具类
- Java压缩解压缩zip文件
- Java实现zip压缩/解压缩
- <s:iterator> 中的status 使用方法
- centos下openmotif安装
- Hadoop : MapReduce中的Shuffle和Sort分析
- USB枚举过程
- 动态库so打包到android apk
- Java压缩/解压缩二进制文件
- asp中SQLHelper的引用问题
- oradebug的简单介绍及应用
- JAVA线程池的分析和使用
- 经典中的经典,启发人类。
- 地震:谷歌如何用“神经网络”改造Android
- Hibernate 无主键,生成2个JAVA类的查询方法
- ios系统越狱才可以享受的100项操作
- Rigidbody.AddForce 添加力