Coverage Report - org.codehaus.plexus.archiver.tar.TarUnArchiver
 
Classes in this File Line Coverage Branch Coverage Complexity
TarUnArchiver
75 %
34/45
83 %
15/18
2,556
TarUnArchiver$UntarCompressionMethod
81 %
9/11
N/A
2,556
 
 1  0
 /**
 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  0
     public TarUnArchiver()
 43  25
     {
 44  25
     }
 45  
 
 46  
     public TarUnArchiver( File sourceFile )
 47  
     {
 48  0
         super( sourceFile );
 49  0
     }
 50  
 
 51  
     /**
 52  
      * compression method
 53  
      */
 54  25
     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  17
         compression = method;
 73  17
     }
 74  
 
 75  
     /**
 76  
      * No encoding support in Untar.
 77  
      */
 78  
     public void setEncoding( String encoding )
 79  
     {
 80  0
         getLogger().warn( "The TarUnArchiver doesn't support the encoding attribute" );
 81  0
     }
 82  
 
 83  
     @Override
 84  
     protected void execute()
 85  
         throws ArchiverException
 86  
     {
 87  11
         execute( getSourceFile(), getDestDirectory() );
 88  11
     }
 89  
 
 90  
     @Override
 91  
     protected void execute( String path, File outputDirectory )
 92  
     {
 93  5
         execute( new File( path ), getDestDirectory() );
 94  5
     }
 95  
 
 96  
     protected void execute( File sourceFile, File destDirectory )
 97  
         throws ArchiverException
 98  
     {
 99  16
         TarArchiveInputStream tis = null;
 100  
         try
 101  
         {
 102  16
             getLogger().info( "Expanding: " + sourceFile + " into " + destDirectory );
 103  16
             TarFile tarFile = new TarFile( sourceFile );
 104  16
             tis = new TarArchiveInputStream(
 105  0
                 decompress( compression, sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) );
 106  
             TarArchiveEntry te;
 107  80
             while ( ( te = tis.getNextTarEntry() ) != null )
 108  
             {
 109  64
                 TarResource fileInfo = new TarResource( tarFile, te );
 110  64
                 if ( isSelected( te.getName(), fileInfo ) )
 111  
                 {
 112  51
                     final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
 113  51
                     extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
 114  0
                                  te.getMode() != 0 ? te.getMode() : null, symlinkDestination );
 115  
 
 116  
                 }
 117  64
             }
 118  16
             getLogger().debug( "expand complete" );
 119  16
             tis.close();
 120  16
             tis = null;
 121  0
         }
 122  0
         catch ( IOException ioe )
 123  
         {
 124  0
             throw new ArchiverException( "Error while expanding " + sourceFile.getAbsolutePath(), ioe );
 125  
         }
 126  
         finally
 127  0
         {
 128  16
             IOUtil.close( tis );
 129  16
         }
 130  16
     }
 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  16
         if ( compression == UntarCompressionMethod.GZIP )
 147  
         {
 148  6
             return Streams.bufferedInputStream( new GZIPInputStream( istream ) );
 149  
         }
 150  10
         else if ( compression == UntarCompressionMethod.BZIP2 )
 151  
         {
 152  1
             return new BZip2CompressorInputStream( istream );
 153  
         }
 154  9
         else if ( compression == UntarCompressionMethod.SNAPPY )
 155  
         {
 156  1
             return new SnappyInputStream( istream, true );
 157  
         }
 158  8
         else if ( compression == UntarCompressionMethod.XZ )
 159  
         {
 160  1
             return new XZCompressorInputStream( istream );
 161  
         }
 162  7
         return istream;
 163  
     }
 164  
 
 165  
     /**
 166  
      * Valid Modes for Compression attribute to Untar Task
 167  
      */
 168  1
     public static enum UntarCompressionMethod
 169  
     {
 170  
 
 171  1
         NONE( "none" ),
 172  1
         GZIP( "gzip" ),
 173  1
         BZIP2( "bzip2" ),
 174  1
         SNAPPY( "snappy" ),
 175  1
         XZ( "xz" );
 176  
 
 177  
         final String value;
 178  
 
 179  
         /**
 180  
          * Constructor
 181  
          */
 182  0
         UntarCompressionMethod( String value )
 183  5
         {
 184  5
             this.value = value;
 185  5
         }
 186  
 
 187  
     }
 188  
 
 189  
 }