1 16 package org.apache.commons.vfs.provider.zip; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.commons.vfs.FileName; 21 import org.apache.commons.vfs.FileObject; 22 import org.apache.commons.vfs.FileSystem; 23 import org.apache.commons.vfs.FileSystemException; 24 import org.apache.commons.vfs.FileSystemOptions; 25 import org.apache.commons.vfs.Selectors; 26 import org.apache.commons.vfs.VfsLog; 27 import org.apache.commons.vfs.provider.AbstractFileSystem; 28 import org.apache.commons.vfs.provider.UriParser; 29 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Enumeration ; 35 import java.util.List ; 36 import java.util.zip.ZipEntry ; 37 import java.util.zip.ZipFile ; 38 39 44 public class ZipFileSystem 45 extends AbstractFileSystem 46 implements FileSystem 47 { 48 private final static Log log = LogFactory.getLog(ZipFileSystem.class); 49 50 private final File file; 51 private ZipFile zipFile; 52 53 public ZipFileSystem(final FileName rootName, 54 final FileObject parentLayer, 55 final FileSystemOptions fileSystemOptions) 56 throws FileSystemException 57 { 58 super(rootName, parentLayer, fileSystemOptions); 59 60 file = parentLayer.getFileSystem().replicateFile(parentLayer, Selectors.SELECT_SELF); 62 63 if (!file.exists()) 65 { 66 zipFile = null; 68 return; 69 } 70 71 } 73 74 public void init() throws FileSystemException 75 { 76 super.init(); 77 78 try 79 { 80 List strongRef = new ArrayList (100); 82 Enumeration entries = getZipFile().entries(); 83 while (entries.hasMoreElements()) 84 { 85 ZipEntry entry = (ZipEntry ) entries.nextElement(); 86 FileName name = getFileSystemManager().resolveName(getRootName(), UriParser.encode(entry.getName())); 87 88 ZipFileObject fileObj; 90 if (entry.isDirectory() && getFileFromCache(name) != null) 91 { 92 fileObj = (ZipFileObject) getFileFromCache(name); 93 fileObj.setZipEntry(entry); 94 continue; 95 } 96 97 fileObj = createZipFileObject(name, entry); 98 putFileToCache(fileObj); 99 strongRef.add(fileObj); 100 fileObj.holdObject(strongRef); 101 102 ZipFileObject parent = null; 105 for (FileName parentName = name.getParent(); 106 parentName != null; 107 fileObj = parent, parentName = parentName.getParent()) 108 { 109 parent = (ZipFileObject) getFileFromCache(parentName); 111 if (parent == null) 112 { 113 parent = createZipFileObject(parentName, null); 114 putFileToCache(parent); 115 strongRef.add(parent); 116 parent.holdObject(strongRef); 117 } 118 119 parent.attachChild(fileObj.getName()); 121 } 122 } 123 } 124 finally 125 { 126 closeCommunicationLink(); 127 } 128 } 129 130 protected ZipFile getZipFile() throws FileSystemException 131 { 132 if (zipFile == null && this.file.exists()) 133 { 134 ZipFile zipFile = createZipFile(this.file); 135 136 this.zipFile = zipFile; 137 } 138 139 return zipFile; 140 } 141 142 protected ZipFileObject createZipFileObject(final FileName name, 143 final ZipEntry entry) throws FileSystemException 144 { 145 return new ZipFileObject(name, entry, this, true); 146 } 147 148 protected ZipFile createZipFile(final File file) throws FileSystemException 149 { 150 try 151 { 152 return new ZipFile (file); 153 } 154 catch (IOException ioe) 155 { 156 throw new FileSystemException("vfs.provider.zip/open-zip-file.error", file, ioe); 157 } 158 } 159 160 protected void doCloseCommunicationLink() 161 { 162 try 164 { 165 if (zipFile != null) 166 { 167 zipFile.close(); 168 zipFile = null; 169 } 170 } 171 catch (final IOException e) 172 { 173 VfsLog.warn(getLogger(), log, "vfs.provider.zip/close-zip-file.error :" + file, e); 175 } 176 } 177 178 181 protected void addCapabilities(final Collection caps) 182 { 183 caps.addAll(ZipFileProvider.capabilities); 184 } 185 186 189 protected FileObject createFile(final FileName name) throws FileSystemException 190 { 191 return new ZipFileObject(name, null, this, false); 193 } 194 195 198 protected void notifyAllStreamsClosed() 199 { 200 closeCommunicationLink(); 201 } 202 } 203 | Popular Tags |