2012-11-01 48 views
1

我有一個JAR文件,它包含在我的JNLP文件的資源部分。這包含大量的子文件夾中的圖像。從資源枚舉某些類型的所有文件

用戶應該能夠替代該JAR文件後面添加/刪除圖像。

有沒有辦法來枚舉從與資源的所有文件,可以說.GIF爲文件擴展名,甚至更好地指定一個像「圖像/ *。GIF」(所以我只是得到它具有圖像「圖像」模式作爲他們的父目錄)。

另一種選擇是寫一個反映該文件結構的文本文件。通過這種方式,您可以逐行掃描它,逐個掃描圖像,但這意味着您必須更新兩個位置,這不是非常方便用戶。

回答

2

是的,你只要抓住罐子,並移動到的目錄,然後枚舉裏面的文件:

public String[] getFiles() throws IOException { 
    ArrayList<String> list = new ArrayList<String>(); 
    List<JarEntry> ents = new ArrayList<JarEntry>(); 
    Enumeration<JarEntry> e = null; 

    URL jarp = getLocation(); 
    if (jarp != null) { 
    jar = jarp.getProtocol().equalsIgnoreCase("jar") ? jarp : new URL("jar:" +                                              jarp.toString() + "!/"); 
    JarFile jarf = null; 
    try { 
     jarf = AccessController.doPrivileged(
       new PrivilegedExceptionAction<JarFile>() { 

        @Override 
        public JarFile run() throws Exception { 
         JarURLConnection conn = (JarURLConnection) jar.openConnection(); 
         conn.setUseCaches(false); 
         return conn.getJarFile(); 
        } 
       }); 
    } catch (PrivilegedActionException ex) { 
     Logger.getLogger(LicenseLoader.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    e = jarf.entries(); 
    while (e.hasMoreElements()) { 
     JarEntry je = e.nextElement(); 
     if (!je.isDirectory()) { 
      ents.add(je); 
     } 
    } 
    for (JarEntry ent : ents) { 
     if ((ent.getName().startsWith(pathName)) && (ent.getName().endsWith(".gif"))) { 
      String name = ent.getName().replace(pathName, ""); 
      list.add(name); 
     } 
    } 
} 
return list.toArray(new String[list.size()]); 
}