gzip对象

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

530
gzip对象

/*
 from 时 代 J a v a 公 众 号 
*/

//package com.nowjava;


import java.io.ByteArrayOutputStream;

import java.io.Closeable;

import java.io.IOException;


import java.io.ObjectOutputStream;


import java.io.Serializable;


import java.util.zip.GZIPOutputStream;


public class Main {

    public static byte[] gzipObject(Serializable data) throws IOException {

        ByteArrayOutputStream baOut = null;

        GZIPOutputStream gzOut = null;

        ObjectOutputStream objOut = null;

        try {
        /**
        时代Java公众号 提供 
        **/

            baOut = new ByteArrayOutputStream();

            gzOut = new GZIPOutputStream(baOut);

            objOut = new ObjectOutputStream(gzOut);

            objOut.writeObject(data);

            gzOut.finish();

            gzOut.flush();

            return baOut.toByteArray();

        } finally {

            closeQuietly(objOut);

            closeQuietly(gzOut);

            closeQuietly(baOut);

        }


    }


    public static void closeQuietly(
展开阅读全文