国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java使用gzip實現(xiàn)文件解壓縮示例

2019-11-26 15:38:09
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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"; 

    /**
     * 數(shù)據(jù)壓縮
     * 
     * @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(); 
        } 
    } 

    /**
     * 數(shù)據(jù)壓縮
     * 
     * @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); 
    } 

    /**
     * 數(shù)據(jù)解壓縮
     * 
     * @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(); 
        } 
    } 

    /**
     * 數(shù)據(jù)解壓縮
     * 
     * @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); 
    } 
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 略阳县| 重庆市| 凤冈县| 宜阳县| 汉沽区| 武宁县| 晋州市| 凤阳县| 小金县| 会东县| 时尚| 漳浦县| 仙游县| 武宁县| 正蓝旗| 昌邑市| 宝山区| 凤城市| 丘北县| 普兰县| 泗水县| 法库县| 吕梁市| 镇雄县| 上虞市| 沧源| 望城县| 搜索| 东海县| 平昌县| 晋城| 房产| 娱乐| 甘孜县| 达孜县| 镇巴县| 方城县| 成都市| 安远县| 嘉荫县| 陇南市|