Skip to content

Instantly share code, notes, and snippets.

@ratnikov
Created March 6, 2014 22:10
Show Gist options
  • Save ratnikov/9400830 to your computer and use it in GitHub Desktop.
Save ratnikov/9400830 to your computer and use it in GitHub Desktop.
javac Test.java && java -classpath . Test
enties for test.jar:
META-INF/
META-INF/MANIFEST.MF
Test.java
Done for test.jar
process jar tf test.jar: META-INF/
process jar tf test.jar: META-INF/MANIFEST.MF
process jar tf test.jar: Test.java
process zip -d test.jar Test.java: deleting: Test.java
process jar tf test.jar: META-INF/
process jar tf test.jar: META-INF/MANIFEST.MF
enties for test.jar:
META-INF/
META-INF/MANIFEST.MF
Test.java
Done for test.jar
import java.io.*;
import java.util.jar.*;
import java.util.*;
public class Test {
public static final void main(String[] args) throws Exception {
exec("jar cf test.jar Test.java");
print(new JarFile("test.jar"));
exec("jar tf test.jar");
exec("zip -d test.jar Test.java");
exec("jar tf test.jar");
print(new JarFile("test.jar"));
}
private static void exec(String cmd) throws Exception {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println("process " + cmd + ": " +line);
}
}
private static void print(JarFile jar) {
System.out.println("enties for " + jar.getName() + ":");
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
System.out.println(entries.nextElement().getName());
}
System.out.println("Done for " + jar.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment