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.inject.Named;
19  
20  import java.io.IOException;
21  
22  import org.codehaus.plexus.archiver.AbstractArchiver;
23  import org.codehaus.plexus.archiver.ArchiveEntry;
24  import org.codehaus.plexus.archiver.ArchiverException;
25  import org.codehaus.plexus.archiver.ResourceIterator;
26  import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
27  
28  /**
29   * @author philiplourandos
30   * @since 3.3
31   */
32  @Named("xz")
33  public class XZArchiver extends AbstractArchiver {
34  
35      private final XZCompressor compressor = new XZCompressor();
36  
37      public XZArchiver() {}
38  
39      @Override
40      protected void execute() throws ArchiverException, IOException {
41          if (!checkForced()) {
42              return;
43          }
44  
45          ResourceIterator iter = getResources();
46          if (!iter.hasNext()) {
47              throw new EmptyArchiveException("archive cannot be empty");
48          }
49          ArchiveEntry entry = iter.next();
50          if (iter.hasNext()) {
51              throw new ArchiverException("There is more than one file in input.");
52          }
53          compressor.setSource(entry.getResource());
54          compressor.setDestFile(getDestFile());
55          compressor.compress();
56      }
57  
58      @Override
59      public boolean isSupportingForced() {
60          return true;
61      }
62  
63      @Override
64      protected void close() throws IOException {
65          compressor.close();
66      }
67  
68      @Override
69      protected String getArchiveType() {
70          return "xz";
71      }
72  }