Java解压缩技术的实现 GZIP ZIP BZIP2
与GZIP ZIP 不同的是BZIP2在Java中没有实现,BZIP2的实现是Apache提供的Commons-Compress.jar来实现的
关于 Commons Compress请移步:http://commons.apache.org/proper/commons-compress/
还是直接上代码
package com.ljh.bzip2; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.compress.compressors.CompressorException; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; /** * @Desc: BZip2 压缩工具(测试结果:适合较大压缩,用法与GZIP类似) * @author ljh * @date 2015-4-14 上午9:39:14 */ public class BZip2Utils { private static final int BUFFER = 8; public static final String EXT = ".bz2"; /** * @Description: GZIP 数据压缩 * @author (ljh) @date 2015-4-13 下午6:00:52 * @param data * @return * @throws IOException * @return byte[] * @throws CompressorException */ public static byte[] compress(byte[] data) throws IOException, CompressorException { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); compress(bais, baos);// 输入流压缩到输出流 baos.flush(); baos.close(); bais.close(); return baos.toByteArray(); } /** * @Description: GZIP 数据压缩 * @author (ljh) @date 2015-4-14 上午9:26:30 * @param is * @param os * @throws IOException * @return void * @throws CompressorException */ public static void compress(InputStream is, OutputStream os) throws IOException, CompressorException { BZip2CompressorOutputStream bzip2OS = new BZip2CompressorOutputStream(os, 8); // CompressorOutputStream bzip2OS = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.BZIP2, os); int count; byte data[] = new byte[BUFFER]; while ((count = is.read(data, 0, data.length)) != -1) { bzip2OS.write(data, 0, count); } bzip2OS.finish(); bzip2OS.flush(); bzip2OS.close(); } /** * @Description: GZIP 数据解压缩 * @author (ljh) @date 2015-4-13 下午6:00:42 * @param data * @return * @throws IOException * @return byte[] */ public static byte[] uncompress(byte[] data) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); uncompress(bais, baos); bais.close(); return baos.toByteArray(); } /** * @Description: GZIP 数据解压缩 * @author (ljh) @date 2015-4-14 上午9:26:51 * @param is * @param os * @throws IOException * @return void */ private static void uncompress(InputStream is, OutputStream os) throws IOException { BZip2CompressorInputStream bzip2IS = new BZip2CompressorInputStream(is); int count; byte data[] = new byte[BUFFER]; while ((count = bzip2IS.read(data, 0, data.length)) != -1) { os.write(data, 0, count); } os.flush(); os.close(); bzip2IS.close(); } /** * @Description: 文件压缩 * @author (ljh) @date 2015-4-14 上午9:28:46 * @param file * @throws Exception * @return void * @throws CompressorException */ public static void compress(File file) throws IOException, CompressorException { compress(file, true); } /** * @Description: 文件压缩 * @author (ljh) @date 2015-4-14 上午9:29:21 * @param file * @param delete * 是否删除源文件 * @throws Exception * @return void * @throws IOException * @throws CompressorException */ public static void compress(File file, boolean delete) throws IOException, CompressorException { if (!file.exists()) { // 文件不存在 } FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getPath() + EXT); compress(fis, fos); fis.close(); fos.flush(); fos.close(); if (delete) { file.delete(); } } /** * @Description: 文件压缩 * @author (ljh) @date 2015-4-14 上午9:32:52 * @param path * 源文件路径 * @throws Exception * @return void * @throws CompressorException */ public static void compress(String path) throws IOException, CompressorException { compress(path, true); } /** * @Description: 文件压缩 * @author (ljh) @date 2015-4-14 上午9:33:18 * @param path * 源文件路径 * @param delete * 是否删除源文件 * @throws Exception * @return void * @throws IOException * @throws CompressorException */ public static void compress(String path, boolean delete) throws IOException, CompressorException { File file = new File(path); compress(file, delete); } /** * @Description: 文件解压缩 * @author (ljh) @date 2015-4-14 上午9:35:03 * @param file * @throws Exception * @return void */ public static void uncompress(File file) throws IOException { uncompress(file, true); } /** * @Description: 文件解压缩 * @author (ljh) @date 2015-4-14 上午9:35:44 * @param file * @param delete * 是否删除源文件 * @throws Exception * @return void * @throws IOException */ public static void uncompress(File file, boolean delete) throws IOException { if (!file.exists()) { // 文件不存在 } FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT, "")); uncompress(fis, fos); fis.close(); fos.flush(); fos.close(); if (delete) { file.delete(); } } /** * @Description: 文件解压缩 * @author (ljh) @date 2015-4-14 上午9:37:48 * @param path * @throws Exception * @return void */ public static void uncompress(String path) throws IOException { uncompress(path, true); } /** * @Description: 文件解压缩 * @author (ljh) @date 2015-4-14 上午9:38:03 * @param path * @param delete * 是否删除源文件 * @throws Exception * @return void */ public static void uncompress(String path, boolean delete) throws IOException { File file = new File(path); uncompress(file, delete); } /** * Tips: * Commons Compress不仅支持BZip2算法实现,同时也支持GZip算法实现。 * 对于GZip算法实现,与Java原生实现基本上没有什么差别。其源代码分析,仅仅是做了简单的包装。 * 不过有必要提及的一点是,Commons Compress为压缩(GZip和BZip2)构建了压缩算法工厂类CompressorStreamFactory。 * 通过这个类可以方便的构建GZip和BZip2的输入输出流,关键字分别为“gz”和“bzip2”。 */ }
测试代码:
@org.junit.Test public final void testBZIP2() throws IOException, CompressorException { byte[] bytes = "阿莱克斯大家噶拉卡死机的个啊了的卡死机格拉卡死机的了感觉打开上岙地沟阿萨德gas的歌撒的歌第三个滴答滴答滴答滴答滴答滴答滴答".getBytes(); // 对bytes压缩,验证一下压缩后的效果对比 System.out.println("压缩前:"); System.out.println(bytes.length); for (byte b : bytes) { System.out.print(b + " "); } System.out.println(); System.out.println("压缩后:"); byte[] bytes2 = BZip2Utils.compress(bytes); System.out.println(bytes2.length); for (byte b : bytes2) { System.out.print(b + " "); } System.out.println(); System.out.println("解压缩后:"); byte[] byte22 = BZip2Utils.uncompress(bytes2); System.out.println(byte22.length); for (byte b : byte22) { System.out.print(b+" "); } //压缩时间较长,测试压缩12305KB文件用时17.203s,压缩后大小383KB // BZip2Utils.compress("F:\\Activity_win9.bmp", false); //解压用时4.667s BZip2Utils.uncompress("F:\\Activity_win9.bmp.bz2" ,false);//解压缩后与源文件相同 //要压缩文件夹请参考ZIPUtils自行实现 }
Commons
Compress不仅支持BZip2算法实现,同时也支持GZip算法实现。对于GZip算法实现,与Java原生实现基本上没有什么差别。其源代码分析,仅仅是做了简单的包装。
不过有必要提及的一点是,Commons Compress为压缩(GZip和BZip2)构建了压缩算法工厂类CompressorStreamFactory。通过这个类可以方便的构建GZip和BZip2的输入输出流,关键字分别为“gz”和“bzip2”。
GZip
// GzipCompressorInputStream CompressorInputStream gzipIn = new CompressorStreamFactory().createCompressorInputStream("gz", is); // GzipCompressorOutputStream CompressorOutputStream gzipOut = new CompressorStreamFactory().createCompressorOutputStream("gz", os);
BZip2
// BZip2CompressorInputStream CompressorInputStream bzip2In = new CompressorStreamFactory().createCompressorInputStream("bzip2", is); // BZip2CompressorOutputStream CompressorOutputStream bzip2Out = new CompressorStreamFactory().createCompressorOutputStream("bzip2", os);
GZip和BZip2在算法实现步骤上基本上没有什么差别,如果有必要统一,可按上述代码实现!
时间: 2024-11-05 11:55:19