1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.plexus.archiver.tar;
18
19 import javax.inject.Named;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.zip.GZIPInputStream;
25
26 import io.airlift.compress.snappy.SnappyFramedInputStream;
27 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
28 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
29 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
30 import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
31 import org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream;
32 import org.codehaus.plexus.archiver.AbstractUnArchiver;
33 import org.codehaus.plexus.archiver.ArchiverException;
34 import org.codehaus.plexus.archiver.util.Streams;
35 import org.codehaus.plexus.components.io.filemappers.FileMapper;
36
37 import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
38 import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
39
40
41
42
43 @Named("tar")
44 public class TarUnArchiver extends AbstractUnArchiver {
45
46 public TarUnArchiver() {}
47
48 public TarUnArchiver(File sourceFile) {
49 super(sourceFile);
50 }
51
52
53
54
55 private UntarCompressionMethod compression = UntarCompressionMethod.NONE;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public void setCompression(UntarCompressionMethod method) {
72 compression = method;
73 }
74
75
76
77
78 public void setEncoding(String encoding) {
79 getLogger().warn("The TarUnArchiver doesn't support the encoding attribute");
80 }
81
82 @Override
83 protected void execute() throws ArchiverException {
84 execute(getSourceFile(), getDestDirectory(), getFileMappers());
85 }
86
87 @Override
88 protected void execute(String path, File outputDirectory) {
89 execute(new File(path), getDestDirectory(), getFileMappers());
90 }
91
92 protected void execute(File sourceFile, File destDirectory, FileMapper[] fileMappers) throws ArchiverException {
93 try {
94 getLogger().info("Expanding: " + sourceFile + " into " + destDirectory);
95 TarFile tarFile = new TarFile(sourceFile);
96 try (TarArchiveInputStream tis = new TarArchiveInputStream(
97 decompress(compression, sourceFile, bufferedInputStream(fileInputStream(sourceFile))))) {
98 TarArchiveEntry te;
99 while ((te = tis.getNextTarEntry()) != null) {
100 TarResource fileInfo = new TarResource(tarFile, te);
101 if (isSelected(te.getName(), fileInfo)) {
102 final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
103 extractFile(
104 sourceFile,
105 destDirectory,
106 tis,
107 te.getName(),
108 te.getModTime(),
109 te.isDirectory(),
110 te.getMode() != 0 ? te.getMode() : null,
111 symlinkDestination,
112 fileMappers);
113 }
114 }
115 getLogger().debug("expand complete");
116 }
117 } catch (IOException ioe) {
118 throw new ArchiverException("Error while expanding " + sourceFile.getAbsolutePath(), ioe);
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 private InputStream decompress(UntarCompressionMethod compression, final File file, final InputStream istream)
134 throws IOException, ArchiverException {
135 if (compression == UntarCompressionMethod.GZIP) {
136 return Streams.bufferedInputStream(new GZIPInputStream(istream));
137 } else if (compression == UntarCompressionMethod.BZIP2) {
138 return new BZip2CompressorInputStream(istream);
139 } else if (compression == UntarCompressionMethod.SNAPPY) {
140 return new SnappyFramedInputStream(istream, true);
141 } else if (compression == UntarCompressionMethod.XZ) {
142 return new XZCompressorInputStream(istream);
143 } else if (compression == UntarCompressionMethod.ZSTD) {
144 return new ZstdCompressorInputStream(istream);
145 }
146 return istream;
147 }
148
149
150
151
152 public enum UntarCompressionMethod {
153 NONE("none"),
154 GZIP("gzip"),
155 BZIP2("bzip2"),
156 SNAPPY("snappy"),
157 XZ("xz"),
158 ZSTD("zstd");
159
160 final String value;
161
162
163
164
165 UntarCompressionMethod(String value) {
166 this.value = value;
167 }
168 }
169 }