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