1 22 package org.jboss.util.file; 23 24 import java.util.Iterator ; 25 import java.util.Enumeration ; 26 import java.util.jar.JarFile ; 27 import java.util.jar.JarEntry ; 28 import java.util.zip.ZipFile ; 29 import java.util.zip.ZipEntry ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.net.URL ; 33 import java.net.JarURLConnection ; 34 35 42 public class JarArchiveBrowser implements Iterator 43 { 44 JarFile zip; 45 Enumeration entries; 46 JarEntry next; 47 ArchiveBrowser.Filter filter; 48 49 public JarArchiveBrowser(JarURLConnection url, ArchiveBrowser.Filter filter) 50 { 51 this.filter = filter; 52 try 53 { 54 zip = url.getJarFile(); 55 entries = zip.entries(); 56 } 57 catch (IOException e) 58 { 59 throw new RuntimeException (e); } 61 setNext(); 62 } 63 64 public JarArchiveBrowser(File f, ArchiveBrowser.Filter filter) 65 { 66 this.filter = filter; 67 try 68 { 69 zip = new JarFile (f); 70 entries = zip.entries(); 71 } 72 catch (IOException e) 73 { 74 throw new RuntimeException (e); } 76 setNext(); 77 } 78 79 public boolean hasNext() 80 { 81 return next != null; 82 } 83 84 private void setNext() 85 { 86 next = null; 87 while (entries.hasMoreElements() && next == null) 88 { 89 do 90 { 91 next = (JarEntry )entries.nextElement(); 92 } while (entries.hasMoreElements() && next.isDirectory()); 93 if (next.isDirectory()) next = null; 94 95 if (next != null && !filter.accept(next.getName())) 96 { 97 next = null; 98 } 99 } 100 } 101 102 public Object next() 103 { 104 ZipEntry entry = next; 105 setNext(); 106 107 try 108 { 109 return zip.getInputStream(entry); 110 } 111 catch (IOException e) 112 { 113 throw new RuntimeException (e); 114 } 115 } 116 117 public void remove() 118 { 119 throw new RuntimeException ("Illegal operation on ArchiveBrowser"); 120 } 121 } 122 | Popular Tags |