Un Zip Folder - Java File Path IO

Java examples for File Path IO:Zip File

Description

Un Zip Folder

Demo Code


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Main{
    /*  ww  w . j a  v a 2  s  .c  o m*/
    public static void UnZipFolder(String zipFileString,
            String outPathString) throws Exception {
        logger.d("UnZipFolder(String, String)");
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(
                zipFileString));
        ZipEntry zipEntry;
        String szName = "";
        while ((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();
            if (zipEntry.isDirectory()) {
                szName = szName.substring(0, szName.length() - 1);
                File folder = new File(outPathString + File.separator
                        + szName);
                folder.mkdirs();
            } else {
                File file = new File(outPathString + File.separator
                        + szName);
                if (file.getParentFile() != null
                        && !file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                logger.e(outPathString + File.separator + szName);
                file.createNewFile();
                FileOutputStream out = new FileOutputStream(file);
                int len;
                byte[] buffer = new byte[1024];
                while ((len = inZip.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }
        inZip.close();
    }
}

Related Tutorials