This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes unjar a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "unjar a file" Watch "unjar a file" New topic
Author

unjar a file

Syed Mahdi
Ranch Hand

Joined: Aug 27, 2004
Posts: 45
Hi i am tryign to unjar a file the input to the contructors will be the jar file name and the path (directory ) in which the jar file has to be extracted .. iu am trying this but doesnt seems to work can anybody help me i trying to call this class from other class
thanks




/**
* <p>Title: REMOVE THIS LINE</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003-2004</p>
* <p>Company: Advantage Business Computer Systems, Inc.</p>
* @author not attributable
* @version 1.0
*/
import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import com.abcsinc.database.dataset.*;
import com.abcsinc.database.schema.*;
import com.abcsinc.util.*;

public class UnJar {
public UnJar() {

}
static final int BUFFER = 2048;
public UnJar(String jarFile, String destDir) {

try {

BufferedOutputStream dest = null;
BufferedInputStream is = null;
JarEntry entry;
System.out.println("Enetered UnJar.java :the jarfile " + jarFile );
JarFile jarfile = new JarFile(jarFile);
Enumeration e = jarfile.entries();
while(e.hasMoreElements()) { //going through each entries one by one
System.out.println("UnJar.java :the path of dest dir " + destDir );
entry = (JarEntry) e.nextElement();
System.out.println("Extracting: " + entry);
is = new BufferedInputStream
(jarfile.getInputStream(entry));
int count;

File destdir = new File(destDir);
destdir.mkdir();



byte data[] = new byte[BUFFER];
System.out.println("entry.getname "+entry.getName());
//FileOutputStream fos = new FileOutputStream(entry.getName());
File destfilename = new File(destdir,entry.getName());
String destfilepath = destfilename.getAbsolutePath();
System.out.println("the name of the destination file name " + destfilepath);
FileOutputStream fos = new FileOutputStream(destfilename);
dest = new
BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER))!= -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
} catch(Exception e) {
e.printStackTrace();
}


}





// }//main ends
}//class ends
BB AA
Greenhorn

Joined: Sep 29, 2004
Posts: 1
public static void unjar(String jarFile, String destDir) {
final int BUFFER = 2048;
try {
BufferedOutputStream dest;
BufferedInputStream is;
byte data[] = new byte[BUFFER];

JarFile jarfile = new JarFile(jarFile);
Enumeration e = jarfile.entries();

while(e.hasMoreElements()) { //going through each entries one by one
JarEntry entry = (JarEntry) e.nextElement();
is = new BufferedInputStream(jarfile.getInputStream(entry));

String path = entry.getName();
int start = path.lastIndexOf("/");
String basis = null;
if(start != -1) {
basis = path.substring(0, start);
}
String filename = path.substring(start + 1);
File tmpPath = new File(destDir);
if(basis != null && basis.length() != 0){
tmpPath = new File(tmpPath, basis);
tmpPath.mkdirs();
}

if(filename != null && filename.length() != 0){
File destfile = new File(tmpPath, filename);
FileOutputStream fos = new FileOutputStream(destfile);
dest = newBufferedOutputStream(fos, BUFFER);

int count;
while ((count = is.read(data, 0, BUFFER))!= -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unjar a file
 
Similar Threads
unzip problem, I give up now
Problem in unzipping a folder using java.util.zip API
extracting all the contents of a jar file
Problem unzipping with ZipInputStream, containing filename with Swedish characters
Problem with Unzipping