View Javadoc
1   package org.codehaus.plexus.archiver.zip;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.net.URL;
6   import javax.annotation.Nonnull;
7   import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
8   import org.apache.commons.compress.archivers.zip.ZipFile;
9   import org.codehaus.plexus.archiver.UnixStat;
10  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
11  import org.codehaus.plexus.components.io.attributes.SimpleResourceAttributes;
12  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
13  import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
14  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResource;
15  import org.codehaus.plexus.components.io.resources.ClosingInputStream;
16  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
17  
18  public class ZipResource extends AbstractPlexusIoResource
19      implements ResourceAttributeSupplier
20  {
21  
22      private final org.apache.commons.compress.archivers.zip.ZipFile zipFile;
23  
24      private final ZipArchiveEntry entry;
25  
26      private final InputStreamTransformer streamTransformer;
27  
28      private PlexusIoResourceAttributes attributes;
29  
30      public ZipResource( ZipFile zipFile, ZipArchiveEntry entry, InputStreamTransformer streamTransformer )
31      {
32          super( entry.getName(), getLastModofied( entry ),
33                 entry.isDirectory() ? PlexusIoResource.UNKNOWN_RESOURCE_SIZE : entry.getSize(), !entry.isDirectory(),
34                 entry.isDirectory(), true );
35  
36          this.zipFile = zipFile;
37          this.entry = entry;
38          this.streamTransformer = streamTransformer;
39      }
40  
41      private static long getLastModofied( ZipArchiveEntry entry )
42      {
43          long l = entry.getLastModifiedDate().getTime();
44          return l == -1 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l;
45      }
46  
47      @Override
48      public synchronized PlexusIoResourceAttributes getAttributes()
49      {
50          int mode = PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
51          if ( entry.getPlatform() == ZipArchiveEntry.PLATFORM_UNIX )
52          {
53              mode = entry.getUnixMode();
54              if ( ( mode & UnixStat.FILE_FLAG ) == UnixStat.FILE_FLAG )
55              {
56                  mode = mode & ~UnixStat.FILE_FLAG;
57              }
58              else
59              {
60                  mode = mode & ~UnixStat.DIR_FLAG;
61              }
62          }
63  
64          if ( attributes == null )
65          {
66              attributes = new SimpleResourceAttributes( null, null, null, null, mode );
67          }
68  
69          return attributes;
70      }
71  
72      public synchronized void setAttributes( PlexusIoResourceAttributes attributes )
73      {
74          this.attributes = attributes;
75      }
76  
77      @Override
78      public URL getURL()
79          throws IOException
80      {
81          return null;
82      }
83  
84      @Nonnull
85      @Override
86      public InputStream getContents()
87          throws IOException
88      {
89          final InputStream inputStream = zipFile.getInputStream( entry );
90          return new ClosingInputStream( streamTransformer.transform( this, inputStream ), inputStream );
91      }
92  
93  }