KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > zip > ZipFileSystem


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.zip.ZipEntry JavaDoc;
37 import java.util.zip.ZipFile JavaDoc;
38
39 /**
40  * A read-only file system for Zip/Jar files.
41  *
42  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
43  */

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 JavaDoc file;
51     private ZipFile JavaDoc 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         // Make a local copy of the file
61
file = parentLayer.getFileSystem().replicateFile(parentLayer, Selectors.SELECT_SELF);
62
63         // Open the Zip file
64
if (!file.exists())
65         {
66             // Don't need to do anything
67
zipFile = null;
68             return;
69         }
70
71         // zipFile = createZipFile(this.file);
72
}
73
74     public void init() throws FileSystemException
75     {
76         super.init();
77
78         try
79         {
80             // Build the index
81
List JavaDoc strongRef = new ArrayList JavaDoc(100);
82             Enumeration JavaDoc entries = getZipFile().entries();
83             while (entries.hasMoreElements())
84             {
85                 ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
86                 FileName name = getFileSystemManager().resolveName(getRootName(), UriParser.encode(entry.getName()));
87
88                 // Create the file
89
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                 // Make sure all ancestors exist
103
// TODO - create these on demand
104
ZipFileObject parent = null;
105                 for (FileName parentName = name.getParent();
106                      parentName != null;
107                      fileObj = parent, parentName = parentName.getParent())
108                 {
109                     // Locate the parent
110
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                     // Attach child to parent
120
parent.attachChild(fileObj.getName());
121                 }
122             }
123         }
124         finally
125         {
126             closeCommunicationLink();
127         }
128     }
129
130     protected ZipFile JavaDoc getZipFile() throws FileSystemException
131     {
132         if (zipFile == null && this.file.exists())
133         {
134             ZipFile JavaDoc 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 JavaDoc entry) throws FileSystemException
144     {
145         return new ZipFileObject(name, entry, this, true);
146     }
147
148     protected ZipFile JavaDoc createZipFile(final File JavaDoc file) throws FileSystemException
149     {
150         try
151         {
152             return new ZipFile JavaDoc(file);
153         }
154         catch (IOException JavaDoc ioe)
155         {
156             throw new FileSystemException("vfs.provider.zip/open-zip-file.error", file, ioe);
157         }
158     }
159
160     protected void doCloseCommunicationLink()
161     {
162         // Release the zip file
163
try
164         {
165             if (zipFile != null)
166             {
167                 zipFile.close();
168                 zipFile = null;
169             }
170         }
171         catch (final IOException JavaDoc e)
172         {
173             // getLogger().warn("vfs.provider.zip/close-zip-file.error :" + file, e);
174
VfsLog.warn(getLogger(), log, "vfs.provider.zip/close-zip-file.error :" + file, e);
175         }
176     }
177
178     /**
179      * Returns the capabilities of this file system.
180      */

181     protected void addCapabilities(final Collection JavaDoc caps)
182     {
183         caps.addAll(ZipFileProvider.capabilities);
184     }
185
186     /**
187      * Creates a file object.
188      */

189     protected FileObject createFile(final FileName name) throws FileSystemException
190     {
191         // This is only called for files which do not exist in the Zip file
192
return new ZipFileObject(name, null, this, false);
193     }
194
195     /**
196      * will be called after all file-objects closed their streams.
197      */

198     protected void notifyAllStreamsClosed()
199     {
200         closeCommunicationLink();
201     }
202 }
203
Popular Tags