1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.provider.zip;
18
19 import org.apache.commons.vfs.FileName;
20 import org.apache.commons.vfs.FileObject;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.FileType;
23 import org.apache.commons.vfs.provider.AbstractFileObject;
24
25 import java.io.InputStream;
26 import java.util.HashSet;
27 import java.util.zip.ZipEntry;
28
29 /***
30 * A file in a Zip file system.
31 *
32 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
33 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
34 */
35 public class ZipFileObject
36 extends AbstractFileObject
37 implements FileObject
38 {
39 private final HashSet children = new HashSet();
40 private final ZipFileSystem fs;
41
42 protected ZipEntry entry;
43 private FileType type;
44
45 protected ZipFileObject(FileName name,
46 ZipEntry entry,
47 ZipFileSystem fs,
48 boolean zipExists) throws FileSystemException
49 {
50 super(name, fs);
51 this.fs = fs;
52 setZipEntry(entry);
53 if (!zipExists)
54 {
55 type = FileType.IMAGINARY;
56 }
57 }
58
59 /***
60 * Sets the details for this file object.
61 */
62 protected void setZipEntry(final ZipEntry entry)
63 {
64 if (this.entry != null)
65 {
66 return;
67 }
68
69 if ((entry == null) || (entry.isDirectory()))
70 {
71 type = FileType.FOLDER;
72 }
73 else
74 {
75 type = FileType.FILE;
76 }
77
78 this.entry = entry;
79 }
80
81 /***
82 * Attaches a child
83 */
84 public void attachChild(FileName childName)
85 {
86 children.add(childName.getBaseName());
87 }
88
89 /***
90 * Determines if this file can be written to.
91 *
92 * @return <code>true</code> if this file is writeable, <code>false</code> if not.
93 */
94 public boolean isWriteable() throws FileSystemException
95 {
96 return false;
97 }
98
99 /***
100 * Returns the file's type.
101 */
102 protected FileType doGetType()
103 {
104 return type;
105 }
106
107 /***
108 * Lists the children of the file.
109 */
110 protected String[] doListChildren()
111 {
112 return (String[]) children.toArray(new String[children.size()]);
113 }
114
115 /***
116 * Returns the size of the file content (in bytes). Is only called if
117 * {@link #doGetType} returns {@link FileType#FILE}.
118 */
119 protected long doGetContentSize()
120 {
121 return entry.getSize();
122 }
123
124 /***
125 * Returns the last modified time of this file.
126 */
127 protected long doGetLastModifiedTime() throws Exception
128 {
129 return entry.getTime();
130 }
131
132 /***
133 * Creates an input stream to read the file content from. Is only called
134 * if {@link #doGetType} returns {@link FileType#FILE}. The input stream
135 * returned by this method is guaranteed to be closed before this
136 * method is called again.
137 */
138 protected InputStream doGetInputStream() throws Exception
139 {
140 return fs.getZipFile().getInputStream(entry);
141 }
142 }