a) Zip is an archive format, but gzip is not. Thus, an input iterator does not make much sense unless (for example) your gz files are compressed by tar files. You probably want:
File outFile = new File(infile.getParent(), infile.getName().replaceAll("\\.gz$", ""));
b) Do you only want to unzip the files? If not, you might find it helpful to use GZIPInputStream and read files directly, that is, without intermediate decompression.
But good. Say you really only want to unzip the files. If so, you can probably use this:
public static File unGzip(File infile, boolean deleteGzipfileOnSuccess) throws IOException { GZIPInputStream gin = new GZIPInputStream(new FileInputStream(infile)); FileOutputStream fos = null; try { File outFile = new File(infile.getParent(), infile.getName().replaceAll("\\.gz$", "")); fos = new FileOutputStream(outFile); byte[] buf = new byte[100000]; int len; while ((len = gin.read(buf)) > 0) { fos.write(buf, 0, len); } fos.close(); if (deleteGzipfileOnSuccess) { infile.delete(); } return outFile; } finally { if (gin != null) { gin.close(); } if (fos != null) { fos.close(); } } }
fredarin
source share