Unzip the .gz file in batch mode

I have 100 of the .gz files that I need to compress. I have a couple of questions.

a) I use the code provided at http://www.roseindia.net/java/beginners/JavaUncompress.shtml to unzip the .gz file. His work is wonderful. Quest: - there is a way to get the file name of the archived file. I know that the Zip Java class gives an enumeration of an entery file to work. This can give me the file name, size, etc. stored in the .zip file. But, we have the same for .gz files or the file name is the same as filename.gz with the .gz removed.

b) there is another elegant way to unzip a .gz file by calling the utility function in java code. Like calling a 7-zip application from your java class. Then I do not need to worry about the input / output stream.

Thanks in advance. Capil

+6
java gzip decompression
source share
6 answers

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(); } } } 
+9
source share

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

As for B, do you need gunzip specifically, or will there be another compression algorithm? There the java port of the LZMA compression algorithm used by 7zip to create .7z files, but it will not process .gz files.

+2
source share

If you have a fixed number of files to extract once, why not use existing tools to do this? As noted by Paul Mori, gunzip can do this: for i in *.gz; do gunzip $i; done for i in *.gz; do gunzip $i; done for i in *.gz; do gunzip $i; done And he will automatically call them by removing .gz$

In windows try winrar, perhaps, or gunzip from http://unxutils.sf.net

0
source share

GZip is usually used only for individual files, so it usually does not contain information about individual files. To combine several files into one compressed archive, they are first combined into an uncompressed Tar file (with information about the individual contents), and then compressed as a single file. This combination is called Tarball.

Libraries exist for extracting information about a single file from Tar, as in ZipEntries. One example. First you need to extract the .gz file to a temporary file in order to use it, or at least submit the GZipInputStream to the Tar library.

You can also call 7-Zip from the command line using Java. The 7-Zip command line syntax is here: 7-Zip command line syntax. An example of invoking a shell from Java: Running shell commands in Java. You will need to call 7-Zip twice: once to extract Tar from a .tar.gz or .tgz file and again to extract individual files from Tar.

Or you could just do the easy thing and write a short shell script or batch file for decompression. There is no reason to hammer a square pin in a round hole - this is what batch files are for. As a bonus, you can also combine their parameters, significantly reducing the complexity of java command line execution, while maintaining Java control execution.

0
source share

You tried

 gunzip *.gz 
0
source share

.gz files (gzipped) can store the file name of a compressed file. For example, FuBar.doc can be saved inside myDocument.gz and with appropriate compression, the file can be restored to the file name FuBar.doc. Unfortunately, java.util.zip.GZIPInputStream does not support any way to read the file name, even if it is stored in the archive.

0
source share

All Articles