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 byte[] compress(byte[] src) throws IOException {

    GZIPOutputStream gzip = null;
    ByteArrayOutputStream baos = null;
    try {/*  w  w  w  .  ja v  a  2 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 byte[] compress(byte[] bytes) throws IOException {
    byte[] result = null;
    ByteArrayOutputStream out = null;
    GZIPOutputStream gzip = null;
    try {//from w w w . j  a  va  2  s .  c  o  m
        out = new ByteArrayOutputStream(bytes.length);
        gzip = new GZIPOutputStream(out);
        gzip.write(bytes);
        gzip.finish();
        close(gzip);
        result = out.toByteArray();
    } catch (IOException e) {
        throw e;
    } finally {
        close(gzip);
        close(out);
    }
    return result;
}

From source file:Main.java

public static void doCompressFile(String inFileName) {
    try {/*from w w  w.j  a  va2s  .  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:Main.java

public static byte[] compressInGzip(byte[] originalData, int offset, int length) throws Exception {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutStream = new GZIPOutputStream(bos);
    gzipOutStream.write(originalData, offset, length);
    gzipOutStream.finish();//from  w  ww.  j  av a  2 s . c o  m
    gzipOutStream.flush();
    gzipOutStream.close();
    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}

From source file:Util.java

public static byte[] compress(Object data) {
    if (data == null) {
        return null;
    }//from w  w  w  . jav  a2  s  . co  m
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(baos);
        ObjectOutputStream oos = new ObjectOutputStream(gout);
        oos.writeObject(data);
        oos.flush();
        gout.finish();
        return baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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  .jav a2s  .  c  o  m
    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();
            }
        }
    }
}

From source file:ZipDemo.java

public static final byte[] compress(final String uncompressed) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream zos = new GZIPOutputStream(baos);

    byte[] uncompressedBytes = uncompressed.getBytes();

    zos.write(uncompressedBytes, 0, uncompressedBytes.length);
    zos.close();//from w  w w  . j a  va  2  s  .c om

    return baos.toByteArray();
}

From source file:Main.java

public static void compressFile(String inputFilePath, boolean deleteSource) throws IOException {
    byte[] buffer = new byte[1024];
    FileOutputStream fos = null;/*w  w w.j a  v a 2 s  .  com*/
    GZIPOutputStream gzos = null;
    FileInputStream fis = null;

    try {
        fos = new FileOutputStream(inputFilePath + ".gz");
        gzos = new GZIPOutputStream(fos);

        fis = new FileInputStream(inputFilePath);
        //         String zipEntryName = inputFilePath.substring(inputFilePath.lastIndexOf(File.separator) + 1);         
        //         gzos.putNextEntry(new ZipEntry(zipEntryName));

        int length;
        while ((length = fis.read(buffer)) > 0) {
            gzos.write(buffer, 0, length);
        }
    } finally {
        gzos.close();
        fos.close();
        fis.close();
    }

    if (deleteSource) {
        new File(inputFilePath).delete();
    }
}

From source file:Main.java

static byte[] getCompressedData(byte[] data) {
    ByteArrayOutputStream ser = new ByteArrayOutputStream();
    GZIPOutputStream gs;// w  w  w .j ava  2s .c o  m
    try {
        gs = new GZIPOutputStream(ser);
        gs.write(data);
        gs.close();
    } catch (IOException e) {
        throw new RuntimeException("Cannot compress data", e);
    }

    byte[] compressed = ser.toByteArray();
    return compressed;
}

From source file:Main.java

/**
 * Comprime uma string utilizando o GZIP
 * /*  w  w w  .j a v a  2 s  . co m*/
 * @param str
 * @param encoding
 * @return
 */
public static String gzipCompressString(String str, String encoding) {
    try {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        Writer writer = new OutputStreamWriter(gzip, encoding);
        writer.write(str);
        writer.close();
        byte[] compressedBytes = out.toByteArray();
        return new String(Base64.encodeBase64(compressedBytes), encoding);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}