Example usage for java.util.zip GZIPOutputStream write

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

Introduction

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

Prototype

public synchronized void write(byte[] buf, int off, int len) throws IOException 

Source Link

Document

Writes array of bytes to the compressed output stream.

Usage

From source file:Main.java

public static final byte[] compress(byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }/*from  w  ww  .j a v  a 2  s.  c  om*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    try {
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(bytes, 0, bytes.length);
        gzip.finish();
        byte[] fewerBytes = bos.toByteArray();
        gzip.close();
        bos.close();
        gzip = null;
        bos = null;
        return fewerBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:ZipDemo.java

public static final byte[] compress(final String uncompressed) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream zos = new GZIPOutputStream(baos);

    byte[] uncompressedBytes = uncompressed.getBytes();

    zos.write(uncompressedBytes, 0, uncompressedBytes.length);
    zos.close();// w  w  w.  j a v  a 2s .  c  o  m

    return baos.toByteArray();
}

From source file:azkaban.utils.GZIPUtils.java

public static byte[] gzipBytes(byte[] bytes, int offset, int length) throws IOException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipStream = null;

    gzipStream = new GZIPOutputStream(byteOutputStream);

    gzipStream.write(bytes, offset, length);
    gzipStream.close();// w  w w  . ja v a  2 s  .  com
    return byteOutputStream.toByteArray();
}

From source file:com.google.api.server.spi.IoUtilTest.java

private static byte[] compress(byte[] bytes) {
    try {/*w  w  w.j  a v  a2  s  .c  om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(baos);
        gos.write(bytes, 0, bytes.length);
        gos.close();
        return baos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cenrise.test.azkaban.GZIPUtils.java

public static byte[] gzipBytes(final byte[] bytes, final int offset, final int length) throws IOException {
    final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipStream = null;

    gzipStream = new GZIPOutputStream(byteOutputStream);

    gzipStream.write(bytes, offset, length);
    gzipStream.close();/*from w w  w.j  a  va 2  s  .  c  om*/
    return byteOutputStream.toByteArray();
}

From source file:org.linguafranca.pwdb.kdbx.Helpers.java

public static byte[] zipBinaryContent(byte[] value) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // zip up the content
    try {//w w  w .  j  a  v  a  2 s  . c o m
        GZIPOutputStream g = new GZIPOutputStream(baos);
        g.write(value, 0, value.length);
        g.flush();
        g.close();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    return baos.toByteArray();
}

From source file:Compress.java

public static void gzipFile(String from, String to) throws IOException {
    FileInputStream in = new FileInputStream(from);
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1)
        out.write(buffer, 0, bytesRead);
    in.close();//from  w w  w .j a  va2  s .c o  m
    out.close();
}

From source file:v7db.files.Compression.java

/**
 * @return null, if the "gzipped" data is larger than the input (or there
 *         has been an exception)//from   w w w  .  ja  v  a2 s . co m
 */
public static byte[] gzip(byte[] data, int off, int len) {

    if (len < GZIP_STORAGE_OVERHEAD)
        return null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
        GZIPOutputStream gz = new GZIPOutputStream(baos);
        gz.write(data, off, len);
        gz.close();
        if (baos.size() >= len)
            return null;
        return baos.toByteArray();
    } catch (Exception e) {
        log.error("failed to gzip byte array", e);
        return null;
    }
}

From source file:com.dbmojo.Util.java

/** GZIP encode a String */
public static String gzipString(String inStr) throws Exception {
    byte[] strBytes = inStr.getBytes();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(bout);

    try {//from   ww w . java 2  s.com
        gout.write(strBytes, 0, strBytes.length);
    } finally {
        gout.close();
    }
    return bout.toString();
}

From source file:com.ery.ertc.estorm.util.GZIPUtils.java

public static final byte[] zip(byte[] in, int offset, int length) {
    try {// w w w  . j  a  v  a 2  s. c om
        // compress using GZIPOutputStream
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream(length / EXPECTED_COMPRESSION_RATIO);

        GZIPOutputStream outStream = new GZIPOutputStream(byteOut);

        try {
            outStream.write(in, offset, length);
        } catch (Exception e) {
            LOG.error("Failed to get outStream.write input", e);
        }

        try {
            outStream.close();
        } catch (IOException e) {
            LOG.error("Failed to implement outStream.close", e);
        }

        return byteOut.toByteArray();

    } catch (IOException e) {
        LOG.error("Failed with IOException", e);
        return null;
    }
}