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 void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream fin = new FileInputStream("a.dat");
    OutputStream fout = new FileOutputStream("a.dat.gz");
    GZIPOutputStream gzout = new GZIPOutputStream(fout);
    for (int c = fin.read(); c != -1; c = fin.read()) {
        gzout.write(c);
    }//ww  w  . j  av  a2  s .c o  m
    gzout.close();
}

From source file:MainClass.java

public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
        try {// w  w w  . j  a  v  a 2 s .c  o  m
            InputStream fin = new FileInputStream(args[i]);
            OutputStream fout = new FileOutputStream(args[i] + GZIP_SUFFIX);
            GZIPOutputStream gzout = new GZIPOutputStream(fout);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                gzout.write(c);
            }
            gzout.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

From source file:Main.java

public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return str;
    }//  w w w .  ja  v a 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[] compress(String str) throws Exception {
    if (str == null || str.length() == 0) {
        return null;
    }//from  ww  w  .  ja va 2s  .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[] zip(byte[] input) {
    if (input == null || input.length == 0) {
        return input;
    }//w ww .jav  a2s  . co  m
    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[] gzip(byte[] data) {
    byte[] b = null;
    try {/* w w w  . j  a va 2  s. com*/
        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:Main.java

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

From source file:Main.java

public static byte[] compress(String str) {
    if (str == null || str.length() == 0) {
        return str.getBytes();
    }/* w w  w . j  a  v a2s . 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

static public final byte[] gzipBytes(byte[] bytes) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(out);
    try {/* ww w.  j a v a 2  s.co m*/
        gout.write(bytes);
    } finally {
        gout.close();
    }
    return out.toByteArray();
}

From source file:Main.java

public static byte[] compressGzip(byte[] content) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    try {/*  w  w  w.ja  va 2s .c o  m*/
        gos.write(content);
        gos.flush();
    } finally {
        gos.close();
    }

    return bos.toByteArray();
}