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