View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.maven.plugins.nodejs.installation;
21  
22  import java.io.BufferedOutputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  
30  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
31  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
32  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
33  import org.apache.commons.io.IOUtils;
34  import org.apache.maven.plugin.MojoExecutionException;
35  
36  /**
37   * Wrapper around the commons compress library to decompress the zipped tar archives
38   */
39  public class TarUnArchiver {
40  
41    private final File archive;
42  
43    /**
44     * @param archive Archive
45     */
46    public TarUnArchiver(File archive) {
47      this.archive = archive;
48    }
49  
50    /**
51     * Unarchives the archive into the base dir
52     * @param baseDir Base dir
53     * @throws MojoExecutionException Mojo execution exception
54     */
55    public void unarchive(String baseDir) throws MojoExecutionException {
56      try (FileInputStream fis = new FileInputStream(archive);
57          TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis))) {
58        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
59        while (tarEntry != null) {
60          // Create a file for this tarEntry
61          final File destPath = new File(baseDir + File.separator + tarEntry.getName());
62          if (tarEntry.isSymbolicLink()) {
63            Path linkPath = destPath.toPath();
64            Path targetPath = new File(tarEntry.getLinkName()).toPath();
65            Files.createSymbolicLink(linkPath, targetPath);
66          }
67          else if (tarEntry.isDirectory()) {
68            destPath.mkdirs();
69          }
70          else {
71            destPath.createNewFile();
72            destPath.setExecutable(true);
73            try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
74              IOUtils.copy(tarIn, bout);
75            }
76          }
77          tarEntry = tarIn.getNextTarEntry();
78        }
79      }
80      catch (IOException ex) {
81        throw new MojoExecutionException("Could not extract archive: " + archive.getAbsolutePath(), ex);
82      }
83  
84      // delete archive after extraction
85      archive.delete();
86    }
87  
88  }