Tuesday, September 17, 2013

Extract zip

Sometimes, you might want to create a simple program in Java to extract zip files. If this is your case, this post can help you.  Java provides ZipFile class that can be used to read all entries in the zip file.  The ZipFile class allows you to create a zip file object by passing the path of the zip file to its constructor.  When you have the zip file object you are able to query all entries of the zip file. Each entry of the zip file is represented by a ZipEntry object. To query all entries of the zip file, you can use the entries method of the zip file object. The entries method returns an enumeration that contains all the entries.
Before extracting the content of an entry, we need to check whether the entry is a directory or file. This can be done by using the isDirectory method of the the entry object. If it is a file, then its content can read. To read the content of the file entry, you need to use the getInputStream method of the zip file object. This method return an InputStream object that has the read method to read the content of the file entry.
So far, i have talked about getting the content of the file entry. What are about directories in the zip file? Do we ignore them? The ZipFile treats a directory as one entry and also every file in the directory as one entry. When you get a file entry, its parent directory comes with it. And then you can create the parent directory that does not exist for storing the files. See the example code below.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ZipExtractor {
public static void main(String[] ars){
selectZip();
}

//allow zip file selection
public static void selectZip(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("ZIP","zip");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(false);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
    File file=chooser.getSelectedFile();
    String path=file.toString();
    unZip(path,file.getParent());
           
            }

     
}

public static void unZip(String zippath,String storeDir){
try {
ZipFile zf=new ZipFile(zippath); //create  a zip file object
if(zf.size()>0){ //read through the zip file
Enumeration<ZipEntry> entries=(Enumeration<ZipEntry>) zf.entries();
while(entries.hasMoreElements()){
ZipEntry entry=entries.nextElement();
if(!entry.isDirectory() && !entry.getName().endsWith(File.separator)){
//start extracting the files
extractFiles(zf.getInputStream(entry),entry.getName(),storeDir);

}


}

}
zf.close();
} catch (IOException e) {

e.printStackTrace();
}
}

public static void extractFiles(InputStream is, String fname, String storeDir){

FileOutputStream fos;
File fi=new File(storeDir+File.separator+fname); //output file
File fparent=new File(fi.getParent()+File.separator);
if(!fparent.exists()) fparent.mkdirs();//create parent directories for output files

try {

fos=new FileOutputStream(fi);
int content=0;
while((content=is.read())!=-1){
fos.write(content);
}
is.close();
fos.close();
} catch (Exception e) {

e.printStackTrace();
}

}
}

No comments:

Post a Comment