1 11 package org.eclipse.ui.wizards.datatransfer; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipFile ; 22 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 26 27 31 public class ZipFileStructureProvider implements IImportStructureProvider { 32 private ZipFile zipFile; 33 34 private ZipEntry root = new ZipEntry ("/"); 36 private Map children; 37 38 private Map directoryEntryCache = new HashMap (); 39 40 46 public ZipFileStructureProvider(ZipFile sourceFile) { 47 super(); 48 zipFile = sourceFile; 49 } 50 51 54 protected void addToChildren(ZipEntry parent, ZipEntry child) { 55 List childList = (List ) children.get(parent); 56 if (childList == null) { 57 childList = new ArrayList (); 58 children.put(parent, childList); 59 } 60 61 childList.add(child); 62 } 63 64 68 protected void createContainer(IPath pathname) { 69 if (directoryEntryCache.containsKey(pathname)) { 70 return; 71 } 72 73 ZipEntry parent; 74 if (pathname.segmentCount() == 1) { 75 parent = root; 76 } else { 77 parent = (ZipEntry ) directoryEntryCache.get(pathname 78 .removeLastSegments(1)); 79 } 80 81 ZipEntry newEntry = new ZipEntry (pathname.toString()); 82 directoryEntryCache.put(pathname, newEntry); 83 addToChildren(parent, newEntry); 84 } 85 86 89 protected void createFile(ZipEntry entry) { 90 IPath pathname = new Path(entry.getName()); 91 ZipEntry parent; 92 if (pathname.segmentCount() == 1) { 93 parent = root; 94 } else { 95 parent = (ZipEntry ) directoryEntryCache.get(pathname 96 .removeLastSegments(1)); 97 } 98 99 addToChildren(parent, entry); 100 } 101 102 105 public List getChildren(Object element) { 106 if (children == null) { 107 initialize(); 108 } 109 110 return ((List ) children.get(element)); 111 } 112 113 116 public InputStream getContents(Object element) { 117 try { 118 return zipFile.getInputStream((ZipEntry ) element); 119 } catch (IOException e) { 120 IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e); 121 return null; 122 } 123 } 124 125 128 public String getFullPath(Object element) { 129 return ((ZipEntry ) element).getName(); 130 } 131 132 135 public String getLabel(Object element) { 136 if (element.equals(root)) { 137 return ((ZipEntry ) element).getName(); 138 } 139 140 return new Path(((ZipEntry ) element).getName()).lastSegment(); 141 } 142 143 148 public ZipEntry getRoot() { 149 return root; 150 } 151 152 157 public ZipFile getZipFile() { 158 return zipFile; 159 } 160 161 165 protected void initialize() { 166 children = new HashMap (1000); 167 168 Enumeration entries = zipFile.entries(); 169 while (entries.hasMoreElements()) { 170 ZipEntry entry = (ZipEntry ) entries.nextElement(); 171 if (!entry.isDirectory()) { 172 IPath path = new Path(entry.getName()).addTrailingSeparator(); 173 int pathSegmentCount = path.segmentCount(); 174 175 for (int i = 1; i < pathSegmentCount; i++) { 176 createContainer(path.uptoSegment(i)); 177 } 178 createFile(entry); 179 } 180 } 181 } 182 183 186 public boolean isFolder(Object element) { 187 return ((ZipEntry ) element).isDirectory(); 188 } 189 } 190 | Popular Tags |