1 13 package org.eclipse.ui.internal.wizards.datatransfer; 14 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.ArrayList ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.zip.ZipEntry ; 23 import java.util.zip.ZipFile ; 24 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 28 29 35 public class ZipLeveledStructureProvider implements 36 ILeveledImportStructureProvider { 37 private ZipFile zipFile; 38 39 private ZipEntry root = new ZipEntry ("/"); 41 private Map children; 42 43 private Map directoryEntryCache = new HashMap (); 44 45 private int stripLevel; 46 47 55 public ZipLeveledStructureProvider(ZipFile sourceFile) { 56 super(); 57 zipFile = sourceFile; 58 stripLevel = 0; 59 } 60 61 68 protected ZipEntry createContainer(IPath pathname) { 69 ZipEntry existingEntry = (ZipEntry ) directoryEntryCache.get(pathname); 70 if (existingEntry != null) { 71 return existingEntry; 72 } 73 74 ZipEntry parent; 75 if (pathname.segmentCount() == 1) { 76 parent = root; 77 } else { 78 parent = createContainer(pathname.removeLastSegments(1)); 79 } 80 ZipEntry newEntry = new ZipEntry (pathname.toString()); 81 directoryEntryCache.put(pathname, newEntry); 82 List childList = new ArrayList (); 83 children.put(newEntry, childList); 84 85 List parentChildList = (List ) children.get(parent); 86 parentChildList.add(newEntry); 87 return newEntry; 88 } 89 90 93 protected void createFile(ZipEntry entry) { 94 IPath pathname = new Path(entry.getName()); 95 ZipEntry parent; 96 if (pathname.segmentCount() == 1) { 97 parent = root; 98 } else { 99 parent = (ZipEntry ) directoryEntryCache.get(pathname 100 .removeLastSegments(1)); 101 } 102 103 List childList = (List ) children.get(parent); 104 childList.add(entry); 105 } 106 107 110 public List getChildren(Object element) { 111 if (children == null) { 112 initialize(); 113 } 114 115 return ((List ) children.get(element)); 116 } 117 118 121 public InputStream getContents(Object element) { 122 try { 123 return zipFile.getInputStream((ZipEntry ) element); 124 } catch (IOException e) { 125 IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e); 126 return null; 127 } 128 } 129 130 133 private String stripPath(String path) { 134 String pathOrig = new String (path); 135 for (int i = 0; i < stripLevel; i++) { 136 int firstSep = path.indexOf('/'); 137 if (firstSep == 0) { 140 path = path.substring(1); 141 firstSep = path.indexOf('/'); 142 } 143 if (firstSep == -1) { 146 return pathOrig; 147 } 148 path = path.substring(firstSep); 149 } 150 return path; 151 } 152 153 156 public String getFullPath(Object element) { 157 return stripPath(((ZipEntry ) element).getName()); 158 } 159 160 163 public String getLabel(Object element) { 164 if (element.equals(root)) { 165 return ((ZipEntry ) element).getName(); 166 } 167 168 return stripPath(new Path(((ZipEntry ) element).getName()).lastSegment()); 169 } 170 171 176 public Object getRoot() { 177 return root; 178 } 179 180 185 public ZipFile getZipFile() { 186 return zipFile; 187 } 188 189 190 194 public boolean closeArchive(){ 195 try { 196 getZipFile().close(); 197 } catch (IOException e) { 198 IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose 199 + getZipFile().getName(), e); 200 return false; 201 } 202 return true; 203 } 204 205 209 protected void initialize() { 210 children = new HashMap (1000); 211 212 children.put(root, new ArrayList ()); 213 Enumeration entries = zipFile.entries(); 214 while (entries.hasMoreElements()) { 215 ZipEntry entry = (ZipEntry ) entries.nextElement(); 216 IPath path = new Path(entry.getName()).addTrailingSeparator(); 217 218 if (entry.isDirectory()) { 219 createContainer(path); 220 } else 221 { 222 int pathSegmentCount = path.segmentCount(); 225 if (pathSegmentCount > 1) { 226 createContainer(path.uptoSegment(pathSegmentCount - 1)); 227 } 228 createFile(entry); 229 } 230 } 231 } 232 233 236 public boolean isFolder(Object element) { 237 return ((ZipEntry ) element).isDirectory(); 238 } 239 240 public void setStrip(int level) { 241 stripLevel = level; 242 } 243 244 public int getStrip() { 245 return stripLevel; 246 } 247 } 248 | Popular Tags |