集册 Java实例教程 gunzip InputStream

gunzip InputStream

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

549
gunzip InputStream


//package com.nowjava;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;/*来 自 时代Java公众号 - nowjava.com*/

import java.io.Closeable;

import java.io.IOException;

import java.io.InputStream;


import java.io.OutputStream;


import java.util.zip.GZIPInputStream;


public class Main {

    public static final int BUFFER = 4096;
    /**
    nowjava.com - 时代Java
    **/


    public static void gunzip(InputStream is, OutputStream os)

            throws IOException {


        GZIPInputStream gis = null;

        try {

            gis = new GZIPInputStream(is);

            int count = 0;

            byte data[] = new byte[BUFFER];

            while ((count = gis.read(data, 0, BUFFER)) != -1) {

                os.write(data, 0, count);

            }


        } finally {

            closeQuietly(gis);

        }


    }


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


        ByteArrayOutputStream baOut = null;

        ByteArrayInputStream baIn = null;

        GZIPInputStream gzIn = null;

        try {

            baOut = new ByteArrayOutputStream();

            baIn = new ByteArrayInputStream(bytes);

            gzIn = new GZIPInputStream(baIn);

            int count = 0;

            byte data[] = new byte[BUFFER];

            while ((count = gzIn.read(data, 0, BUFFER)) != -1) {

                baOut.write(data, 0, count);

            }


            return baOut.toByteArray();

        } finally {

            closeQuietly(gzIn);

            c
展开阅读全文