As per the link : https://stackoverflow.com/a/1281295/1794012
I followed the instructions and created a jar file, the input directory where the source files for creating the jar is as follows,
- so (directory)
- so/some.txt (file)
when i am traversing through JarFile#entries method it is printing the following,
JarFile#entries output when jar created via JarOutputStream
META-INF/MANIFEST.MF
D:/so/
D:/so/some.txt
but i created the jar file using jar tool
Created jar using simple jar tool
jar -cvf so_commond.jar so so/some.txt
added manifest
adding: so/(in = 0) (out= 0)(stored 0%)
adding: so/some.txt(in = 7) (out= 9)(deflated -28%)
Now i use JarFile#entries to iterate the entries, the following is the output
JarFile#entries output when jar is created by jar tool
META-INF/ (this is not there when jar created by JarOutputStream)
META-INF/MANIFEST.MF
so/
so/some.txt
Could you please explain why the jar entry META-INF is shown only when jar is created by jar tool and does not shown when jar is created by JarOutputStream?
Code:
public static void main(String[] args){
run();
for(Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements();){
System.out.println(e.nextElement().getName());
}
}
public static void run() throws IOException
{
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream("D:\\so.jar"),
manifest);
add(new File("D:\\so"), 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);
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));
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();
}
}