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  
29  import de.schlichtherle.truezip.zip.ZipEntry;
30  import de.schlichtherle.truezip.zip.ZipFile;
31  
32  public class TrueZipZipFileHandle
33      extends AbstractZipHandle
34      implements ZipHandle
35  {
36      private final ZipFile zipFile;
37  
38      public TrueZipZipFileHandle( final File targetFile )
39          throws IOException
40      {
41          super( targetFile );
42  
43          this.zipFile = new ZipFile( targetFile );
44      }
45  
46      protected ZipFile getZipFile()
47      {
48          return zipFile;
49      }
50  
51      public boolean hasEntry( String path )
52          throws IOException
53      {
54          return getZipFile().getEntry( path ) != null;
55      }
56  
57      public List<String> getEntries()
58      {
59          return getEntries( new EntryNameFilter()
60          {
61              public boolean accepts( String entryName )
62              {
63                  return true;
64              }
65          } );
66      }
67  
68      public List<String> getEntries( EntryNameFilter filter )
69      {
70          ArrayList<String> entries = new ArrayList<String>();
71  
72          Enumeration<? extends ZipEntry> en = getZipFile().entries();
73  
74          while ( en.hasMoreElements() )
75          {
76              final ZipEntry e = en.nextElement();
77  
78              final String name = e.getName();
79  
80              if ( filter != null && !filter.accepts( name ) )
81              {
82                  continue;
83              }
84  
85              entries.add( name );
86          }
87  
88          return entries;
89      }
90  
91      public InputStream getEntryContent( String path )
92          throws IOException
93      {
94          ZipEntry entry = getZipFile().getEntry( path );
95  
96          if ( entry != null )
97          {
98              return getZipFile().getInputStream( entry );
99          }
100         else
101         {
102             return null;
103         }
104     }
105 
106     public void close()
107         throws IOException
108     {
109         getZipFile().close();
110     }
111 
112 }