View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.zip;
18  
19  import java.io.InputStream;
20  import java.util.HashSet;
21  import java.util.zip.ZipEntry;
22  
23  import org.apache.commons.vfs2.FileName;
24  import org.apache.commons.vfs2.FileSystemException;
25  import org.apache.commons.vfs2.FileType;
26  import org.apache.commons.vfs2.provider.AbstractFileName;
27  import org.apache.commons.vfs2.provider.AbstractFileObject;
28  
29  /**
30   * A file in a Zip file system.
31   */
32  public class ZipFileObject extends AbstractFileObject<ZipFileSystem>
33  {
34      /** The ZipEntry. */
35      protected ZipEntry entry;
36      private final HashSet<String> children = new HashSet<String>();
37      // protected final ZipFile file;
38  
39      private FileType type;
40  
41      protected ZipFileObject(final AbstractFileName name,
42                              final ZipEntry entry,
43                              final ZipFileSystem fs,
44                              final boolean zipExists) throws FileSystemException
45      {
46          super(name, fs);
47          setZipEntry(entry);
48          if (!zipExists)
49          {
50              type = FileType.IMAGINARY;
51          }
52      }
53  
54      /**
55       * Sets the details for this file object.
56       * @param entry ZIP information related to this file.
57       */
58      protected void setZipEntry(final ZipEntry entry)
59      {
60          if (this.entry != null)
61          {
62              return;
63          }
64  
65          if (entry == null || entry.isDirectory())
66          {
67              type = FileType.FOLDER;
68          }
69          else
70          {
71              type = FileType.FILE;
72          }
73  
74          this.entry = entry;
75      }
76  
77      /**
78       * Attaches a child.
79       * <p>
80       * TODO: Shouldn't this method have package-only visibility?
81       * Cannot change this without breaking binary compatibility.
82       *
83       * @param childName The name of the child.
84       */
85      public void attachChild(final FileName childName)
86      {
87          children.add(childName.getBaseName());
88      }
89  
90      /**
91       * Determines if this file can be written to.
92       *
93       * @return {@code true} if this file is writeable, {@code false} if not.
94       * @throws FileSystemException if an error occurs.
95       */
96      @Override
97      public boolean isWriteable() throws FileSystemException
98      {
99          return false;
100     }
101 
102     /**
103      * Returns the file's type.
104      */
105     @Override
106     protected FileType doGetType()
107     {
108         return type;
109     }
110 
111     /**
112      * Lists the children of the file.
113      */
114     @Override
115     protected String[] doListChildren()
116     {
117         try
118         {
119             if (!getType().hasChildren())
120             {
121                 return null;
122             }
123         }
124         catch (final FileSystemException e)
125         {
126             // should not happen as the type has already been cached.
127             throw new RuntimeException(e);
128         }
129 
130         return children.toArray(new String[children.size()]);
131     }
132 
133     /**
134      * Returns the size of the file content (in bytes).  Is only called if
135      * {@link #doGetType} returns {@link FileType#FILE}.
136      */
137     @Override
138     protected long doGetContentSize()
139     {
140         return entry.getSize();
141     }
142 
143     /**
144      * Returns the last modified time of this file.
145      */
146     @Override
147     protected long doGetLastModifiedTime() throws Exception
148     {
149         return entry.getTime();
150     }
151 
152     /**
153      * Creates an input stream to read the file content from.  Is only called
154      * if  {@link #doGetType} returns {@link FileType#FILE}.  The input stream
155      * returned by this method is guaranteed to be closed before this
156      * method is called again.
157      */
158     @Override
159     protected InputStream doGetInputStream() throws Exception
160     {
161         // VFS-210: zip allows to gather an input stream even from a directory and will
162         // return -1 on the first read. getType should not be expensive and keeps the tests
163         // running
164         if (!getType().hasContent())
165         {
166             throw new FileSystemException("vfs.provider/read-not-file.error", getName());
167         }
168 
169         return getAbstractFileSystem().getZipFile().getInputStream(entry);
170     }
171 }