I have a function which gets object of some type as it parameter. I have to get all the resource files having extension .yml
from that location.
How to do it?
I have a function which gets object of some type as it parameter. I have to get all the resource files having extension .yml
from that location.
How to do it?
You could make use of this code
JarFile jarFile = new JarFile("my.jar");
for(Enumeration<JarEntry> em = jarFile.entries(); em.hasMoreElements();) {
String s= em.nextElement().toString();
if(s.startsWith(("path/to/resource/directory/"))){
ZipEntry entry = jarFile.getEntry(s);
String fileName = s.substring(s.lastIndexOf("/")+1, s.length());
if(fileName.endsWith(".yml")){
InputStream inStream= jarFile.getInputStream(entry);
OutputStream out = new FileOutputStream(fileName);
int c;
while ((c = inStream.read()) != -1){
out.write(c);
}
inStream.close();
out.close();
}
}
}
jarFile.close();
You can use FileFilter
to filter out .yaml
files. This post gives an example of how to implement and use a one.
I feel the phrasing of the question is a bit unclear, but if I understand it correctly, you want to extract all files with a .yml file extension from a given jar.
If this is the case, you could simply instantiate a java.util.JarFile from the known jar file path and use getEntries(). This method returns an enumeration of JarEntries which you could iterate through and verify that their names end with ".yml".