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