1 11 package org.eclipse.jdt.internal.compiler.apt.util; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.nio.charset.Charset ; 16 import java.util.ArrayList ; 17 import java.util.Enumeration ; 18 import java.util.Hashtable ; 19 import java.util.Set ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipException ; 22 import java.util.zip.ZipFile ; 23 24 27 public class Archive { 28 29 public static final Archive UNKNOWN_ARCHIVE = new Archive(); 30 31 ZipFile zipFile; 32 File file; 33 protected Hashtable <String , ArrayList <String >> packagesCache; 34 35 private Archive() { 36 } 37 38 public Archive(File file) throws ZipException , IOException { 39 this.file = file; 40 this.zipFile = new ZipFile (file); 41 initialize(); 42 } 43 44 private void initialize() { 45 this.packagesCache = new Hashtable <String , ArrayList <String >>(); 47 nextEntry : for (Enumeration <? extends ZipEntry > e = this.zipFile.entries(); e.hasMoreElements(); ) { 48 String fileName = ((ZipEntry ) e.nextElement()).getName(); 49 50 int last = fileName.lastIndexOf('/'); 52 String packageName = fileName.substring(0, last + 1); 54 String typeName = fileName.substring(last + 1); 55 ArrayList <String > types = this.packagesCache.get(packageName); 56 if (types == null) { 57 if (typeName.length() == 0) { 59 continue nextEntry; 60 } 61 types = new ArrayList <String >(); 62 types.add(typeName); 63 this.packagesCache.put(packageName, types); 64 } else { 65 types.add(typeName); 66 } 67 } 68 } 69 70 public ArchiveFileObject getArchiveFileObject(String entryName, Charset charset) { 71 return new ArchiveFileObject(this.file, this.zipFile, entryName, charset); 72 } 73 74 public boolean contains(String entryName) { 75 return this.zipFile.getEntry(entryName) != null; 76 } 77 78 public Set <String > allPackages() { 79 if (this.packagesCache == null) { 80 this.initialize(); 81 } 82 return this.packagesCache.keySet(); 83 } 84 85 public ArrayList <String > getTypes(String packageName) { 86 return this.packagesCache.get(packageName); 88 } 89 90 public void flush() { 91 this.packagesCache = null; 92 } 93 94 public void close() { 95 try { 96 if (this.zipFile != null) this.zipFile.close(); 97 this.packagesCache = null; 98 } catch (IOException e) { 99 } 101 } 102 } 103 | Popular Tags |