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