Example usage for java.util.zip GZIPOutputStream close

List of usage examples for java.util.zip GZIPOutputStream close

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:Main.java

public static byte[] compressGZIP(byte bytes[]) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    GZIPOutputStream gzipos = new GZIPOutputStream(os);
    gzipos.write(bytes, 0, bytes.length);
    gzipos.close();
    return os.toByteArray();
}

From source file:Main.java

public static byte[] zip(byte[] input) {
    if (input == null || input.length == 0) {
        return input;
    }/*from  ww w.j  a  va2s  . c  om*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(input);
        gzip.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] compress(String str) throws Exception {
    if (str == null || str.length() == 0) {
        return null;
    }//from w  ww. j a  v a  2 s .  c  o  m

    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);

    gzip.write(str.getBytes());
    gzip.close();

    byte[] compressed = obj.toByteArray();
    obj.close();

    return compressed;
}

From source file:Main.java

public static byte[] doZip(byte[] data) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    java.util.zip.GZIPOutputStream gout = new GZIPOutputStream(out);
    gout.write(data);/*from  ww  w  .  j  av a 2  s  .c  o  m*/
    gout.close();
    return out.toByteArray();
}

From source file:Main.java

public static byte[] gzipCompress(byte[] input) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//w ww. j  a  va  2 s . c o m
        GZIPOutputStream os = new GZIPOutputStream(bos);
        os.write(input);
        os.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return bos.toByteArray();
}

From source file:Main.java

/**
 * GZip a given byte-array in-memory./*from  w  ww.  j a  va  2  s  . co  m*/
 * 
 * @param bytes
 *            the uncompressed bytes
 * 
 * @return the GZipped byte stream
 * 
 * @throws IOException
 *             if something fails
 */
public static byte[] gzipByteArray(byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);

    GZIPOutputStream gzip = new GZIPOutputStream(baos);
    gzip.write(bytes);
    gzip.close();

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] compress(String str) {
    if (str == null || str.length() == 0) {
        return str.getBytes();
    }//from w w  w.  jav a  2  s.  c  o m
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        final GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
    } catch (final IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

From source file:Main.java

public static final byte[] compress(byte[] val) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    gos.write(val, 0, val.length);
    gos.finish();//from w w w.j  a  v  a2s  .  co m
    gos.close();

    // store it and set compression flag
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] gzip(byte[] data) {
    byte[] b = null;
    try {/*from   w w w.  j a v  a 2 s . c om*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(data);
        gzip.finish();
        gzip.close();

        b = bos.toByteArray();
        bos.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:ByteUtils.java

public static byte[] packRaw(byte[] b) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    GZIPOutputStream zos = new GZIPOutputStream(baos);
    zos.write(b);// w  ww  .j ava2s  .com
    zos.close();

    return baos.toByteArray();
}