Compress data bytes by gzip algorithm - Java File Path IO

Java examples for File Path IO:GZIP

Description

Compress data bytes by gzip algorithm

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**//from  w  ww  . ja  v  a2  s .c om
     * Compress data bytes by gzip algorithm
     * 
     * @param data
     * @return compressed data
     * @throws IOException
     */
    public static byte[] gzip(byte[] data) throws IOException {
        if (data == null) {
            return data;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gos = null;
        try {
            gos = new GZIPOutputStream(out);
            gos.write(data);
        } finally {
            if (gos != null) {
                gos.close();
            }
        }

        return out.toByteArray();
    }
}

Related Tutorials