View Javadoc
1   /*
2    * Copyright 2016 Codehaus.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.archiver.xz;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import javax.annotation.Nonnull;
22  import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
23  import org.codehaus.plexus.archiver.AbstractUnArchiver;
24  import org.codehaus.plexus.archiver.ArchiverException;
25  import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
26  import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
27  import static org.codehaus.plexus.archiver.util.Streams.copyFully;
28  import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
29  import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
30  
31  /**
32   * @author philip.lourandos
33   * @since 3.3
34   */
35  public class XZUnArchiver extends AbstractUnArchiver
36  {
37  
38      private static final String OPERATION_XZ = "xz";
39  
40      public XZUnArchiver()
41      {
42      }
43  
44      public XZUnArchiver( File source )
45      {
46          super( source );
47      }
48  
49      @Override
50      protected void execute() throws ArchiverException
51      {
52          if ( getSourceFile().lastModified() > getDestFile().lastModified() )
53          {
54              getLogger().info( "Expanding " + getSourceFile().getAbsolutePath() + " to "
55                                    + getDestFile().getAbsolutePath() );
56  
57              copyFully( getXZInputStream( bufferedInputStream( fileInputStream( getSourceFile(), OPERATION_XZ ) ) ),
58                         bufferedOutputStream( fileOutputStream( getDestFile(), OPERATION_XZ ) ), OPERATION_XZ );
59  
60          }
61      }
62  
63      public static @Nonnull
64      XZCompressorInputStream getXZInputStream( InputStream in )
65          throws ArchiverException
66      {
67          try
68          {
69              return new XZCompressorInputStream( in );
70          }
71          catch ( IOException ioe )
72          {
73              throw new ArchiverException( "Trouble creating BZIP2 compressor, invalid file ?", ioe );
74          }
75      }
76  
77      @Override
78      protected void execute( String path, File outputDirectory ) throws ArchiverException
79      {
80          throw new UnsupportedOperationException( "Targeted execution not supported in xz format" );
81  
82      }
83  
84  }