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
120
121
122
123
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Boeing.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.framework.jdk.core.util;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Ryan D. Brooks
* @author Andrew M. Finkbeiner
*/
public class JarCreator {
private final JarOutputStream out;
private final byte[] buffer;
/**
* @throws IOException
* @throws FileNotFoundException
*/
public JarCreator(File path, String title, String version) throws FileNotFoundException, IOException {
super();
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attributes.putValue("Implementation-Title", title);
attributes.putValue("Implementation-Version", version);
out = new JarOutputStream(new FileOutputStream(path), manifest);
buffer = new byte[20480];
}
private void addFile(File file, String pathInJar) throws IOException {
out.putNextEntry(new JarEntry(pathInJar.replace('\\', '/')));
// Read the file and write it to the jar.
FileInputStream in = new FileInputStream(file);
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
}
/**
* @param path
* @param rootPathPos
* @throws IOException
*/
private void addRelativeToPosition(File path, int rootPathPos, FileFilter filenameFilter) throws IOException {
if (path.isDirectory()) {
File[] files = path.listFiles(filenameFilter);
for (int i = 0; i < files.length; i++) {
addRelativeToPosition(files[i], rootPathPos, filenameFilter);
}
} else {
addFile(path, path.getAbsolutePath().substring(rootPathPos));
}
}
/**
* just an entry point into the recursive addRelativeTo so the (unchanging) rootPathPos can be computed once
*/
private void addDirectoryContents(File directory, FileFilter filenameFilter) throws IOException {
if (directory.isDirectory()) {
int rootPathPos = directory.getPath().length() + 1;
addRelativeToPosition(directory, rootPathPos, filenameFilter);
} else {
throw new IllegalArgumentException("Must be a directory: " + directory);
}
}
public void addFileRelativeTo(File path, String relativeTo) throws IOException {
addRelativeToPosition(path, relativeTo.length() + 1, null);
}
public void addDirectoryContents(File directory) throws IOException {
addDirectoryContents(directory, (FilenameAndDirectoryFilter) null);
}
public void addDirectoryContents(File directory, String fileNamePattern) throws IOException {
addDirectoryContents(directory, new FilenameAndDirectoryFilter(fileNamePattern));
}
public void close() throws IOException {
out.close();
}
private static class FilenameAndDirectoryFilter implements FileFilter {
private final Matcher matcher;
public FilenameAndDirectoryFilter(String pattern) {
this.matcher = Pattern.compile(pattern).matcher("");
}
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
matcher.reset(pathname.getName());
return matcher.matches();
}
}
}
|