jarファイル内のリソースとして保持されるディレクトリ(サブディレクトリを含む)テンプレートがあります。実行時に、それ (テンプレート) を抽出して tmp dir にいくつかのコンテンツを変更し、最終的に圧縮されたアーティファクトとして公開する必要があります。
私の質問は、このコンテンツを簡単に抽出する方法ですか? getResource() と getResourceAsStream() を試していました。
次のコードはここで正常に動作します: (Java7)
String s = this.getClass().getResource("").getPath();
if (s.contains("jar!")) {
// we have a jar file
// format: file:/location...jar!...path-in-the-jar
// we only want to have location :)
int excl = s.lastIndexOf("!");
s = s.substring(0, excl);
s = s.substring("file:/".length());
Path workingDirPath = workingDir = Files.createTempDirectory("demo")
try (JarFile jf = new JarFile(s);){
Enumeration<JarEntry> entries = jf.entries();
while (entries.hasMoreElements()) {
JarEntry je = entries.nextElement();
String name = je.getName();
if (je.isDirectory()) {
// directory found
Path dir = workingDirPath.resolve(name);
Files.createDirectory(dir);
} else {
Path file = workingDirPath.resolve(name);
try (InputStream is = jf.getInputStream(je);) {
Files.copy(is, file, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
} else {
// debug mode: no jar
}