gzip Object - Java File Path IO

Java examples for File Path IO:GZIP

Description

gzip Object

Demo Code


//package com.java2s;

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 {// w  w  w.  j ava 2  s .c o  m
            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(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related Tutorials