欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年03月03日 16:54:59   作者:  
這篇文章主要介紹了java使用gzip實(shí)現(xiàn)文件解壓縮示例,需要的朋友可以參考下

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

相關(guān)文章

  • Java 如何判斷Integer類型的值是否相等

    Java 如何判斷Integer類型的值是否相等

    這篇文章主要介紹了Java 如何判斷Integer類型的值是否相等操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java使用Unsafe類的示例詳解

    Java使用Unsafe類的示例詳解

    java不能直接訪問操作系統(tǒng)底層,而是通過本地方法來訪問。Unsafe類提供了硬件級(jí)別的原子操作,這篇文章主要介紹了Java使用Unsafe類,需要的朋友可以參考下
    2021-09-09
  • idea創(chuàng)建spring boot項(xiàng)目及java版本只能選擇17和21的問題

    idea創(chuàng)建spring boot項(xiàng)目及java版本只能選擇17和21的問題

    這篇文章主要介紹了idea創(chuàng)建spring boot項(xiàng)目及java版本只能選擇17和21的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • java.sql.SQLTimeoutException異常的正確解決方法(親測(cè)有效!)

    java.sql.SQLTimeoutException異常的正確解決方法(親測(cè)有效!)

    在我們編寫程序的時(shí)候,有時(shí)候要進(jìn)行復(fù)雜的查詢時(shí),就會(huì)出現(xiàn)執(zhí)行sql時(shí)間過長,引起頁面執(zhí)行不了并提示執(zhí)行腳本超時(shí),這就是我們遇到超時(shí)異常,這篇文章主要給大家介紹了關(guān)于java.sql.SQLTimeoutException異常的正確解決方法,需要的朋友可以參考下
    2024-02-02
  • Java 圖表類庫詳解

    Java 圖表類庫詳解

    本文主要介紹了Java圖表類庫的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • JDK更換IDEA如何修改圖文詳解

    JDK更換IDEA如何修改圖文詳解

    這篇文章主要給大家介紹了關(guān)于JDK更換IDEA如何修改的相關(guān)資料,Java的不同版本的JDK之間存在細(xì)微的差別,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Java實(shí)現(xiàn)LRU緩存的實(shí)例詳解

    Java實(shí)現(xiàn)LRU緩存的實(shí)例詳解

    這篇文章主要介紹了Java實(shí)現(xiàn)LRU緩存的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • java制作帶界面的聊天工具

    java制作帶界面的聊天工具

    這篇文章主要教大家如何利用java制作帶界面的聊天工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Java設(shè)計(jì)模式中的簡(jiǎn)單工廠模式解析

    Java設(shè)計(jì)模式中的簡(jiǎn)單工廠模式解析

    這篇文章主要介紹了Java設(shè)計(jì)模式中的簡(jiǎn)單工廠模式解析,簡(jiǎn)單工廠模式提供一個(gè)創(chuàng)建對(duì)象實(shí)例的功能,而無須關(guān)心其具體實(shí)現(xiàn),被創(chuàng)建實(shí)例的類型可以是接口、抽象類,也可以是具體的類,需要的朋友可以參考下
    2023-11-11
  • Java開源工具iText生成PDF簡(jiǎn)單實(shí)例

    Java開源工具iText生成PDF簡(jiǎn)單實(shí)例

    這篇文章主要介紹了Java開源工具iText生成PDF簡(jiǎn)單實(shí)例,本文給出了3段代碼實(shí)例,講解創(chuàng)建一個(gè)簡(jiǎn)單PDF文件,在PDF中添加表格以及在PDF中添加圖片,需要的朋友可以參考下
    2015-07-07

最新評(píng)論