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 java.io.BufferedInputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.zip.GZIPInputStream;
25  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
26  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
27  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
28  import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
29  import org.codehaus.plexus.archiver.AbstractUnArchiver;
30  import org.codehaus.plexus.archiver.ArchiverException;
31  import org.codehaus.plexus.archiver.util.Streams;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.iq80.snappy.SnappyInputStream;
34  
35  /**
36   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
37   */
38  public class TarUnArchiver
39      extends AbstractUnArchiver
40  {
41  
42      public TarUnArchiver()
43      {
44      }
45  
46      public TarUnArchiver( File sourceFile )
47      {
48          super( sourceFile );
49      }
50  
51      /**
52       * compression method
53       */
54      private UntarCompressionMethod compression = UntarCompressionMethod.NONE;
55  
56      /**
57       * Set decompression algorithm to use; default=none.
58       * <p/>
59       * Allowable values are
60       * <ul>
61       * <li>none - no compression</li>
62       * <li>gzip - Gzip compression</li>
63       * <li>bzip2 - Bzip2 compression</li>
64       * <li>snappy - Snappy compression</li>
65       * <li>xz - Xz compression</li>
66       * </ul>
67       *
68       * @param method compression method
69       */
70      public void setCompression( UntarCompressionMethod method )
71      {
72          compression = method;
73      }
74  
75      /**
76       * No encoding support in Untar.
77       */
78      public void setEncoding( String encoding )
79      {
80          getLogger().warn( "The TarUnArchiver doesn't support the encoding attribute" );
81      }
82  
83      @Override
84      protected void execute()
85          throws ArchiverException
86      {
87          execute( getSourceFile(), getDestDirectory() );
88      }
89  
90      @Override
91      protected void execute( String path, File outputDirectory )
92      {
93          execute( new File( path ), getDestDirectory() );
94      }
95  
96      protected void execute( File sourceFile, File destDirectory )
97          throws ArchiverException
98      {
99          TarArchiveInputStream tis = null;
100         try
101         {
102             getLogger().info( "Expanding: " + sourceFile + " into " + destDirectory );
103             TarFile tarFile = new TarFile( sourceFile );
104             tis = new TarArchiveInputStream(
105                 decompress( compression, sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) );
106             TarArchiveEntry te;
107             while ( ( te = tis.getNextTarEntry() ) != null )
108             {
109                 TarResource fileInfo = new TarResource( tarFile, te );
110                 if ( isSelected( te.getName(), fileInfo ) )
111                 {
112                     final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
113                     extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
114                                  te.getMode() != 0 ? te.getMode() : null, symlinkDestination );
115 
116                 }
117             }
118             getLogger().debug( "expand complete" );
119             tis.close();
120             tis = null;
121         }
122         catch ( IOException ioe )
123         {
124             throw new ArchiverException( "Error while expanding " + sourceFile.getAbsolutePath(), ioe );
125         }
126         finally
127         {
128             IOUtil.close( tis );
129         }
130     }
131 
132     /**
133      * This method wraps the input stream with the
134      * corresponding decompression method
135      *
136      * @param file provides location information for BuildException
137      * @param istream input stream
138      *
139      * @return input stream with on-the-fly decompression
140      *
141      * @throws IOException thrown by GZIPInputStream constructor
142      */
143     private InputStream decompress( UntarCompressionMethod compression, final File file, final InputStream istream )
144         throws IOException, ArchiverException
145     {
146         if ( compression == UntarCompressionMethod.GZIP )
147         {
148             return Streams.bufferedInputStream( new GZIPInputStream( istream ) );
149         }
150         else if ( compression == UntarCompressionMethod.BZIP2 )
151         {
152             return new BZip2CompressorInputStream( istream );
153         }
154         else if ( compression == UntarCompressionMethod.SNAPPY )
155         {
156             return new SnappyInputStream( istream, true );
157         }
158         else if ( compression == UntarCompressionMethod.XZ )
159         {
160             return new XZCompressorInputStream( istream );
161         }
162         return istream;
163     }
164 
165     /**
166      * Valid Modes for Compression attribute to Untar Task
167      */
168     public static enum UntarCompressionMethod
169     {
170 
171         NONE( "none" ),
172         GZIP( "gzip" ),
173         BZIP2( "bzip2" ),
174         SNAPPY( "snappy" ),
175         XZ( "xz" );
176 
177         final String value;
178 
179         /**
180          * Constructor
181          */
182         UntarCompressionMethod( String value )
183         {
184             this.value = value;
185         }
186 
187     }
188 
189 }