1 | package org.greenstone.gsdl3.gs3build.expander;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.net.URL;
|
---|
5 | import java.util.*;
|
---|
6 |
|
---|
7 | import org.greenstone.gsdl3.gs3build.BuildManager;
|
---|
8 |
|
---|
9 | public class ZipExpander implements ExpanderInterface
|
---|
10 | { BuildManager buildManager;
|
---|
11 |
|
---|
12 | public ZipExpander()
|
---|
13 | {
|
---|
14 | }
|
---|
15 |
|
---|
16 | public void configure(BuildManager manager)
|
---|
17 | { this.buildManager = manager;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public boolean expandFile(URL url, java.io.File expansionDirectory)
|
---|
21 | { if (url.toString().startsWith("file://"))
|
---|
22 | { String fileName = url.toString().substring(7);
|
---|
23 |
|
---|
24 | if (fileName.endsWith(".zip"))
|
---|
25 | { System.out.println("Expanding Zip file " + fileName);
|
---|
26 |
|
---|
27 | // determine which directory to use
|
---|
28 |
|
---|
29 | // get the leaf name
|
---|
30 | String leafName;
|
---|
31 | int leafAt = fileName.lastIndexOf(System.getProperty("file.separator"));
|
---|
32 |
|
---|
33 | if (leafAt >= 0)
|
---|
34 | { leafName = fileName.substring(leafAt+1);
|
---|
35 | }
|
---|
36 | else
|
---|
37 | { leafName = fileName;
|
---|
38 | }
|
---|
39 |
|
---|
40 | int extensionAt = leafName.lastIndexOf('.');
|
---|
41 | if (extensionAt >= 0)
|
---|
42 | { leafName = leafName.substring(0, extensionAt);
|
---|
43 | }
|
---|
44 |
|
---|
45 | File targetDir = new File(expansionDirectory, leafName);
|
---|
46 | if (targetDir.exists())
|
---|
47 | { // find alternative name
|
---|
48 | int extension = 1;
|
---|
49 | File expandedTarget;
|
---|
50 |
|
---|
51 | do
|
---|
52 | { expandedTarget = new File(targetDir.toString() + Integer.toString(extension));
|
---|
53 | } while (expandedTarget.exists());
|
---|
54 |
|
---|
55 | targetDir = expandedTarget;
|
---|
56 | }
|
---|
57 | if (!targetDir.mkdir())
|
---|
58 | { // TODO: pass/log error - unable to make directory to
|
---|
59 | // expand file.
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // Call zip expansion
|
---|
64 | try
|
---|
65 | {
|
---|
66 | Runtime.getRuntime().exec("unzip -d "+targetDir.toString());
|
---|
67 | }
|
---|
68 | catch (IOException io)
|
---|
69 | { // TODO: handle errors - report to log, etc.
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | // ensure that the build manager knows about the new directory,
|
---|
74 | // and will expand any content in it, etc. in turn
|
---|
75 | this.buildManager.addInputDirectory(targetDir.toString());
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 | } |
---|