dcsimg
How to create a JAR file (urgent!)
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Samar_Hossam
Posted On:   Sunday, October 24, 2004 03:58 AM

Dear everyone, I need to write a method that takes a source directory and jars its content. I wrote the following: public static void jar(String sourceDir,JarOutputStream out,String parent){ try { // get a list of files from the current directory File f = new File(sourceDir); String files[] = f.list(); // update the parent if (parent != ""){ parent = parent + File.separatorChar; } File tempFile; BufferedInputStream origin = null; byte data[] = new byte[2048]; for (int i=0; i System.out.println("Adding: " + files[i]); tempFile = new   More>>

Dear everyone,

I need to write a method that takes a source directory and jars its content. I wrote the following:



public static void jar(String sourceDir,JarOutputStream out,String parent){

try {

// get a list of files from the current directory

File f = new File(sourceDir);

String files[] = f.list();

// update the parent

if (parent != ""){

parent = parent + File.separatorChar;

}

File tempFile;

BufferedInputStream origin = null;

byte data[] = new byte[2048];

for (int i=0; i

System.out.println("Adding: " + files[i]);

tempFile = new File(sourceDir +File.separatorChar + files[i]);

if(tempFile.isFile()){

FileInputStream fi = new FileInputStream(sourceDir + File.separatorChar + files[i]);

origin = new BufferedInputStream(fi, 2048);

JarEntry entry = new JarEntry(parent + files[i]);

out.putNextEntry(entry);

int count;

while((count = origin.read(data, 0, 2048)) != -1) {

out.write(data, 0, count);

}

origin.close();

}

else{

JarEntry entry = new JarEntry(parent + files[i] + File.separatorChar);

out.putNextEntry(entry);

jar(sourceDir + File.separatorChar + files[i],out, parent + files[i]);

}

}

} catch(Exception e) {

e.printStackTrace();

}

}



The problem with the code is the following:


  1. Each subdirectory contains an extra empty file
  2. I don't know how to create the META-INF folder



I would appreciate your help sooooo much. Regards, Samar    <<Less

Re: How to create a JAR file (urgent!)

Posted By:   Anonymous  
Posted On:   Monday, October 25, 2004 05:55 AM

Maybe you can reuse Ant class.
About | Sitemap | Contact