1 3 package jxxload_help; 4 5 import java.util.Vector ; 6 import java.util.Hashtable ; 7 import java.util.Enumeration ; 8 import java.util.zip.ZipFile ; 9 import java.util.zip.ZipEntry ; 10 import java.io.*; 11 12 public class PathVFS extends Object { 13 14 public interface VFS { 15 16 public InputStream open(String id); 17 18 } 19 20 public static class JarVFS implements VFS { 21 private ZipFile zipfile; 22 23 public JarVFS(String fname) throws IOException { 24 zipfile = new ZipFile (fname); 25 } 26 27 public InputStream open(String id) { 28 ZipEntry ent = zipfile.getEntry(id); 29 if (ent == null) return null; 30 try { 31 return zipfile.getInputStream(ent); 32 } catch(IOException e) { 33 return null; 34 } 35 } 36 37 } 38 39 public static class DirVFS implements VFS { 40 private String prefix; 41 42 public DirVFS(String dir) { 43 if (dir.length() == 0) 44 prefix = null; 45 else 46 prefix = dir; 47 } 48 49 public InputStream open(String id) { 50 File file = new File (prefix,id.replace('/',File.separatorChar)); 51 if (file.isFile()) { 52 try { 53 return new BufferedInputStream(new FileInputStream(file)); 54 } catch(IOException e) { 55 return null; 56 } 57 } 58 return null; 59 } 60 } 61 62 private Vector vfs = new Vector (); 63 private Hashtable once = new Hashtable (); 64 65 private final static Object PRESENT = new Object (); 66 67 public void addVFS(String fname) { 68 if (fname.length() == 0) { 69 if (!once.containsKey("")) { 70 once.put("",PRESENT); 71 vfs.addElement(new DirVFS("")); 72 } 73 return; 74 } 75 try { 76 File file = new File (fname); 77 String canon = file.getCanonicalPath().toString(); 78 if (!once.containsKey(canon)) { 79 once.put(canon,PRESENT); 80 if (file.isDirectory()) vfs.addElement(new DirVFS(fname)); 81 else if (file.exists() && (fname.endsWith(".jar") || fname.endsWith(".zip"))) { 82 vfs.addElement(new JarVFS(fname)); 83 } 84 } 85 86 } catch(IOException e) {} 87 } 88 89 public InputStream open(String id) { 90 for(Enumeration enumm = vfs.elements(); enumm.hasMoreElements();) { 91 VFS v = (VFS)enumm.nextElement(); 92 InputStream stream = v.open(id); 93 if (stream != null) return stream; 94 } 95 return null; 96 } 97 98 } 99 | Popular Tags |