1 | |
package com.simpligility.maven.plugins.androidndk.common; |
2 | |
|
3 | |
import org.apache.commons.io.IOUtils; |
4 | |
import org.codehaus.plexus.util.IOUtil; |
5 | |
|
6 | |
import java.io.File; |
7 | |
import java.io.FileOutputStream; |
8 | |
import java.io.IOException; |
9 | |
import java.io.InputStream; |
10 | |
import java.io.OutputStream; |
11 | |
import java.util.Enumeration; |
12 | |
import java.util.jar.JarEntry; |
13 | |
import java.util.jar.JarFile; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | 0 | public class JarHelper |
21 | |
{ |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public interface UnjarListener |
27 | |
{ |
28 | |
boolean include( JarEntry jarEntry ); |
29 | |
} |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public static void unjar( JarFile jarFile, File outputDirectory, UnjarListener unjarListener ) throws IOException |
40 | |
{ |
41 | 0 | for ( Enumeration en = jarFile.entries(); en.hasMoreElements(); ) |
42 | |
{ |
43 | 0 | JarEntry entry = ( JarEntry ) en.nextElement(); |
44 | 0 | File entryFile = new File( outputDirectory, entry.getName() ); |
45 | 0 | if ( unjarListener.include( entry ) ) |
46 | |
{ |
47 | |
|
48 | 0 | if ( ! entryFile.getParentFile().exists() ) |
49 | |
{ |
50 | 0 | if ( ! entryFile.getParentFile().mkdirs() ) |
51 | |
{ |
52 | 0 | throw new IOException( "Error creating output directory: " + entryFile.getParentFile() ); |
53 | |
} |
54 | |
} |
55 | |
|
56 | |
|
57 | 0 | if ( ! entry.isDirectory() ) |
58 | |
{ |
59 | 0 | final InputStream in = jarFile.getInputStream( entry ); |
60 | |
try |
61 | |
{ |
62 | 0 | final OutputStream out = new FileOutputStream( entryFile ); |
63 | |
try |
64 | |
{ |
65 | 0 | IOUtil.copy( in, out ); |
66 | |
} |
67 | |
finally |
68 | |
{ |
69 | 0 | IOUtils.closeQuietly( out ); |
70 | 0 | } |
71 | |
} |
72 | |
finally |
73 | |
{ |
74 | 0 | IOUtils.closeQuietly( in ); |
75 | 0 | } |
76 | |
} |
77 | |
} |
78 | 0 | } |
79 | 0 | } |
80 | |
|
81 | |
} |