2

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mohamed Jameel
  • 602
  • 5
  • 21

3 Answers3

2

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();
Rakesh
  • 4,264
  • 4
  • 32
  • 58
0

You can use FileFilter to filter out .yaml files. This post gives an example of how to implement and use a one.

Jugal Shah
  • 3,621
  • 1
  • 24
  • 35
0

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".

Alderath
  • 3,761
  • 1
  • 25
  • 43