java使用gzip實現檔案解壓縮樣本_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

package com.cjonline.foundation.cpe.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public abstract class GZipUtils { 

    public static final int BUFFER = 1024; 
    public static final String EXT = ".gz"; 

    /**
     * 資料壓縮
     * 
     * @param data
     * @return
     * @throws Exception
     */ 
    public static byte[] compress(byte[] data) throws Exception { 
        ByteArrayInputStream bais = new ByteArrayInputStream(data); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

        // 壓縮 
        compress(bais, baos); 

        byte[] output = baos.toByteArray(); 

        baos.flush(); 
        baos.close(); 

        bais.close(); 

        return output; 
    } 

    /**
     * 檔案壓縮
     * 
     * @param file
     * @throws Exception
     */ 
    public static void compress(File file) throws Exception { 
        compress(file, true); 
    } 

    /**
     * 檔案壓縮
     * 
     * @param file
     * @param delete
     *            是否刪除原始檔案
     * @throws Exception
     */ 
    public static void compress(File file, boolean delete) throws Exception { 
        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(); 
        } 
    } 

    /**
     * 資料壓縮
     * 
     * @param is
     * @param os
     * @throws Exception
     */ 
    public static void compress(InputStream is, OutputStream os) 
            throws Exception { 

        GZIPOutputStream gos = new GZIPOutputStream(os); 

        int count; 
        byte data[] = new byte[BUFFER]; 
        while ((count = is.read(data, 0, BUFFER)) != -1) { 
            gos.write(data, 0, count); 
        } 

        gos.finish(); 

        gos.flush(); 
        gos.close(); 
    } 

    /**
     * 檔案壓縮
     * 
     * @param path
     * @throws Exception
     */ 
    public static void compress(String path) throws Exception { 
        compress(path, true); 
    } 

    /**
     * 檔案壓縮
     * 
     * @param path
     * @param delete
     *            是否刪除原始檔案
     * @throws Exception
     */ 
    public static void compress(String path, boolean delete) throws Exception { 
        File file = new File(path); 
        compress(file, delete); 
    } 

    /**
     * 資料解壓縮
     * 
     * @param data
     * @return
     * @throws Exception
     */ 
    public static byte[] decompress(byte[] data) throws Exception { 
        ByteArrayInputStream bais = new ByteArrayInputStream(data); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

        // 解壓縮 

        decompress(bais, baos); 

        data = baos.toByteArray(); 

        baos.flush(); 
        baos.close(); 

        bais.close(); 

        return data; 
    } 

    /**
     * 檔案解壓縮
     * 
     * @param file
     * @throws Exception
     */ 
    public static void decompress(File file) throws Exception { 
        decompress(file, true); 
    } 

    /**
     * 檔案解壓縮
     * 
     * @param file
     * @param delete
     *            是否刪除原始檔案
     * @throws Exception
     */ 
    public static void decompress(File file, boolean delete) throws Exception { 
        FileInputStream fis = new FileInputStream(file); 
        FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT, 
                "")); 
        decompress(fis, fos); 
        fis.close(); 
        fos.flush(); 
        fos.close(); 

        if (delete) { 
            file.delete(); 
        } 
    } 

    /**
     * 資料解壓縮
     * 
     * @param is
     * @param os
     * @throws Exception
     */ 
    public static void decompress(InputStream is, OutputStream os) 
            throws Exception { 

        GZIPInputStream gis = new GZIPInputStream(is); 

        int count; 
        byte data[] = new byte[BUFFER]; 
        while ((count = gis.read(data, 0, BUFFER)) != -1) { 
            os.write(data, 0, count); 
        } 

        gis.close(); 
    } 

    /**
     * 檔案解壓縮
     * 
     * @param path
     * @throws Exception
     */ 
    public static void decompress(String path) throws Exception { 
        decompress(path, true); 
    } 

    /**
     * 檔案解壓縮
     * 
     * @param path
     * @param delete
     *            是否刪除原始檔案
     * @throws Exception
     */ 
    public static void decompress(String path, boolean delete) throws Exception { 
        File file = new File(path); 
        decompress(file, delete); 
    } 
}

相關文章

聯繫我們

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