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 void zip(InputStream is, OutputStream os) {
    GZIPOutputStream gzip = null;
    try {//from   w w  w  . java2s.c o 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 w w.j a v a  2  s .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 void gzipIt(String inputFile, String outputFile) {

    byte[] buffer = new byte[1024];

    try {//from   w ww. ja v a 2 s.  co  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:org.apache.ode.daohib.bpel.hobj.GZipDataType.java

/**
 * Compress (using gzip algorithm) a byte array into an output stream.
 *//*from   www.j  a  v a  2 s. c om*/
public static void gzip(byte[] content, OutputStream out) {
    try {
        GZIPOutputStream zip = new GZIPOutputStream(out);
        zip.write(content, 0, content.length);
        zip.finish();
        zip.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:edu.harvard.i2b2.fhir.Utils.java

public static byte[] compress(final String data, final String encoding) throws IOException {
    if (data == null || data.length() == 0) {
        return null;
    } else {//from  w w  w. j a v  a2 s .c o  m
        byte[] bytes = data.getBytes(encoding);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream os = new GZIPOutputStream(baos);
        os.write(bytes, 0, bytes.length);
        os.close();
        byte[] result = baos.toByteArray();
        return result;
    }
}

From source file:co.cask.hydrator.transforms.Compressor.java

public static byte[] compressGZIP(byte[] input) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(input, 0, input.length);
    gzip.close();/*ww w  .  j a  v  a2 s .  c o  m*/
    return out.toByteArray();
}

From source file:Main.java

public static void doCompressFile(String inFileName) {
    try {/*w  ww . ja  va  2s  . c  o  m*/
        File file = new File(inFileName);
        FileOutputStream fos = new FileOutputStream(file + ".gz");
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream in = new BufferedInputStream(fin);
        byte[] buffer = new byte[1024];
        int i;
        while ((i = in.read(buffer)) >= 0) {
            gzos.write(buffer, 0, i);
        }
        in.close();
        gzos.close();
    } catch (IOException e) {
        System.out.println("Exception is" + e);
    }
}

From source file:Compress.java

/** Gzip the contents of the from file and save in the to file. */
public static void gzipFile(String from, String to) throws IOException {
    // Create stream to read from the from file
    FileInputStream in = new FileInputStream(from);
    // Create stream to compress data and write it to the to file.
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    // Copy bytes from one stream to the other
    byte[] buffer = new byte[4096];
    int bytes_read;
    while ((bytes_read = in.read(buffer)) != -1)
        out.write(buffer, 0, bytes_read);
    // And close the streams
    in.close();//from w  w  w  .ja  v a2s.  com
    out.close();
}

From source file:com.opengamma.web.analytics.json.Compressor.java

static void compressStream(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    InputStream iStream = new BufferedInputStream(inputStream);
    GZIPOutputStream oStream = new GZIPOutputStream(
            new Base64OutputStream(new BufferedOutputStream(outputStream), true, -1, null), 2048);
    byte[] buffer = new byte[2048];
    int bytesRead;
    while ((bytesRead = iStream.read(buffer)) != -1) {
        oStream.write(buffer, 0, bytesRead);
    }/*from w  ww  .  java  2s  .c o  m*/
    oStream.close(); // this is necessary for the gzip and base64 streams
}

From source file:Main.java

public static void toTargz(String srcFile, String targetFile) throws IOException {
    File sourceFile = new File(srcFile);
    File target = new File(targetFile);
    FileInputStream in = null;//from   w  ww.  java  2s . c  om
    GZIPOutputStream gout = null;
    try {
        in = new FileInputStream(sourceFile);
        gout = new GZIPOutputStream(new FileOutputStream(target));
        byte[] array = new byte[BUFFER_LEN];
        int number = -1;
        while ((number = in.read(array, 0, array.length)) != -1) {
            gout.write(array, 0, number);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (gout != null) {
            try {
                gout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}