Example usage for java.util.zip GZIPOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:com.wbtech.dao.NetworkUitlity.java

/**
 * ?/*from  w ww .  j  a  v  a  2s  .  co  m*/
 * @param str
 * @return
 */
public static byte[] compressToByte(String str) {
    if (str == null || str.length() == 0) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
        gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes("utf-8"));
        gzip.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

From source file:GZIPUtils.java

/**
 * Returns an gzipped copy of the input array.
 *//*from   w  ww.  j a v  a  2 s.  c om*/
public static final byte[] zip(byte[] in) {
    try {
        // compress using GZIPOutputStream 
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

        GZIPOutputStream outStream = new GZIPOutputStream(byteOut);

        try {
            outStream.write(in);
        } catch (Exception e) {

        }

        try {
            outStream.close();
        } catch (IOException e) {

        }

        return byteOut.toByteArray();

    } catch (IOException e) {

        return null;
    }
}

From source file:org.apache.ode.daohib.bpel.hobj.GZipDataType.java

/**
 * Compress (using gzip algorithm) a byte array into an output stream.
 *///from  w  w  w  .ja  va2  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:io.digibyte.tools.util.BRCompressor.java

public static byte[] gZipCompress(byte[] data) {
    if (data == null)
        return null;
    byte[] compressedData = null;
    try {//from w  w w .  ja v a 2 s  . com
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(data.length);
        try {
            GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);
            try {
                zipStream.write(data);
            } finally {
                try {
                    zipStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            try {
                byteStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compressedData = byteStream.toByteArray();
    } catch (Exception e) {
        BRReportsManager.reportBug(e);
        e.printStackTrace();
    }
    return compressedData;
}

From source file:com.breadwallet.tools.util.BRCompressor.java

public static byte[] gZipCompress(byte[] data) {
    if (data == null)
        return null;
    byte[] compressedData = null;
    try {//from  w w  w . j  a v a  2  s. c o  m
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(data.length);
        try {
            GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);
            try {
                zipStream.write(data);
            } finally {
                try {
                    zipStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            try {
                byteStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compressedData = byteStream.toByteArray();
    } catch (Exception e) {
        FirebaseCrash.report(e);
        e.printStackTrace();
    }
    return compressedData;
}

From source file:Main.java

public static byte[] compress(byte[] src) throws IOException {

    GZIPOutputStream gzip = null;
    ByteArrayOutputStream baos = null;
    try {// w w  w .  jav  a2 s . c  om
        baos = new ByteArrayOutputStream();

        gzip = new GZIPOutputStream(baos);
        gzip.write(src);
        gzip.finish();

        return baos.toByteArray();

    } finally {
        if (gzip != null) {
            gzip.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:Main.java

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

    byte[] buffer = new byte[1024];

    try {//from w ww .  j  a va  2s.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:com.basistech.rosette.dm.json.array.CompareJsons.java

private static byte[] gzipCompress(byte[] data) {
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    try {/* w  ww  . ja  v  a  2s .  c o  m*/
        GZIPOutputStream compressedStream = new GZIPOutputStream(sink);
        ByteStreams.copy(new ByteArrayInputStream(data), compressedStream);
        compressedStream.close();
        return sink.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.dd4t.core.util.CompressionUtils.java

/**
 * Compresses a given content to a GZipped byte array.
 *
 * @param content the content to encode/*from  w ww  . j av a 2  s  .  c o  m*/
 * @return byte[] representing the compressed content bytes
 * @throws SerializationException if something goes wrong with the streams
 */
public static byte[] compressGZip(String content) throws SerializationException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gos = null;

    try {
        baos = new ByteArrayOutputStream();
        gos = new GZIPOutputStream(baos);

        gos.write(content.getBytes("UTF-8"));
        gos.close();

        return baos.toByteArray();
    } catch (IOException ioe) {
        LOG.error("String compression failed.", ioe);
        throw new SerializationException("Failed to compress String", ioe);
    } finally {
        IOUtils.closeQuietly(gos);
        IOUtils.closeQuietly(baos);
    }
}

From source file:Main.java

public static void doCompressFile(String inFileName) {
    try {//  ww  w  . java 2  s  .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);
    }
}