Compresses the input byte array using GZIP. - Java File Path IO

Java examples for File Path IO:GZIP

Description

Compresses the input byte array using GZIP.

Demo Code


//package com.java2s;

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

import java.util.zip.GZIPOutputStream;
import sun.misc.BASE64Decoder;

public class Main {
    /**/*from   w ww .  j av a2  s .  com*/
     * Compresses the input byte array using GZIP.
     *
     * @param binaryInput Array of bytes that should be compressed.
     * @return Compressed bytes
     * @throws IOException
     */
    public static byte[] gzip(byte[] binaryInput) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzipOut = new GZIPOutputStream(baos);

        gzipOut.write(binaryInput);
        gzipOut.finish();
        gzipOut.close();

        return baos.toByteArray();
    }

    /**
     * Compresses the input BASE64 encoded byte array using GZIP.
     *
     * @param base64Input BASE64 encoded bytes that should be compressed
     * @return Compressed bytes
     * @exception IOException
     */
    public static byte[] gzip(String base64Input) throws IOException {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] binaryInput = base64Decoder.decodeBuffer(base64Input);

        return gzip(binaryInput);
    }
}

Related Tutorials