Coverage Report - org.codehaus.plexus.archiver.zip.ZipResource
 
Classes in this File Line Coverage Branch Coverage Complexity
ZipResource
85%
18/21
91%
11/12
1,833
 
 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  322
         super( entry.getName(), getLastModofied( entry ),
 33  
                entry.isDirectory() ? PlexusIoResource.UNKNOWN_RESOURCE_SIZE : entry.getSize(), !entry.isDirectory(),
 34  
                entry.isDirectory(), true );
 35  
 
 36  322
         this.zipFile = zipFile;
 37  322
         this.entry = entry;
 38  322
         this.streamTransformer = streamTransformer;
 39  322
     }
 40  
 
 41  
     private static long getLastModofied( ZipArchiveEntry entry )
 42  
     {
 43  322
         long l = entry.getLastModifiedDate().getTime();
 44  322
         return l == -1 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l;
 45  
     }
 46  
 
 47  
     @Override
 48  
     public synchronized PlexusIoResourceAttributes getAttributes()
 49  
     {
 50  379
         int mode = PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
 51  379
         if ( entry.getPlatform() == ZipArchiveEntry.PLATFORM_UNIX )
 52  
         {
 53  378
             mode = entry.getUnixMode();
 54  378
             if ( ( mode & UnixStat.FILE_FLAG ) == UnixStat.FILE_FLAG )
 55  
             {
 56  284
                 mode = mode & ~UnixStat.FILE_FLAG;
 57  
             }
 58  
             else
 59  
             {
 60  94
                 mode = mode & ~UnixStat.DIR_FLAG;
 61  
             }
 62  
         }
 63  
 
 64  379
         if ( attributes == null )
 65  
         {
 66  318
             attributes = new SimpleResourceAttributes( null, null, null, null, mode );
 67  
         }
 68  
 
 69  379
         return attributes;
 70  
     }
 71  
 
 72  
     public synchronized void setAttributes( PlexusIoResourceAttributes attributes )
 73  
     {
 74  0
         this.attributes = attributes;
 75  0
     }
 76  
 
 77  
     @Override
 78  
     public URL getURL()
 79  
         throws IOException
 80  
     {
 81  0
         return null;
 82  
     }
 83  
 
 84  
     @Nonnull
 85  
     @Override
 86  
     public InputStream getContents()
 87  
         throws IOException
 88  
     {
 89  226
         final InputStream inputStream = zipFile.getInputStream( entry );
 90  226
         return new ClosingInputStream( streamTransformer.transform( this, inputStream ), inputStream );
 91  
     }
 92  
 
 93  
 }