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.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.zip.GZIPInputStream;
24  import org.codehaus.plexus.archiver.AbstractUnArchiver;
25  import org.codehaus.plexus.archiver.ArchiverException;
26  import org.codehaus.plexus.archiver.util.Streams;
27  import static org.codehaus.plexus.archiver.util.Streams.copyFully;
28  import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
29  import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
30  
31  /**
32   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
33   */
34  public class GZipUnArchiver
35      extends AbstractUnArchiver
36  {
37  
38      private static final String OPERATION_GZIP = "gzip";
39  
40      public GZipUnArchiver()
41      {
42      }
43  
44      public GZipUnArchiver( File sourceFile )
45      {
46          super( sourceFile );
47      }
48  
49      @Override
50      protected void execute()
51          throws ArchiverException
52      {
53          if ( getSourceFile().lastModified() > getDestFile().lastModified() )
54          {
55              getLogger().info( "Expanding " + getSourceFile().getAbsolutePath() + " to "
56                                    + getDestFile().getAbsolutePath() );
57  
58              copyFully( getGzipInputStream( fileInputStream( getSourceFile(), OPERATION_GZIP ) ),
59                         fileOutputStream( getDestFile(), OPERATION_GZIP ), OPERATION_GZIP );
60          }
61      }
62  
63      private InputStream getGzipInputStream( FileInputStream in )
64          throws ArchiverException
65      {
66          try
67          {
68              return Streams.bufferedInputStream( new GZIPInputStream( in ) );
69          }
70          catch ( IOException e )
71          {
72              throw new ArchiverException( "Problem creating GZIP input stream", e );
73          }
74      }
75  
76      @Override
77      protected void execute( String path, File outputDirectory )
78      {
79          throw new UnsupportedOperationException( "Targeted extraction not supported in GZIP format." );
80      }
81  
82  }