Question:
How to decompress file from GZIP file using java?
Code:
import java.io.*; import java.util.zip.GZIPInputStream; public class GUnZipFile { public static void main( String[] args ) { String input = "/tmp/file.txt.gz"; String output = "/tmp/file.txt"; byte[] buffer = new byte[1024]; try { FileInputStream fis = new FileInputStream(input); GZIPInputStream gzis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(output); int len; while ((len = gzis.read(buffer)) > 0) { fos.write(buffer, 0, len); } gzis.close(); fos.close(); System.out.println("Done"); } catch(Exception ex) { System.out.println(ex); } } }
Output:
$ java GUnZipFile Done $ ls -l /tmp/file.txt.gz -rw-rw-r-- 1 dennis dennis 56 Feb 26 16:44 /tmp/file.txt.gz $ ls -l /tmp/file.txt -rw-rw-r-- 1 dennis dennis 4880 Feb 26 16:52 /tmp/file.txt