View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 org.apache.commons.compress.archivers.tar.TarArchiveEntry;
27  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
28  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
29  import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
30  import org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream;
31  import org.codehaus.plexus.archiver.AbstractUnArchiver;
32  import org.codehaus.plexus.archiver.ArchiverException;
33  import org.codehaus.plexus.archiver.util.Streams;
34  import org.codehaus.plexus.components.io.filemappers.FileMapper;
35  import org.iq80.snappy.SnappyFramedInputStream;
36  
37  import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
38  import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
39  
40  /**
41   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
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       * compression method
54       */
55      private UntarCompressionMethod compression = UntarCompressionMethod.NONE;
56  
57      /**
58       * Set decompression algorithm to use; default=none.
59       * <p>
60       * Allowable values are </p>
61       * <ul>
62       * <li>none - no compression</li>
63       * <li>gzip - Gzip compression</li>
64       * <li>bzip2 - Bzip2 compression</li>
65       * <li>snappy - Snappy compression</li>
66       * <li>xz - Xz compression</li>
67       * </ul>
68       *
69       * @param method compression method
70       */
71      public void setCompression(UntarCompressionMethod method) {
72          compression = method;
73      }
74  
75      /**
76       * No encoding support in Untar.
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      * This method wraps the input stream with the
124      * corresponding decompression method
125      *
126      * @param file provides location information for BuildException
127      * @param istream input stream
128      *
129      * @return input stream with on-the-fly decompression
130      *
131      * @throws IOException thrown by GZIPInputStream constructor
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      * Valid Modes for Compression attribute to Untar Task
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          * Constructor
164          */
165         UntarCompressionMethod(String value) {
166             this.value = value;
167         }
168     }
169 }