GZip compress byte array - Java java.lang

Java examples for java.lang:byte Array Compress

Description

GZip compress byte array

Demo Code

/**/*from   ww  w.jav a 2s  .c  o m*/
 * QucikKV
 * Copyright 2014-2016 Sumi Makito
 * Licensed under Apache License 2.0.
 *
 * @author sumimakito<sumimakito@hotmail.com>
 */
//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.util.zip.GZIPOutputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(compress(bytes)));
    }

    public static byte[] compress(byte[] bytes) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(baos);
        gzip.write(bytes);
        gzip.close();
        return baos.toByteArray();
    }
}

Related Tutorials