View Javadoc
1   package org.codehaus.plexus.archiver.tar;
2   
3   /*
4    * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés.
5    * Author(s) : Franck Bonin, Neticoa SAS France
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   *
19   */
20  
21  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
22  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
23  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
24  import org.codehaus.plexus.archiver.AbstractArchiveContentLister;
25  import org.codehaus.plexus.archiver.ArchiveContentEntry;
26  import org.codehaus.plexus.archiver.ArchiverException;
27  import org.codehaus.plexus.util.IOUtil;
28  // SNAPPY
29  //import org.xerial.snappy.SnappyInputStream;
30  
31  import java.io.BufferedInputStream;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.util.zip.GZIPInputStream;
37  import java.util.List;
38  import java.util.ArrayList;
39  
40  
41  /**
42   * @author Franck Bonin
43   * @version $Revision$ $Date$
44   */
45  public class TarArchiveContentLister
46      extends AbstractArchiveContentLister
47  {
48      public TarArchiveContentLister()
49      {
50      }
51  
52      public TarArchiveContentLister( File sourceFile )
53      {
54          super( sourceFile );
55      }
56  
57      /**
58       * compression method
59       */
60      private UntarCompressionMethod compression = UntarCompressionMethod.NONE;
61  
62      /**
63       * Set decompression algorithm to use; default=none.
64       * <p/>
65       * Allowable values are
66       * <ul>
67       * <li>none - no compression</li>
68       * <li>gzip - Gzip compression</li>
69       * <li>bzip2 - Bzip2 compression</li>
70       * <li>snappy - Snappy compression</li>
71       * </ul>
72       *
73       * @param method compression method
74       */
75      public void setCompression( UntarCompressionMethod method )
76      {
77          compression = method;
78      }
79  
80      /**
81       * No encoding support in Untar.
82       */
83      public void setEncoding( String encoding )
84      {
85          getLogger().warn( "The TarArchiveContentLister doesn't support the encoding attribute" );
86      }
87      
88      private static class MyTarResource
89          extends TarResource
90      {   
91          public MyTarResource( TarFile tarFile, TarArchiveEntry entry )
92          {
93              super( tarFile, entry );
94          }
95          
96          public ArchiveContentEntry asArchiveContentEntry()
97          {
98              if ( isSymbolicLink() )
99              {
100                 return ArchiveContentEntry.createSymlinkEntry( getName(), this, -1 );
101             }
102             else if ( isDirectory() )
103             {
104                 return ArchiveContentEntry.createDirectoryEntry( getName(), this, -1 );
105             }
106             else
107             {
108                 return ArchiveContentEntry.createFileEntry( getName(), this, -1 );
109             }
110         }
111     };
112       
113     protected List<ArchiveContentEntry> execute()
114         throws ArchiverException
115     {
116         ArrayList<ArchiveContentEntry> archiveContentList = new ArrayList<ArchiveContentEntry>();
117         getLogger().debug( "listing: " + getSourceFile() );
118         TarArchiveInputStream tis = null;
119         try
120         {
121             TarFile tarFile = new TarFile( getSourceFile() );
122             tis = new TarArchiveInputStream(
123                 decompress( compression, getSourceFile(),
124                 new BufferedInputStream( new FileInputStream( getSourceFile() ) ) ) );
125             TarArchiveEntry te;
126             while ( ( te = tis.getNextTarEntry() ) != null )
127             {
128                 MyTarResource fileInfo = new MyTarResource( tarFile, te );
129                 if ( isSelected( te.getName(), fileInfo ) )
130                 {
131                     ArchiveContentEntry ae = fileInfo.asArchiveContentEntry();
132                     archiveContentList.add( ae );
133                 }
134             }
135             getLogger().debug( "listing complete" );
136         }
137         catch ( final IOException ioe )
138         {
139             throw new ArchiverException( "Error while listing " + getSourceFile().getAbsolutePath(), ioe );
140         }
141         finally
142         {
143             IOUtil.close( tis );
144         }
145         return archiveContentList;
146     }
147 
148 /*
149     protected void execute()
150         throws ArchiverException
151     {
152         execute( getSourceFile(), getDestDirectory() );
153     }
154 
155     protected void execute( String path, File outputDirectory )
156     {
157         execute( new File( path ), getDestDirectory() );
158     }
159 
160     protected void execute( File sourceFile, File destDirectory )
161         throws ArchiverException
162     {
163         TarArchiveInputStream tis = null;
164         try
165         {
166             getLogger().info( "Expanding: " + sourceFile + " into " + destDirectory );
167             TarFile tarFile = new TarFile( sourceFile );
168             tis = new TarArchiveInputStream(
169                 decompress( compression, sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) );
170             TarArchiveEntry te;
171             while ( ( te = tis.getNextTarEntry() ) != null )
172             {
173                 TarResource fileInfo = new TarResource( tarFile, te );
174                 if ( isSelected( te.getName(), fileInfo ) )
175                 {
176                     final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null;
177                     extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
178                                  te.getMode() != 0 ? te.getMode() : null, symlinkDestination );
179                 }
180 
181             }
182             getLogger().debug( "expand complete" );
183 
184         }
185         catch ( IOException ioe )
186         {
187             throw new ArchiverException( "Error while expanding " + sourceFile.getAbsolutePath(), ioe );
188         }
189         finally
190         {
191             IOUtil.close( tis );
192         }
193     }
194 */
195     /**
196      * This method wraps the input stream with the
197      * corresponding decompression method
198      *
199      * @param file    provides location information for BuildException
200      * @param istream input stream
201      * @return input stream with on-the-fly decompression
202      * @throws IOException thrown by GZIPInputStream constructor
203      */
204     private InputStream decompress( UntarCompressionMethod aiCompression, final File file, final InputStream istream )
205         throws IOException, ArchiverException
206     {
207         if ( aiCompression == UntarCompressionMethod.GZIP )
208         {
209             return new GZIPInputStream( istream );
210         }
211         else if ( aiCompression == UntarCompressionMethod.BZIP2 )
212         {
213             return new BZip2CompressorInputStream( istream );
214         }
215         else if ( aiCompression == UntarCompressionMethod.SNAPPY )
216         {
217             // SNAPPY
218             //return new SnappyInputStream( istream );
219         }
220         return istream;
221     }
222 
223     /**
224        * Valid Modes for Compression attribute to Untar Task
225        */
226     public static enum UntarCompressionMethod
227     {
228         NONE( "none" ), GZIP( "gzip" ), BZIP2( "bzip2" ), SNAPPY( "snappy" );
229 
230         final String value;
231 
232         /**
233          * Constructor
234          */
235         UntarCompressionMethod( String value )
236         {
237             this.value = value;
238         }
239     }
240 }