Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decompress .gz file in batch

I have 100 of .gz files which I need to de-compress. I have couple of questions

a) I am using the code given at http://www.roseindia.net/java/beginners/JavaUncompress.shtml to decompress the .gz file. Its working fine. Quest:- is there a way to get the file name of the zipped file. I know that Zip class of Java gives of enumeration of entery file to work upon. This can give me the filename, size etc stored in .zip file. But, do we have the same for .gz files or does the file name is same as filename.gz with .gz removed.

b) is there another elegant way to decompress .gz file by calling the utility function in the java code. Like calling 7-zip application from your java class. Then, I don't have to worry about input/output stream.

Thanks in advance. Kapil

like image 624
Kapil D Avatar asked May 23 '09 06:05

Kapil D


2 Answers

a) Zip is an archive format, while gzip is not. So an entry iterator does not make much sense unless (for example) your gz-files are compressed tar files. What you want is probably:

File outFile = new File(infile.getParent(), infile.getName().replaceAll("\\.gz$", ""));

b) Do you only want to uncompress the files? If not you may be ok with using GZIPInputStream and read the files directly, i.e. without intermediate decompression.

But ok. Let's say you really only want to uncompress the files. If so, you could 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();    
        }
    }       
}
like image 128
fredarin Avatar answered Sep 24 '22 08:09

fredarin


Regarding A, the gunzip command creates an uncompressed file with the original name minus the .gz suffix. See the man page.

Regarding B, Do you need gunzip specifically, or will another compression algorithm do? There's a java port of the LZMA compression algorithm used by 7zip to create .7z files, but it will not handle .gz files.

like image 21
Paul Morie Avatar answered Sep 23 '22 08:09

Paul Morie