Jun 26, 2010

Java Unzip Zip File



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
* Unzip a zip file to specific destination.
* @param source Zip File
* @param destination Unzipped file path
*/
public static void decompress(File source, File destination) throws IOException {
    ZipInputStream zis = new ZipInputStream(new FileInputStream(source));
    byte[] buffer = new byte[1024];
    for (ZipEntry zip; (zip = zis.getNextEntry()) != null;) {
        File file = new File(destination, zip.getName());
        if (zip.isDirectory()) {
            file.mkdir();
        } else {
            FileOutputStream fos = new FileOutputStream(file);
            for (int length; (length = zis.read(buffer)) > 0;) {
                fos.write(buffer, 0, length);
            }
            fos.close();
        }
        zis.closeEntry();
    }
    zis.close();
}

This method can be unzip a zip file
source which is a parameter to input zip file
destination which is parameter to unzipped file path


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Google Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Hostgator Coupon Code