Extract the Contents of ZIP/JAR Files Programmatically Suppose jarFile is the jar/zip file to be extracted. destDir is the path where it will be extracted:
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enum = jar.entries();
while (enum.hasMoreElements()) {
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enum.nextElement();
java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
if (file.isDirectory()) { // if its a directory, create it
f.mkdir();
continue;
}
java.io.InputStream is = jar.getInputStream(file); // get the input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
while (is.available() > 0) { // write contents of 'is' to 'fos'
fos.write(is.read());
}
fos.close();
is.close();
}
source: [http://www.devx.com/tips/Tip/22124]
import java.io.*; import java.util.*; import java.util.jar.*; import java.util.zip.ZipException; public class jara { public static void main (String args[])throws IOException,ZipException { JarFile jarFile = new JarFile("dirp.jar"); Enumeration en = jarFile.entries(); while (en.hasMoreElements()) { String ent=proc(en.nextElement()); if(ent.indexOf("/")>0) { String fil=ent.substring(0,ent.indexOf("/")); System.out.println(fil); File local=new File(fil); if(!local.exists()) local.mkdirs(); } if(ent.indexOf(".")>0) { int n=ent.length(); String fil1=ent.substring(ent.lastIndexOf("/")+1,n); System.out.println(fil1); extract(jarFile.getName(),ent); } } }
public static String proc(Object obj) { JarEntry entry = (JarEntry)obj; String name = entry.getName(); System.out.println("\nEntry Name: "+name); return(name); } public static void extract(String jarName,String entryName)throws IOException,ZipException { JarFile jar = new JarFile(jarName); System.out.println(jarName + " opened."); try { // Get the entry and its input stream. JarEntry entry = jar.getJarEntry(entryName); // If the entry is not null, extract it. Otherwise, print a // message. if (entry != null) { // Get an input stream for the entry. InputStream entryStream = jar.getInputStream(entry); try { // Create the output file (clobbering the file if it exists). FileOutputStream file = new FileOutputStream(entry.getName()); try { // Allocate a buffer for reading the entry data. byte[] buffer = new byte[1024]; int bytesRead; // Read the entry data and write it to the output file. while ((bytesRead = entryStream.read(buffer)) != -1) { file.write(buffer, 0, bytesRead); } System.out.println(entry.getName() + " extracted."); } finally { file.close(); } } finally { entryStream.close(); } } else { System.out.println(entryName + " not found."); } // end if } finally { jar.close(); System.out.println(jarName + " closed."); } } }