集册 Java实例教程 GZIP解压缩数据。

GZIP解压缩数据。

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

525
GZIP解压缩数据。

/*****************************************************************************

 * Copyright (c) 2007 Jet Propulsion Laboratory,

 * California Institute of Technology.  All rights reserved

 *****************************************************************************//**来自 时代Java公众号 - nowjava.com**/

//package com.nowjava;

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.IOException;

import java.io.OutputStream;

import java.io.UnsupportedEncodingException;

import java.util.zip.GZIPInputStream;

/* 
 来自 
*时 代 J a v a 公 众 号 - nowjava.com*/

public class Main {

    private static final int _BUFFER_SIZE = 1024;


    /**

     * Decompresses data.

     * 

     * @param data Data to be decompressed.

     * @return String object in specified encoding.

     * @throws IOException If there is an IO related error.

     * @throws UnsupportedEncodingException If given encoding is not supported.

     */

    public static byte[] decompress(byte[] data) throws IOException,

            UnsupportedEncodingException {

        byte[] buffer = new byte[_BUFFER_SIZE];

        ByteArrayInputStream bais = null;

        GZIPInputStream gis = null;

        ByteArrayOutputStream baos = null;


        int byteRead = 0;

        try {

            bais = new ByteArrayInputStream(data);

            gis = new GZIPInputStream(bais);

            baos = new ByteArrayOutputStream();


            while ((byteRead = gis.read(buffer, 0, buffer.length)) != -1) {

                baos.write(buffer, 0, byteRead);

            }

        } catch (Exception exception) {

            throw new IOException("Failed to decompress data.");

        } finally {

            try {

                if (baos != null) {

                    baos.close();

                }


                if (gis != null) {

                    gis.close();

                }


                if (bais != null) {

                    bais.close();

                }

            } catch (Exception exception) {

            }

        }

        return baos.toByteArray();

    }


    public static void decompress(InputStream inputStream,

            OutputStream outputStream) throws IOException {

        byte[] buffer = new byte[_BUFFER_SIZE];

        GZIPInputStream gis = null;


        try {

            gis = new GZIPInputStream(inputStream);


            int bytesRead = 0;

            while ((bytesRead = gis.read(buffer, 0, buffer.length)) != -1) {

                outputStream.write(buffer, 0, bytesRead);

            }

        } catch (IOException exception) {

            exception.printStackTrace();

            throw exception;

        } finally {

            if (gis != null) {

                gis.close();

            }

        }

    }


    public static void decompress(
展开阅读全文