Java 6, also provide java.util.zip.ZipInputStream class which is used to read and decompress the Zip compressed files. The below example will show how to use the ZipInputStream class and decompress the zip files.
Example:-
package com.visualbuilder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ReadingZipExample {
public static void main(String[] args) {
try{
FileInputStream fi = new FileInputStream("C://test.zip");
CheckedInputStream csumi = new CheckedInputStream( fi, new Adler32());
ZipInputStream in2 = new ZipInputStream(new BufferedInputStream(csumi));
ZipEntry ze;
OutputStream output = null;
while ((ze = in2.getNextEntry()) != null) {
System.out.println("Reading file " + ze);
output = new FileOutputStream( ze.getName());
int x;
while ((x = in2.read()) != -1)
output.write(x);
}
output.close();
in2.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
|
Output:-
The above program will decompress the files and place all the files in the same directory which is used for the files and passed in the command line argument in the previous example.