View Javadoc
1   package org.codehaus.plexus.archiver.zip;
2   
3   import java.io.Closeable;
4   import java.io.File;
5   import java.io.IOException;
6   import java.nio.charset.Charset;
7   import java.util.Enumeration;
8   import java.util.Iterator;
9   import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
10  import org.apache.commons.compress.archivers.zip.ZipFile;
11  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoArchiveResourceCollection;
12  import org.codehaus.plexus.components.io.resources.EncodingSupported;
13  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
14  
15  public class PlexusArchiverZipFileResourceCollection
16      extends AbstractPlexusIoArchiveResourceCollection implements EncodingSupported
17  {
18  
19      /**
20       * The zip file resource collections role hint.
21       */
22      public static final String ROLE_HINT = "zip";
23  
24      private Charset charset = Charset.forName( "UTF-8" );
25  
26      @Override
27      protected Iterator<PlexusIoResource> getEntries()
28          throws IOException
29      {
30          final File f = getFile();
31          if ( f == null )
32          {
33              throw new IOException( "The tar archive file has not been set." );
34          }
35          final ZipFile zipFile = new ZipFile( f, charset != null ? charset.name() : "UTF8" );
36          return new CloseableIterator( zipFile );
37      }
38  
39      @Override
40      public boolean isConcurrentAccessSupported()
41      {
42          // Well maybe someday investigate if we can do concurrent zip
43          return false;
44      }
45  
46      class CloseableIterator
47          implements Iterator<PlexusIoResource>, Closeable
48      {
49  
50          final Enumeration en;
51  
52          private final ZipFile zipFile;
53  
54          public CloseableIterator( ZipFile zipFile )
55          {
56              this.en = zipFile.getEntriesInPhysicalOrder();
57              this.zipFile = zipFile;
58          }
59  
60          @Override
61          public boolean hasNext()
62          {
63              return en.hasMoreElements();
64          }
65  
66          @Override
67          public PlexusIoResource next()
68          {
69              final ZipArchiveEntry entry = (ZipArchiveEntry) en.nextElement();
70              return entry.isUnixSymlink()
71                         ? new ZipSymlinkResource( zipFile, entry, getStreamTransformer() )
72                         : new ZipResource( zipFile, entry, getStreamTransformer() );
73  
74          }
75  
76          @Override
77          public void remove()
78          {
79              throw new UnsupportedOperationException( "Removing isn't implemented." );
80          }
81  
82          @Override
83          public void close()
84              throws IOException
85          {
86              zipFile.close();
87          }
88  
89      }
90  
91      @Override
92      public void setEncoding( Charset charset )
93      {
94          this.charset = charset;
95      }
96  
97  }