001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.zip;
018:
019: import org.apache.commons.vfs.FileName;
020: import org.apache.commons.vfs.FileObject;
021: import org.apache.commons.vfs.FileSystemException;
022: import org.apache.commons.vfs.FileType;
023: import org.apache.commons.vfs.provider.AbstractFileObject;
024:
025: import java.io.InputStream;
026: import java.util.HashSet;
027: import java.util.zip.ZipEntry;
028:
029: /**
030: * A file in a Zip file system.
031: *
032: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
033: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
034: */
035: public class ZipFileObject extends AbstractFileObject implements
036: FileObject {
037: private final HashSet children = new HashSet();
038: private final ZipFileSystem fs;
039: // protected final ZipFile file;
040: protected ZipEntry entry;
041: private FileType type;
042:
043: protected ZipFileObject(FileName name, ZipEntry entry,
044: ZipFileSystem fs, boolean zipExists)
045: throws FileSystemException {
046: super (name, fs);
047: this .fs = fs;
048: setZipEntry(entry);
049: if (!zipExists) {
050: type = FileType.IMAGINARY;
051: }
052: }
053:
054: /**
055: * Sets the details for this file object.
056: */
057: protected void setZipEntry(final ZipEntry entry) {
058: if (this .entry != null) {
059: return;
060: }
061:
062: if ((entry == null) || (entry.isDirectory())) {
063: type = FileType.FOLDER;
064: } else {
065: type = FileType.FILE;
066: }
067:
068: this .entry = entry;
069: }
070:
071: /**
072: * Attaches a child
073: */
074: public void attachChild(FileName childName) {
075: children.add(childName.getBaseName());
076: }
077:
078: /**
079: * Determines if this file can be written to.
080: *
081: * @return <code>true</code> if this file is writeable, <code>false</code> if not.
082: */
083: public boolean isWriteable() throws FileSystemException {
084: return false;
085: }
086:
087: /**
088: * Returns the file's type.
089: */
090: protected FileType doGetType() {
091: return type;
092: }
093:
094: /**
095: * Lists the children of the file.
096: */
097: protected String[] doListChildren() {
098: return (String[]) children.toArray(new String[children.size()]);
099: }
100:
101: /**
102: * Returns the size of the file content (in bytes). Is only called if
103: * {@link #doGetType} returns {@link FileType#FILE}.
104: */
105: protected long doGetContentSize() {
106: return entry.getSize();
107: }
108:
109: /**
110: * Returns the last modified time of this file.
111: */
112: protected long doGetLastModifiedTime() throws Exception {
113: return entry.getTime();
114: }
115:
116: /**
117: * Creates an input stream to read the file content from. Is only called
118: * if {@link #doGetType} returns {@link FileType#FILE}. The input stream
119: * returned by this method is guaranteed to be closed before this
120: * method is called again.
121: */
122: protected InputStream doGetInputStream() throws Exception {
123: return fs.getZipFile().getInputStream(entry);
124: }
125: }
|