集册 Java实例教程 gzip字节数组

gzip字节数组

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

479
gzip字节数组


//package com.nowjava;


import java.io.ByteArrayOutputStream;
/*
 from 时 代 J a v a 
*/

import java.io.Closeable;

import java.io.IOException;

import java.io.InputStream;


import java.io.OutputStream;


import java.util.zip.GZIPOutputStream;


public class Main {

    public static final int BUFFER = 4096;


    public static void gzip(InputStream is, OutputStream os)
    /* 
     来自 
    *时   代    Java - nowjava.com*/

            throws IOException {


        GZIPOutputStream gos = null;


        try {

            gos = new GZIPOutputStream(os);


            int count = 0;

            byte data[] = new byte[BUFFER];

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

                gos.write(data, 0, count);

            }


            gos.finish();

            gos.flush();

        } finally {

            closeQuietly(gos);

        }

    }


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

        ByteArrayOutputStream baOut = null;

        GZIPOutputStream gzOut = null;

        try {

            baOut = new ByteArrayOutputStream();

            gzOut = new GZIPOutputStream(baOut);

            gzOut.write(bytes);

            gzOut.finish();

            gzOut.flush();

            return baOut.toByteArray();

        } finally {

            closeQuietly(gzOut);

            closeQuietly(baOut);

        }
展开阅读全文