Skip to content

Instantly share code, notes, and snippets.

@aNNiMON
Created April 5, 2014 11:17
Show Gist options
  • Save aNNiMON/9990588 to your computer and use it in GitHub Desktop.
Save aNNiMON/9990588 to your computer and use it in GitHub Desktop.
Java 8 print text based resource file inside a jar file. (eg: META-INF/MANIFEST.MF)
package ztools;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Print text based resource file inside a jar file. (eg: META-INF/MANIFEST.MF)
* @author Zemian Deng
*/
public class PrintJar {
public static void main(String[] args) throws Exception {
// Search given name in a jar
JarFile jarFile = new JarFile(args[0]);
final String searchName = (args.length >= 2) ? args[1] : "META-INF/MANIFEST.MF";
Optional<JarEntry> searchResult = jarFile
.stream()
.filter(e -> e.getName().equals(searchName))
.findFirst();
if (!searchResult.isPresent())
throw new RuntimeException(searchName + " not found!");
// Print the JarEntry
JarEntry entry = searchResult.get();
try (InputStream instream = jarFile.getInputStream(entry)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
reader.lines().forEach(line -> System.out.println(line));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment