/
JarBuilder.java
119 lines (105 loc) · 3.95 KB
/
JarBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.apache.commons.io.IOUtils;
import org.jumpmind.symmetric.Version;
public class JarBuilder {
private File baseDir;
private File[] sourceFiles;
private File outputFile;
public JarBuilder(File baseDir, File outputFile, File[] sourceFiles) {
this.sourceFiles = sourceFiles;
this.outputFile = outputFile;
this.baseDir = baseDir;
}
public void build() throws IOException {
this.outputFile.delete();
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Version.version());
if (outputFile.getParentFile() != null) {
outputFile.getParentFile().mkdirs();
}
JarOutputStream target = new JarOutputStream(new FileOutputStream(outputFile), manifest);
for (File file : sourceFiles) {
add(file, target);
}
target.close();
}
private String massageJarEntryName(File source) {
String name = source.getPath();
if (baseDir != null && name.startsWith(baseDir.getPath())) {
if (name.length() > baseDir.getPath().length()) {
name = name.substring(baseDir.getPath().length() + 1);
} else {
name = "";
}
}
if (name.equals("META-INF/MANIFEST.MF")) {
name = "";
}
return name.replace("\\", "/");
}
private void add(File source, JarOutputStream target) throws IOException {
BufferedInputStream in = null;
try {
if (source.isDirectory()) {
String name = massageJarEntryName(source);
if (name.trim().length() != 0) {
if (!name.endsWith("/")) {
name += "/";
}
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile : source.listFiles()) {
add(nestedFile, target);
}
} else {
JarEntry entry = new JarEntry(massageJarEntryName(source));
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[1024];
while (true) {
int count = in.read(buffer);
if (count == -1) {
break;
}
target.write(buffer, 0, count);
}
target.closeEntry();
}
} finally {
IOUtils.closeQuietly(in);
}
}
}