JarOutputStream put parent folder before my-wanted folder
- by adhitya kristanto
I tried to make jar with code from How to use JarOutputStream to create a JAR file? but that code always makes new parent folder of my input file/folder before it's inserted into .jar
Folder's Path that I want to be added into jar: C:/Trial/MyFolder
Folder that I want in MyJar.jar: MyFolder
But Folder that was inserted in MyJar.jar: Trial
Has it to be done that way? Thanks.
here is the code:
import EditorXML.GlobalStatus.GlobalStatus;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
/**
*
* @author Photosphere
*/
public class JarCreatingProgram {
public JarCreatingProgram() {
}
public void proceedNow() throws IOException{
File ed = new File("C:/Trial/EditingDini");
File meta = new File("C:/Trial/META-INF");
File net = new File("C:/Trial/net");
File org = new File("C:/Trial/org");
JarOutputStream target = new JarOutputStream(new FileOutputStream("D:/EditingDiniApp.jar"));
add(ed, target);
add(meta, target);
add(net, target);
add(org, target);
target.close();
}
private static void add(File source, JarOutputStream target) throws IOException
{
BufferedInputStream in = null;
try{
if (source.isDirectory())
{
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty())
{
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
System.out.println("ENTRY DALAM IF: "+entry.toString());
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
System.out.println("ENTRY: "+entry.toString());
byte[] buffer = new byte[1024];
while (true)
{
int count = in.read(buffer);
if (count == -1)
break;
target.write(buffer, 0, count);
}
target.closeEntry();
}
finally{
if (in != null)
in.close();
}
}
}