1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
28 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
29 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
30 import org.apache.commons.io.IOUtils;
31 import org.apache.maven.plugin.MojoExecutionException;
32
33
34
35
36 public class ZipUnArchiver {
37
38 private final File archive;
39
40
41
42
43 public ZipUnArchiver(File archive) {
44 this.archive = archive;
45 }
46
47
48
49
50
51
52 public void unarchive(String baseDir) throws MojoExecutionException {
53 try (FileInputStream fis = new FileInputStream(archive);
54 ZipArchiveInputStream zipIn = new ZipArchiveInputStream(fis)) {
55 ZipArchiveEntry zipEnry = zipIn.getNextZipEntry();
56 while (zipEnry != null) {
57
58 final File destPath = new File(baseDir + File.separator + zipEnry.getName());
59 if (zipEnry.isDirectory()) {
60 destPath.mkdirs();
61 }
62 else {
63 destPath.createNewFile();
64 try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
65 IOUtils.copy(zipIn, bout);
66 }
67 }
68 zipEnry = zipIn.getNextZipEntry();
69 }
70 }
71 catch (IOException ex) {
72 throw new MojoExecutionException("Could not extract archive: " + archive.getAbsolutePath(), ex);
73 }
74
75
76 archive.delete();
77 }
78
79 }