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