激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java使用gzip實現文件解壓縮示例

java使用gzip實現文件解壓縮示例

2019-11-12 14:30java教程網 JAVA教程

這篇文章主要介紹了java使用gzip實現文件解壓縮示例,需要的朋友可以參考下

代碼如下:

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);  
    }  
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色猫av| 国产91久久精品一区二区 | 欧美一级黄色录像片 | 《97色伦在色在线播放》 | 日本精品久久久一区二区三区 | 欧美性生交xxxxx免费观看 | 免看一级片 | 久久久久久久久久一本门道91 | 中文字幕一二区 | 亚洲成人国产 | 国产成人高清在线 | av在线观 | 国产精品成人亚洲一区二区 | 久久区二区 | 美女污污视频在线观看 | 久久成人免费观看 | 久久久久久久久久久一区 | 日韩激情一区二区三区 | 久热久操| 国产午夜探花 | 国产精品久久久久久久午夜片 | 91看片淫黄大片欧美看国产片 | 欧美国产综合视频 | 中国大陆一级毛片 | 国产精品午夜性视频 | 日韩剧情片 | av在线中文 | 黄色的视频在线观看 | xxxxxx免费| 蜜桃网站在线观看 | 欧美性色生活片免费播放 | 国产一区二区视频在线播放 | 日韩.www| 国产免费一区二区三区在线能观看 | 九九视屏 | 亚洲福利在线视频 | zzzzzzzxxxxxx日本人 | 婷婷久久网 | 国产毛毛片一区二区三区四区 | 成人国产精品齐天大性 | 日本aⅴ在线 |