View Javadoc

1   package org.apache.maven.index.util.zip;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.ArrayList;
26  import java.util.Enumeration;
27  import java.util.List;
28  import java.util.zip.ZipEntry;
29  import java.util.zip.ZipFile;
30  
31  public class JavaZipFileHandle
32      extends AbstractZipHandle
33      implements ZipHandle
34  {
35      private final ZipFile zipFile;
36  
37      public JavaZipFileHandle( final File targetFile )
38          throws IOException
39      {
40          super( targetFile );
41  
42          this.zipFile = new ZipFile( targetFile );
43      }
44  
45      protected ZipFile getZipFile()
46      {
47          return zipFile;
48      }
49  
50      public boolean hasEntry( String path )
51          throws IOException
52      {
53          return getZipFile().getEntry( path ) != null;
54      }
55  
56      public List<String> getEntries()
57      {
58          return getEntries( new EntryNameFilter()
59          {
60              public boolean accepts( String entryName )
61              {
62                  return true;
63              }
64          } );
65      }
66  
67      public List<String> getEntries( EntryNameFilter filter )
68      {
69          ArrayList<String> entries = new ArrayList<String>();
70  
71          Enumeration<? extends ZipEntry> en = getZipFile().entries();
72  
73          while ( en.hasMoreElements() )
74          {
75              final ZipEntry e = en.nextElement();
76  
77              final String name = e.getName();
78  
79              if ( filter != null && !filter.accepts( name ) )
80              {
81                  continue;
82              }
83  
84              entries.add( name );
85          }
86  
87          return entries;
88      }
89  
90      public InputStream getEntryContent( String path )
91          throws IOException
92      {
93          ZipEntry entry = getZipFile().getEntry( path );
94  
95          if ( entry != null )
96          {
97              return getZipFile().getInputStream( entry );
98          }
99          else
100         {
101             return null;
102         }
103     }
104 
105     public void close()
106         throws IOException
107     {
108         getZipFile().close();
109     }
110 }