View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.codehaus.plexus.archiver.bzip2;
18  
19  import javax.inject.Named;
20  
21  import java.io.IOException;
22  
23  import org.codehaus.plexus.archiver.AbstractArchiver;
24  import org.codehaus.plexus.archiver.ArchiveEntry;
25  import org.codehaus.plexus.archiver.ArchiverException;
26  import org.codehaus.plexus.archiver.ResourceIterator;
27  import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
28  
29  @Named("bzip2")
30  public class BZip2Archiver extends AbstractArchiver {
31  
32      private final BZip2Compressor compressor = new BZip2Compressor();
33  
34      @Override
35      public void execute() throws ArchiverException, IOException {
36          if (!checkForced()) {
37              return;
38          }
39  
40          ResourceIterator iter = getResources();
41          if (!iter.hasNext()) {
42              throw new EmptyArchiveException("archive cannot be empty");
43          }
44          ArchiveEntry entry = iter.next();
45          if (iter.hasNext()) {
46              throw new ArchiverException("There is more than one file in input.");
47          }
48          compressor.setSource(entry.getResource());
49          compressor.setDestFile(getDestFile());
50          compressor.compress();
51      }
52  
53      @Override
54      public boolean isSupportingForced() {
55          return true;
56      }
57  
58      @Override
59      protected void close() {
60          compressor.close();
61      }
62  
63      @Override
64      protected String getArchiveType() {
65          return "bzip2";
66      }
67  }