集册 Java实例教程 用gzip压缩数据流

用gzip压缩数据流

欢马劈雪     最近更新时间:2020-01-02 10:19:05

504
用gzip压缩数据流
/**来 自 N o  w  J a v a . c o m - 时  代  Java**/


//package com.nowjava;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;
/** from 
n o w  j a v a  . c o m**/

import java.nio.charset.Charset;


import java.util.zip.GZIPOutputStream;


public class Main {

    private static final int BYTE_BUFFER = 1024;

    private static final String GZIP_FILE_SUFFIX = ".gz";


    /**

     * compress data stream

     * @author Crow

     * @date 2015?6?19?

     * @version v0.1

     * @param is   input stream of bytes

     * @param os   output stream of bytes

     * @throws IOException 

     */

    public static void gzip(InputStream is, OutputStream os)

            throws IOException {

        GZIPOutputStream gos = new GZIPOutputStream(os);


        byte[] data = new byte[BYTE_BUFFER];

        int length = -1;

        while ((length = is.read(data)) != -1) {

            gos.write(data, 0, length);

        }

        gos.finish();

        gos.flush();

        gos.close();

    }


    /**

     * compress bytes

     * @author Crow

     * @date 2015?6?19?

     * @version v0.1

     * @param content   the bytes to be compressed

     * @return

     * @throws IOException 

     */

    public static byte[] gzip(byte[] content) throws IOException {

        ByteArrayInputStream bis = new ByteArrayInputStream(content);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();


        gzip(bis, bos);


        bos.flush();

        byte[] result = bos.toByteArray();

        bos.close();

        bis.close();


        return result;

    }


    /**

     * compress string, default UTF-8 encoding

     * @author Crow

     * @date 2015?6?19?

     * @version v0.1

     * @param content   the string to be compressed

     * @return

     * @throws IOException

     */

    public static byte[] gzip(String content) throws IOException {

        return gzip(content, Charset.forName("UTF-8"));

    }


    /**

     * compress string, need to provide encoding

     * @author Crow

     * @date 2015?6?19?

     * @version v0.1

     * @param content   the string to be compressed

     * @param cs      encoding

     * @return

     * @throws IOException 

     */

    public static byte[] gzip(String content, Charset cs)

            throws IOException {

        ByteArrayInputStream bis = new ByteArrayInputStream(

                content.getBytes(cs));

        ByteArrayOutputStream bos = new ByteArrayOutputStream();


        gzip(bis, bos);


        bos.flush();

        byte[] result = bos.toByteArray();

        bos.close();

        bis.close();


        return result;

    }


    /**

     * compress file, after generates a .gz file in the same directory, the original file will be deleted 

     * @author Crow

     * @date 2015?6?19?

     * @version v0.1

     * @param sourceFile   the file to be compressed

     * @return

     */

    public static boolean gzip(File sourceFile) {

        return gzip(sourceFile, true);

    }


    /**

     * compress file, generates a .gz file in the same directory

     * @author Crow

     * @date 2015?6?19?

     * @version v0.1

     * @param sourceFile      the file to be compressed

     * @param deleteSourceFile   if true, the original file will be deleted 

     * @return

     */

    public static boolean gzip(File sourceFile, boolean deleteSourceFile) {

        File gzipFile = new File(sourceFile.getParent(),

                sourceFile.getName() + GZIP_FILE_SUFFIX);

        return gzip(sourceFile, gzipFile, deleteSourceFile);

    }


    public static boolean gzip(File sourceFile, File gzipFile,

            boolean deleteSourceFile) {

        BufferedInputStream bis = null;

        try {

            bis = new BufferedInputStream(new FileInputStream(sourceFile));

        } catch (FileNotFoundException e) {

            System.out.println("?????????????????????????");

            e.printStackTrace();

            return false;

        }


        BufferedOutputStream bos = null;

        try {

            bos = new BufferedOutputStream(new FileOutputStream(gzipFile));

        } catch (FileNotFoundException e) {

            e.printStackTrace();

            try {

                if (bis != null)

                    bis.close();

            } catch (IOException e1) {

                e1.printStackTrace();

            }

            return false;

        }


        GZIPOutputStream gos = null;

        try {

            gos = new 
展开阅读全文