KickJava   Java API By Example, From Geeks To Geeks.

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


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.vfs.FileName;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystemException;
21 import org.apache.commons.vfs.FileType;
22 import org.apache.commons.vfs.provider.AbstractFileObject;
23
24 import java.io.InputStream JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.zip.ZipEntry JavaDoc;
27
28 /**
29  * A file in a Zip file system.
30  *
31  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
32  */

33 public class ZipFileObject
34     extends AbstractFileObject
35     implements FileObject
36 {
37     private final HashSet JavaDoc children = new HashSet JavaDoc();
38     private final ZipFileSystem fs;
39     // protected final ZipFile file;
40
protected ZipEntry JavaDoc entry;
41     private FileType type;
42
43     protected ZipFileObject(FileName name,
44                             ZipEntry JavaDoc entry,
45                             ZipFileSystem fs,
46                             boolean zipExists) throws FileSystemException
47     {
48         super(name, fs);
49         this.fs = fs;
50         setZipEntry(entry);
51         if (!zipExists)
52         {
53             type = FileType.IMAGINARY;
54         }
55     }
56
57     /**
58      * Sets the details for this file object.
59      */

60     protected void setZipEntry(final ZipEntry JavaDoc entry)
61     {
62         if (this.entry != null)
63         {
64             return;
65         }
66
67         if ((entry == null) || (entry.isDirectory()))
68         {
69             type = FileType.FOLDER;
70         }
71         else
72         {
73             type = FileType.FILE;
74         }
75
76         this.entry = entry;
77     }
78
79     /**
80      * Attaches a child
81      */

82     public void attachChild(FileName childName)
83     {
84         children.add(childName.getBaseName());
85     }
86
87     /**
88      * Returns true if this file is read-only.
89      */

90     public boolean isWriteable()
91     {
92         return false;
93     }
94
95     /**
96      * Returns the file's type.
97      */

98     protected FileType doGetType()
99     {
100         return type;
101     }
102
103     /**
104      * Lists the children of the file.
105      */

106     protected String JavaDoc[] doListChildren()
107     {
108         return (String JavaDoc[]) children.toArray(new String JavaDoc[children.size()]);
109     }
110
111     /**
112      * Returns the size of the file content (in bytes). Is only called if
113      * {@link #doGetType} returns {@link FileType#FILE}.
114      */

115     protected long doGetContentSize()
116     {
117         return entry.getSize();
118     }
119
120     /**
121      * Returns the last modified time of this file.
122      */

123     protected long doGetLastModifiedTime() throws Exception JavaDoc
124     {
125         return entry.getTime();
126     }
127
128     /**
129      * Creates an input stream to read the file content from. Is only called
130      * if {@link #doGetType} returns {@link FileType#FILE}. The input stream
131      * returned by this method is guaranteed to be closed before this
132      * method is called again.
133      */

134     protected InputStream JavaDoc doGetInputStream() throws Exception JavaDoc
135     {
136         return fs.getZipFile().getInputStream(entry);
137     }
138 }
139
Popular Tags