1 19 20 package edu.umd.cs.findbugs.classfile.impl; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.MissingResourceException ; 28 import java.util.NoSuchElementException ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipException ; 31 import java.util.zip.ZipFile ; 32 33 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry; 34 import edu.umd.cs.findbugs.classfile.ICodeBaseIterator; 35 import edu.umd.cs.findbugs.classfile.ICodeBaseLocator; 36 import edu.umd.cs.findbugs.classfile.IScannableCodeBase; 37 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException; 38 39 44 public class ZipFileCodeBase extends AbstractScannableCodeBase { 45 ZipFile zipFile; 46 47 55 public ZipFileCodeBase(ICodeBaseLocator codeBaseLocator, File file) throws IOException { 56 super(codeBaseLocator); 57 try { 58 this.zipFile = new ZipFile (file); 59 setLastModifiedTime(file.lastModified()); 60 } catch (ZipException e) { 61 throw new ZipException ("Error opening " + file); 62 } 63 } 64 65 68 public ICodeBaseEntry lookupResource(String resourceName) throws ResourceNotFoundException { 69 resourceName = translateResourceName(resourceName); 73 74 try { 75 ZipEntry entry = zipFile.getEntry(resourceName); 76 if (entry == null) { 77 throw new ResourceNotFoundException(resourceName); 78 } 79 return new ZipFileCodeBaseEntry(this, entry); 80 } 81 catch (IllegalStateException ise) { 82 throw new ResourceNotFoundException(resourceName, ise); 84 } 85 } 86 87 public ICodeBaseIterator iterator() { 88 final Enumeration <? extends ZipEntry > zipEntryEnumerator = zipFile.entries(); 89 90 return new ICodeBaseIterator() { 91 ZipFileCodeBaseEntry nextEntry; 92 93 public boolean hasNext() { 94 scanForNextEntry(); 95 return nextEntry != null; 96 } 97 98 101 public ICodeBaseEntry next() throws InterruptedException { 102 scanForNextEntry(); 103 if (nextEntry == null) { 104 throw new NoSuchElementException (); 105 } 106 ICodeBaseEntry result = nextEntry; 107 nextEntry = null; 108 return result; 109 } 110 111 private void scanForNextEntry() { 112 while (nextEntry == null) { 113 if (!zipEntryEnumerator.hasMoreElements()) { 114 return; 115 } 116 117 ZipEntry zipEntry = zipEntryEnumerator.nextElement(); 118 119 if (!zipEntry.isDirectory()) { 120 setLastModifiedTime(zipEntry.getTime()); 121 nextEntry = new ZipFileCodeBaseEntry(ZipFileCodeBase.this, zipEntry); 122 break; 123 } 124 } 125 } 126 127 }; 128 } 129 130 133 public String getPathName() { 134 return zipFile.getName(); 135 } 136 137 140 public void close() { 141 try { 142 zipFile.close(); 143 } catch (IOException e) { 144 } 146 } 147 148 151 @Override 152 public String toString() { 153 return zipFile.getName(); 154 } 155 } 156 | Popular Tags |