You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathVFS.java 2.7KB

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