集册 Java实例教程 做一个gzip操作。

做一个gzip操作。

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

504
执行g-unzip操作。


//package com.nowjava;

import java.io.ByteArrayInputStream;/* from N  o w  J a v a . c o m*/

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.util.zip.GZIPInputStream;


public class Main {

    /**

     * Do a g-unzip operation.

     */

    public static byte[] gunzip(byte[] data) {

        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(10240);

        GZIPInputStream input = null;

        try {

            input = new GZIPInputStream(new ByteArrayInputStream(data));

            byte[] buffer = new byte[1024];

            int n = 0;

            for (;;) {

                n = input.read(buffer);

                if (n <= 0)

                    break;

                byteOutput.write(buffer, 0, n);

            }//N o w  J a v a  . c o m 提 供

        } catch (IOException e) {

            throw new RuntimeException("G-Unzip failed.", e);

        } finally {

            
展开阅读全文