1
2
3
4
5 package org.apache.maven.it.util;
6
7 import java.io.File;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.net.URI;
11 import java.net.URISyntaxException;
12 import java.net.URL;
13 import java.util.Enumeration;
14 import java.util.zip.ZipEntry;
15 import java.util.zip.ZipFile;
16
17
18
19 public class ResourceExtractor {
20
21 public static File simpleExtractResources(Class cl, String resourcePath) throws IOException {
22 String tempDirPath = System.getProperty( "maven.test.tmpdir", System.getProperty( "java.io.tmpdir" ) );
23 File tempDir = new File(tempDirPath);
24
25 File testDir = new File( tempDir, resourcePath );
26
27 FileUtils.deleteDirectory( testDir );
28
29 testDir = ResourceExtractor.extractResourcePath(cl, resourcePath, tempDir, false);
30 return testDir;
31 }
32
33 public static File extractResourcePath(String resourcePath, File dest) throws IOException {
34 return extractResourcePath(ResourceExtractor.class, resourcePath, dest);
35 }
36
37 public static File extractResourcePath(Class cl, String resourcePath, File dest) throws IOException {
38 return extractResourcePath(cl, resourcePath, dest, false);
39 }
40
41 public static File extractResourcePath(Class cl, String resourcePath, File tempDir, boolean alwaysExtract)
42 throws IOException {
43 File dest = new File(tempDir, resourcePath);
44 URL url = cl.getResource(resourcePath);
45 if (url == null) throw new IllegalArgumentException("Resource not found: " + resourcePath);
46 if ("jar".equalsIgnoreCase(url.getProtocol())) {
47 File jarFile = getJarFileFromUrl(url);
48 extractResourcePathFromJar(cl, jarFile, resourcePath, dest);
49 } else {
50 try {
51 File resourceFile = new File(new URI(url.toExternalForm()));
52 if (!alwaysExtract) return resourceFile;
53 if (resourceFile.isDirectory()) {
54 FileUtils.copyDirectoryStructure(resourceFile, dest);
55 } else {
56 FileUtils.copyFile(resourceFile, dest);
57 }
58 } catch (URISyntaxException e) {
59 throw new RuntimeException("Couldn't convert URL to File:" + url, e);
60 }
61 }
62 return dest;
63 }
64
65 private static void extractResourcePathFromJar(Class cl, File jarFile, String resourcePath, File dest) throws IOException {
66 ZipFile z = new ZipFile(jarFile, ZipFile.OPEN_READ);
67 String zipStyleResourcePath = resourcePath.substring(1) + "/";
68 ZipEntry ze = z.getEntry(zipStyleResourcePath);
69 if (ze != null) {
70
71 for (Enumeration entries = z.entries(); entries.hasMoreElements();) {
72 ze = (ZipEntry) entries.nextElement();
73 if (ze.getName().startsWith(zipStyleResourcePath)) {
74 String relativePath = ze.getName().substring(zipStyleResourcePath.length());
75 File destFile = new File(dest, relativePath);
76 if (ze.isDirectory()) {
77 destFile.mkdirs();
78 } else {
79 FileOutputStream fos = new FileOutputStream(destFile);
80 try {
81 IOUtil.copy(z.getInputStream(ze), fos);
82 } finally {
83 IOUtil.close(fos);
84 }
85 }
86 }
87 }
88 } else {
89 FileOutputStream fos = new FileOutputStream(dest);
90 try {
91 IOUtil.copy(cl.getResourceAsStream(resourcePath), fos);
92 } finally {
93 IOUtil.close(fos);
94 }
95 }
96 }
97
98 private static File getJarFileFromUrl(URL url) {
99 if (!"jar".equalsIgnoreCase(url.getProtocol()))
100 throw new IllegalArgumentException("This is not a Jar URL:"
101 + url.toString());
102 String resourceFilePath = url.getFile();
103 int index = resourceFilePath.indexOf("!");
104 if (index == -1) {
105 throw new RuntimeException("Bug! " + url.toExternalForm()
106 + " does not have a '!'");
107 }
108 String jarFileURI = resourceFilePath.substring(0, index);
109 try {
110 File jarFile = new File(new URI(jarFileURI));
111 return jarFile;
112 } catch (URISyntaxException e) {
113 throw new RuntimeException("Bug! URI failed to parse: " + jarFileURI, e);
114 }
115
116 }
117 }