Example usage for java.util.zip GZIPOutputStream GZIPOutputStream

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

Introduction

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

Prototype

public GZIPOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size.

Usage

From source file:Main.java

/**
 * TODO check faster and more stable implementations.
 *
 * @param source_filepath/*w  w  w .j ava  2s. c om*/
 * @param destinaton_zip_filepath
 * @throws IOException
 */
public static void gzipFile(final String source_filepath, final String destinaton_zip_filepath)
        throws IOException {
    byte[] buffer = new byte[4096];
    try (FileOutputStream fileOutputStream = new FileOutputStream(destinaton_zip_filepath)) {
        try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream)) {
            try (FileInputStream fileInput = new FileInputStream(source_filepath)) {
                int bytes_read;
                while ((bytes_read = fileInput.read(buffer)) > 0) {
                    gzipOutputStream.write(buffer, 0, bytes_read);
                }
            }
            gzipOutputStream.finish();
        }
    } catch (IOException ex) {
        throw ex;
    }
}

From source file:Main.java

/**
 * Compresses a GZIP file./*from ww w.j a  v a2s.co  m*/
 * @param bytes The uncompressed bytes.
 * @return The compressed bytes.
 * @throws IOException if an I/O error occurs.
 */
public static byte[] gzip(byte[] bytes) throws IOException {
    /* create the streams */
    try (InputStream is = new ByteArrayInputStream(bytes)) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try (OutputStream os = new GZIPOutputStream(bout)) {
            /* copy data between the streams */
            byte[] buf = new byte[4096];
            int len;
            while ((len = is.read(buf, 0, buf.length)) != -1) {
                os.write(buf, 0, len);
            }
        }

        /* return the compressed bytes */
        return bout.toByteArray();
    }
}

From source file:Main.java

/**
 * Compresses a GZIP file./*w  w w .j a  va  2 s.c  om*/
 * 
 * @param bytes
 *            The uncompressed bytes.
 * @return The compressed bytes.
 * @throws IOException
 *             if an I/O error occurs.
 */
public static byte[] gzip(byte[] bytes) throws IOException {
    /* create the streams */
    InputStream is = new ByteArrayInputStream(bytes);
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        OutputStream os = new GZIPOutputStream(bout);
        try {
            /* copy data between the streams */
            byte[] buf = new byte[4096];
            int len = 0;
            while ((len = is.read(buf, 0, buf.length)) != -1) {
                os.write(buf, 0, len);
            }
        } finally {
            os.close();
        }

        /* return the compressed bytes */
        return bout.toByteArray();
    } finally {
        is.close();
    }
}

From source file:Main.java

static public byte[] gzip(byte src[], byte default_value[]) {
    try {/*from   w  ww. ja  v  a 2  s  .com*/
        if (src == null)
            return default_value;

        ByteArrayOutputStream out_raw = new ByteArrayOutputStream();
        GZIPOutputStream out = new GZIPOutputStream(out_raw);

        ByteArrayInputStream in = new ByteArrayInputStream(src);

        IOUtils.copy(in, out);

        in.close();
        out.close();

        return out_raw.toByteArray();
    } catch (Exception e) {
        return default_value;
    }
}

From source file:Main.java

public static void zip(InputStream is, OutputStream os) {
    GZIPOutputStream gzip = null;
    try {/*from w  w  w. ja  v a 2 s.co m*/
        gzip = new GZIPOutputStream(os);
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) != -1) {
            gzip.write(buf, 0, len);
            gzip.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(is, gzip);
    }
}

From source file:Main.java

public static void zip(InputStream is, OutputStream os) {
    GZIPOutputStream gzip = null;
    try {/*from w ww  . jav  a 2s  .com*/
        gzip = new GZIPOutputStream(os);
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) != -1) {
            gzip.write(buf, 0, len);
            gzip.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(is);
        closeIO(gzip);
    }
}

From source file:Main.java

public static byte[] compress(byte[] content) throws IOException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    byte[] byteMe = null;
    try {/*from w ww .ja  v a 2  s  .c o  m*/
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteOut);
        gzipOutputStream.write(content);
        gzipOutputStream.close();
        byteMe = byteOut.toByteArray();
    } finally {
        byteOut.close();
    }
    return byteMe;
}

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  w ww  . j a  va 2 s.  co m*/
    gout.close();
    return out.toByteArray();
}

From source file:Main.java

static byte[] compress(byte[] decompressed) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(bos);
    gzip.write(decompressed);/*from www  . j a v a2 s .  co m*/
    gzip.flush();
    gzip.close();
    bos.flush();
    bos.close();
    return bos.toByteArray();
}

From source file:Main.java

public static void writeGZipCompressedObject(Object o, OutputStream out) throws IOException {
    GZIPOutputStream gos = new GZIPOutputStream(out);
    ObjectOutputStream oos = new ObjectOutputStream(gos);
    oos.writeObject(o);/*  w w w . j  av a 2 s .  c  o m*/
    oos.flush();
    gos.finish();
}