S
Samar Hossam
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<files.length; i++) {
System.out.println("Adding: " + files);
tempFile = new File(sourceDir +File.separatorChar + files);
if(tempFile.isFile()){
FileInputStream fi = new FileInputStream(sourceDir +
File.separatorChar + files);
origin = new BufferedInputStream(fi, 2048);
JarEntry entry = new JarEntry(parent + files);
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 + File.separatorChar);
out.putNextEntry(entry);
jar(sourceDir + File.separatorChar + files,out, parent + files);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
The problem with the code is the following:
1. Each subdirectory contains an extra empty file
2. If you try to extract a jar file created by this method. The entry
of a subdirectory is considered as a file not a directory.
3. I don't know how to create the META-INF folder
I would appreciate your help sooooo much. Regards, Samar
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<files.length; i++) {
System.out.println("Adding: " + files);
tempFile = new File(sourceDir +File.separatorChar + files);
if(tempFile.isFile()){
FileInputStream fi = new FileInputStream(sourceDir +
File.separatorChar + files);
origin = new BufferedInputStream(fi, 2048);
JarEntry entry = new JarEntry(parent + files);
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 + File.separatorChar);
out.putNextEntry(entry);
jar(sourceDir + File.separatorChar + files,out, parent + files);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
The problem with the code is the following:
1. Each subdirectory contains an extra empty file
2. If you try to extract a jar file created by this method. The entry
of a subdirectory is considered as a file not a directory.
3. I don't know how to create the META-INF folder
I would appreciate your help sooooo much. Regards, Samar