Just provide a zip file which to be unziped and run the code.
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Unziping {
//Function to unzip a file, provide source zip file and destination directory where to unzip a file
public void unzip(File zipFile,String destFolder) {
System.out.println("Unzip :: "+zipFile.getPath()+" into "+destFolder);
try {
ZipFile zip = new ZipFile(zipFile);
File dest = new File(destFolder);
Enumeration files = zip.entries();
File file;
FileOutputStream fos;
int bytes = 0;
byte[] buffer = new byte[2156];
while (files.hasMoreElements()) {
ZipEntry entry = (ZipEntry)files.nextElement();
InputStream eis = zip.getInputStream(entry);
file = new File(dest.getAbsolutePath() + File.separator + entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
continue;
}
else {
file.getParentFile().mkdirs();
file.createNewFile();
}
fos = new FileOutputStream(file);
while ((bytes = eis.read(buffer)) != -1) {
fos.write(buffer, 0, bytes);
}
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Unziping obj = new Unziping();
obj.unzip(new File("C:\\MY\\Code\\com.zip"),"C:\\MY\\Code");
}
}