Java解壓縮技術(二)GZIP壓縮-解壓縮(

來源:互聯網
上載者:User

標籤:bzip2   gzip   java壓縮   zip   資料壓縮   


Java解壓縮技術的實現 GZIP ZIP BZIP2


沒啥好說的,都是些檔案IO操作


package com.ljh.gzip;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 java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;/** * @Desc: GZIP 壓縮公用程式(資料壓縮建議使用,支援單檔案壓縮) * 要壓縮檔夾請參考ZipUtils類 * Tips:GZIP也可以採用Apache開源的Commons-Compress.jar的實現 * @author ljh * @date 2015-4-14 上午9:39:14 */public class GZipUtils {private static final int BUFFER = 1024;public static final String EXT = ".gz";/** * @Description: GZIP 資料壓縮 * @author (ljh) @date 2015-4-13 下午6:00:52 * @param data * @return * @throws IOException * @return byte[] */public static byte[] compress(byte[] data) throws IOException {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 */public static void compress(InputStream is, OutputStream os) throws IOException {GZIPOutputStream gzipOS = new GZIPOutputStream(os, 1024);int count;byte data[] = new byte[BUFFER];while ((count = is.read(data, 0, data.length)) != -1) {gzipOS.write(data, 0, count);}gzipOS.finish();gzipOS.flush();gzipOS.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(BUFFER);uncompress(bais, baos);baos.flush();baos.close();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 {GZIPInputStream gzipIS = new GZIPInputStream(is);int count;byte data[] = new byte[BUFFER];while ((count = gzipIS.read(data, 0, data.length)) != -1) {os.write(data, 0, count);}gzipIS.close();}/** * @Description: 檔案壓縮 * @author (ljh) @date 2015-4-14 上午9:28:46 * @param file * @throws Exception * @return void */public static void compress(File file) throws IOException {compress(file, true);}/** * @Description: 檔案壓縮 * @author (ljh) @date 2015-4-14 上午9:29:21 * @param file * @param delete *            是否刪除源檔案 * @throws Exception * @return void * @throws IOException */public static void compress(File file, boolean delete) throws IOException {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 */public static void compress(String path) throws IOException {compress(path, true);}/** * @Description: 檔案壓縮 * @author (ljh) @date 2015-4-14 上午9:33:18 * @param path *            源檔案路徑 * @param delete *            是否刪除源檔案 * @throws Exception * @return void * @throws IOException */public static void compress(String path, boolean delete) throws IOException {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);}}


測試代碼:


/** * @Description: 中文150位元組壓縮比較明顯,英文50位元組壓縮比較明顯 * @author (ljh) @date 2015-4-14 上午11:37:37  * @throws IOException  * @return void *///@org.junit.Testpublic void testGZIP() throws IOException {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 = GZipUtils.compress(bytes);System.out.println(bytes2.length);for (byte b : bytes2) {System.out.print(b + " ");}System.out.println();System.out.println("解壓縮後:");        byte[] byte22 = GZipUtils.uncompress(bytes2);        System.out.println(byte22.length);        for (byte b : byte22) {        System.out.print(b+" ");}        //GZIP工具//        GZipUtils.compress("F:\\Activity_win9.bmp", false);//被壓縮檔是一個12M的bmp檔案,壓縮後大小為1.2M//        GZipUtils.uncompress("F:\\Activity_win9.bmp.gz", false);//解壓縮後與源檔案相同        }


點我下載相關源碼



Java解壓縮技術(二)GZIP壓縮-解壓縮(

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.