• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unjar a file

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic