package zip;

import java.io.DataOutputStream;
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;

/**
 *
 * @author webkkk
 *
 */
public class Unzip {
 public static void main(String[] args) {
  String unzipfile = "c:\\test\\test.zip";
  String extFold = "c:\\test\\";
  int intCount = 0;
  try {
   // File oldDirec = new File(unzipfile);
   ZipInputStream zin = new ZipInputStream(new FileInputStream(unzipfile));
   ZipEntry entry;
   while ((entry = zin.getNextEntry()) != null) {
    if (entry.isDirectory()) {
     File directory = new File(extFold, entry.getName());
     if (!directory.exists() && directory.mkdirs())
      System.exit(0);
     zin.closeEntry();
    } else {
     String strFileName = entry.getName();
     if (strFileName.indexOf("/") != -1) {
      makeDirs(extFold, strFileName.substring(0, strFileName.lastIndexOf("/")));
     }
     File myFile = new File(strFileName);
     FileOutputStream fout = new FileOutputStream(extFold + myFile.getPath());
     DataOutputStream dout = new DataOutputStream(fout);
     byte[] b = new byte[1024];
     int len = 0;
     while ((len = zin.read(b)) != -1) {
      dout.write(b, 0, len);
     }
     dout.close();
     fout.close();
     zin.closeEntry();
    }
    intCount++;
   }
  } catch (IOException e) {
   e.printStackTrace();
   System.out.println(e);
  }
  System.out.println("Total: [" + intCount + "] files has been created!");
 }

 private static boolean makeDirs(String strBottomFoldName, String strFoldName) {
  boolean blnResult = true;
  File newFold = new File(strBottomFoldName, strFoldName);
  if (!newFold.exists() && !newFold.mkdirs()) {
   blnResult = false;
  }
  return blnResult;
 }
}