Java解壓縮技術(三)BZIP2壓縮-解壓縮

來源:互聯網
上載者:User

標籤:bzip2   gzip   java壓縮   zip   壓縮   


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.Testpublic 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在演算法實現步驟上基本上沒有什麼差別,如果有必要統一,可按上述代碼實現! 



點我下載相關源碼



Java解壓縮技術(三)BZIP2壓縮-解壓縮

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.