带预览zip和jar文件功能的文件选择器,jar选择器,带预览zip和jar文件
分享于 点击 36237 次 点评:258
带预览zip和jar文件功能的文件选择器,jar选择器,带预览zip和jar文件
带预览zip和jar文件功能的文件选择器
[Java]代码
import java.util.zip.*; public class ZipEntryFileProxy extends File { ZipFileProxy zip; ZipFile zipfile; String name, path; File parent; ZipEntry entry; public ZipEntryFileProxy(ZipFileProxy zip, ZipFile zipfile, String path, File parent) { super(""); this.zip = zip; this.zipfile = zipfile; this.path = path; this.parent = parent; this.entry = zipfile.getEntry(path); // determine if the entry is a directory String tmp = path; if(entry.isDirectory()) { tmp = path.substring(0,path.length()-1); } // then calculate the name int brk = tmp.lastIndexOf("/"); name = path; if(brk != -1) { name = tmp.substring(brk+1); } } public boolean exists() { return true; } public int hashCode() { return name.hashCode() ^ 1234321; } public String getName() { return name; } public String getPath() { return path; } public boolean isDirectory() { return entry.isDirectory(); } public boolean isAbsolute() { return true; } public String getAbsolutePath() { return path; } public File getAbsoluteFile() { return this; } public File getCanonicalFile() { return this; } public File getParentFile() { return parent; } public boolean equals(Object obj) { if(obj instanceof ZipEntryFileProxy) { ZipEntryFileProxy zo = (ZipEntryFileProxy)obj; if(zo.getAbsolutePath().equals(getAbsolutePath())) { return true; } } return false; } public InputStream getInputStream() throws IOException { return zipfile.getInputStream(entry); } public File[] listFiles() { Map children = (Map)zip.hash.get(path); File[] files = new File[children.size()]; Iterator it = children.keySet().iterator(); int count = 0; while(it.hasNext()) { String name = (String)it.next(); files[count] = new ZipEntryFileProxy(zip, zipfile, name,this); count++; } return files; }} public class ZipFileProxy extends File { protected Map hash; private ZipFile zipfile; private File real_file; public ZipFileProxy(File file) { super(file.getAbsolutePath()); try { this.hash = new HashMap(); this.real_file = file; zipfile = new ZipFile(file,ZipFile.OPEN_READ); hash.put("",new HashMap()); Enumeration en = zipfile.entries(); parse(en); } catch (IOException ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } } public String getPath() { return real_file.getPath(); } public boolean exists() { return real_file.exists(); } public String getName() { return real_file.getName(); } public File[] getFiles(String dir) { Map children = (Map)hash.get(dir); File[] files = new File[children.size()]; Iterator it = children.keySet().iterator(); int count = 0; while(it.hasNext()) { String name = (String)it.next(); files[count] = new ZipEntryFileProxy(this, zipfile, name, this); count++; } return files; } /* create a hashtable of the entries and their paths */ private void parse(Enumeration en) { while(en.hasMoreElements()) { ZipEntry ze = (ZipEntry)en.nextElement(); String full_name = ze.getName(); String name = full_name; if(ze.isDirectory()) { name = full_name.substring(0,full_name.length()-1); } int brk = name.lastIndexOf("/"); String parent = ""; if(brk != -1) { parent = name.substring(0,brk+1); } String node_name = name; if(brk != -1) { node_name = full_name.substring(brk+1); } if(ze.isDirectory()) { HashMap children = new HashMap(); hash.put(full_name,children); } Map parent_children = (Map)hash.get(parent); parent_children.put(full_name,""); } } } public class ZipFileSystemView extends FileSystemView { public ZipFileSystemView() throws IOException { } public File createNewFolder(File file) { return null; } public File createFileObject(File dir, String filename) { if(dir instanceof ZipEntryFileProxy) { ZipEntryFileProxy zdir = (ZipEntryFileProxy) dir; return new ZipEntryFileProxy(zdir.zip, zdir.zipfile, filename, dir); } return super.createFileObject(dir,filename); } public File getChild(File dir, String filename) { if(dir instanceof ZipEntryFileProxy) { ZipEntryFileProxy zdir = (ZipEntryFileProxy) dir; return new ZipEntryFileProxy(zdir.zip,zdir.zipfile, dir.getPath()+filename,dir); } return super.getChild(dir,filename); } public String getSystemDisplayName(File f) { if(f instanceof ZipEntryFileProxy) { return f.getName(); } return super.getSystemDisplayName(f); } public File getParentDirectory(File dir) { if(dir instanceof ZipEntryFileProxy) { return dir.getParentFile(); } return super.getParentDirectory(dir); } public File[] getFiles(File dir, boolean useFileHiding) { if(dir.getName().endsWith(".zip")) { ZipFileProxy proxy = new ZipFileProxy(dir); File[] fs = proxy.getFiles(""); return fs; } if(dir instanceof ZipEntryFileProxy) { return dir.listFiles(); } return super.getFiles(dir,useFileHiding); } public Boolean isTraversable(File f) { if(f.getName().endsWith(".zip")) { return new Boolean(true); } if(f instanceof ZipEntryFileProxy) { boolean b = ((ZipEntryFileProxy)f).isDirectory(); return new Boolean(b); } return super.isTraversable(f); } } public class ZipTest { public static void main(String[] args) throws Exception { FileSystemView fsv = new ZipFileSystemView(); JFileChooser chooser = new JFileChooser("."); chooser.setFileSystemView(fsv); chooser.showOpenDialog(null); File file = chooser.getSelectedFile(); System.out.println("Got the file: " + file + " " + file.getClass()); InputStream in = null; if(file instanceof ZipEntryFileProxy) { in = ((ZipEntryFileProxy)file).getInputStream(); } else { in = new FileInputStream(file); } } }
用户点评