1 package org.apache.maven.index.util.zip;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }