程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java比擬器comparator應用示例分享

java比擬器comparator應用示例分享

編輯:關於JAVA

java比擬器comparator應用示例分享。本站提示廣大學習愛好者:(java比擬器comparator應用示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java比擬器comparator應用示例分享正文



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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved