集册 Java实例教程 使用GZIP解压缩输入流。

使用GZIP解压缩输入流。

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

590
使用GZIP解压缩输入流。
/** from 
时代Java公众号 - N o w J a  v a . c o m**/


//package com.nowjava;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.zip.GZIPInputStream;


import sun.misc.BASE64Decoder;


public class Main {

    /**

     * Decompresses the input stream using GZIP.

     *

     * @param inputStream Stream those content should be decompressed.

     * @return Decompressed bytes

     * @throws IOException

     */

    public static byte[] gunzip(InputStream inputStream) throws IOException {

        byte[] buffer = new byte[1024];

        int count;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        GZIPInputStream gzipIn = new GZIPInputStream(inputStream);//nowjava - 时代Java 提供


        while ((count = gzipIn.read(buffer)) != -1) {

            baos.write(buffer, 0, count);

        }


        gzipIn.close();


        return baos.toByteArray();

    }


    /**

     * Decompresses the input byte array using GZIP.

     *

     * @param binaryInput Array of bytes that should be decompressed.

     * @return Decompressed bytes

     * @throws IOException

     */

    public static byte[] gunzip(byte[] binaryInput) throws IOException {

        ByteArrayInputStream bais = new ByteArrayInputStream(binaryInput);

        return gunzip(bais);

    }


    
展开阅读全文