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

public static OutputStream buildOutputStream(OutputStream os, byte type) throws IOException {
    switch (type) {
    case GZIP:/*  w  w  w . j a  va  2  s  . c  om*/
        return new GZIPOutputStream(os);
    default:
        return os;
    }
}

From source file:Main.java

public static byte[] compressGzip(byte[] content) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    try {//from   ww  w  .j  a  v a  2  s .  co  m
        gos.write(content);
        gos.flush();
    } finally {
        gos.close();
    }

    return bos.toByteArray();
}

From source file:Main.java

static public final byte[] gzipBytes(byte[] bytes) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(out);
    try {// w  w  w .  j  ava2 s .  c  o m
        gout.write(bytes);
    } finally {
        gout.close();
    }
    return out.toByteArray();
}

From source file:Main.java

public static void gzipIt(String inputFile, String outputFile) {

    byte[] buffer = new byte[1024];

    try {//from w ww .j  a  v a2 s  . c o  m

        GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFile));

        FileInputStream in = new FileInputStream(inputFile);

        int len;
        while ((len = in.read(buffer)) > 0) {
            gzos.write(buffer, 0, len);
        }

        in.close();

        gzos.finish();
        gzos.close();

        System.out.println("Done");

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static byte[] compress(String str) throws Exception {
    if (str == null || str.length() == 0) {
        return null;
    }/*from w  ww  .jav 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: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  ww .java  2s .c o m
    out.close();
}

From source file:Main.java

public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return str;
    }/*from w  w  w  .  j a va 2 s  . c om*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    return out.toString("ISO-8859-1");
}

From source file:Main.java

public static byte[] compressStringToByteArray(String uncompressedString) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream;
    try {/*  w  ww . ja  v a  2  s.  co  m*/
        gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(uncompressedString.getBytes("UTF-8"));
        gzipOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

public static byte[] zip(byte[] input) {
    if (input == null || input.length == 0) {
        return input;
    }/*from  w w w  . java  2  s  .  com*/
    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) {
    if (str == null || str.length() == 0) {
        return str.getBytes();
    }//from   w  w w  .ja va 2s.  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();
}