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