1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.plexus.archiver.tar;
17
18 import javax.inject.Named;
19
20 import java.io.Closeable;
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Enumeration;
24 import java.util.Iterator;
25
26 import org.apache.commons.compress.archivers.ArchiveEntry;
27 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
28 import org.codehaus.plexus.components.io.resources.AbstractPlexusIoArchiveResourceCollection;
29 import org.codehaus.plexus.components.io.resources.PlexusIoResource;
30
31 @Named("tar")
32 public class PlexusIoTarFileResourceCollection extends AbstractPlexusIoArchiveResourceCollection implements Closeable {
33
34 protected TarFile newTarFile(File file) {
35 return new TarFile(file);
36 }
37
38 TarFile tarFile = null;
39
40 @Override
41 public void close() throws IOException {
42 if (tarFile != null) {
43 tarFile.close();
44 }
45 }
46
47 @Override
48 public boolean isConcurrentAccessSupported() {
49 return false;
50 }
51
52 @Override
53 protected Iterator<PlexusIoResource> getEntries() throws IOException {
54 final File f = getFile();
55 if (f == null) {
56 throw new IOException("The tar archive file has not been set.");
57 }
58 if (tarFile == null) {
59 tarFile = newTarFile(f);
60 }
61 final Enumeration<ArchiveEntry> en = tarFile.getEntries();
62 return new Iterator<PlexusIoResource>() {
63
64 @Override
65 public boolean hasNext() {
66 return en.hasMoreElements();
67 }
68
69 @Override
70 public PlexusIoResource next() {
71 final TarArchiveEntry entry = (TarArchiveEntry) en.nextElement();
72 return entry.isSymbolicLink()
73 ? new TarSymlinkResource(tarFile, entry)
74 : new TarResource(tarFile, entry);
75 }
76
77 @Override
78 public void remove() {
79 throw new UnsupportedOperationException("Removing isn't implemented.");
80 }
81 };
82 }
83 }