in initializer-generator/src/main/java/com/alibaba/initializer/core/template/loader/ClasspathTemplateLoader.java [157:206]
private void visitJarSystem(TempFileVisitor visitor, Resource resource) throws IOException {
URL url = resource.getURL();
URL jarUrl = ResourceUtils.extractJarFileURL(url);
JarFile jarFile = new JarFile(jarUrl.getFile());
Enumeration<JarEntry> entries = jarFile.entries();
String resourcePath = url.getFile();
// the scan root from jar file
String scanRoot = resourcePath.substring(resourcePath.indexOf(JAR_URL_SEPARATOR) + JAR_URL_SEPARATOR.length());
scanRoot = scanRoot.replace(JAR_URL_SEPARATOR, "/");
if (!scanRoot.endsWith("/")) {
scanRoot += "/";
}
Path scanRootPath = Paths.get(scanRoot);
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
// the full name of jar entry
String entryName = entry.getName();
if (!entryName.startsWith(scanRoot) || entry.isDirectory()) {
continue;
}
Path entryPath = Paths.get(entryName);
String fileName = entryPath.getFileName().toString();
Path relativePath = entryPath.subpath(scanRootPath.getNameCount(), entryPath.getNameCount());
Path relativeFolderPath = relativePath.getNameCount() == 1
? null
: relativePath.subpath(0,
relativePath.getNameCount() - 1);
visitor.visit(new CodeTemplate(relativeFolderPath, fileName) {
@Override
public Reader getReader() {
try {
return new InputStreamReader(resourceLoader.getResource("classpath:/" + entryPath.toString()).getInputStream());
} catch (IOException e) {
throw new BizRuntimeException(ErrorCodeEnum.SYSTEM_ERROR, "load resource error", e);
}
}
}
);
}
}