cstrap
7/24/2014 - 9:31 AM

Gzip file in java

Gzip file in java

public static void gzipFile(String source, String destination, boolean delete) {

    try {

        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(format("%s.gz", destination));
        GZIPOutputStream gzipOS = new GZIPOutputStream(fos);

        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            gzipOS.write(buffer, 0, len);
        }

        gzipOS.close();
        fos.close();
        fis.close();

        if (delete) {
            new File(source).delete();
        }
        
    } catch (IOException e) {
        err.println(e.getMessage());
    }

}