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 javax.annotation.Nonnull;
19  import javax.inject.Named;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
26  import org.codehaus.plexus.archiver.AbstractUnArchiver;
27  import org.codehaus.plexus.archiver.ArchiverException;
28  
29  import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
30  import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
31  import static org.codehaus.plexus.archiver.util.Streams.copyFully;
32  import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
33  import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
34  
35  /**
36   * @author philip.lourandos
37   * @since 3.3
38   */
39  @Named("xz")
40  public class XZUnArchiver extends AbstractUnArchiver {
41  
42      private static final String OPERATION_XZ = "xz";
43  
44      public XZUnArchiver() {}
45  
46      public XZUnArchiver(File source) {
47          super(source);
48      }
49  
50      @Override
51      protected void execute() throws ArchiverException {
52          if (getSourceFile().lastModified() > getDestFile().lastModified()) {
53              getLogger()
54                      .info("Expanding " + getSourceFile().getAbsolutePath() + " to "
55                              + getDestFile().getAbsolutePath());
56  
57              copyFully(
58                      getXZInputStream(bufferedInputStream(fileInputStream(getSourceFile(), OPERATION_XZ))),
59                      bufferedOutputStream(fileOutputStream(getDestFile(), OPERATION_XZ)),
60                      OPERATION_XZ);
61          }
62      }
63  
64      public static @Nonnull XZCompressorInputStream getXZInputStream(InputStream in) throws ArchiverException {
65          try {
66              return new XZCompressorInputStream(in);
67          } catch (IOException ioe) {
68              throw new ArchiverException("Trouble creating BZIP2 compressor, invalid file ?", ioe);
69          }
70      }
71  
72      @Override
73      protected void execute(String path, File outputDirectory) throws ArchiverException {
74          throw new UnsupportedOperationException("Targeted execution not supported in xz format");
75      }
76  }