下面列出了java.util.jar.JarFile#entries ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Add classes to the map file.
*
* @param writer XML stream writer
* @param jarFile jar file
* @param mapClassMethods true if we want to produce .Net style class method names
* @throws IOException
* @throws ClassNotFoundException
* @throws XMLStreamException
* @throws IntrospectionException
*/
private void addClasses(XMLStreamWriter writer, File jarFile, boolean mapClassMethods) throws IOException, ClassNotFoundException, XMLStreamException, IntrospectionException
{
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
URLClassLoader loader = new URLClassLoader(new URL[]
{
jarFile.toURI().toURL()
}, currentThreadClassLoader);
JarFile jar = new JarFile(jarFile);
Enumeration<JarEntry> enumeration = jar.entries();
while (enumeration.hasMoreElements())
{
JarEntry jarEntry = enumeration.nextElement();
if (!jarEntry.isDirectory() && jarEntry.getName().endsWith(".class"))
{
addClass(loader, jarEntry, writer, mapClassMethods);
}
}
jar.close();
}
private static ClassInfo generateClassInfo(URL url, Class clazz) throws IOException {
ClassInfo classInfo = new ClassInfo();
classInfo.setClassPath(url.toString());
if (isJarFile(url)) {
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
final String path;
if (codeSource == null || Strings.isNullOrEmpty(path = codeSource.getLocation().getPath())) {
classInfo.setMaven(false);
return classInfo;
}
JarFile jarFile = new JarFile(path);
classInfo.setJarName(jarFile.getName());
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
if (jarEntry.getName().endsWith("/pom.properties")) {
Properties properties = readJarFile(path, jarEntry.getName());
classInfo.setMavenInfo(properties);
classInfo.setMaven(true);
break;
}
}
}
return classInfo;
}
private void findClasses(List<Class<?>> classList, String packageName, URL url, boolean recursive) throws Throwable {
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
JarFile jarFile = jarURLConnection.getJarFile();
Enumeration<JarEntry> jarEntries = jarFile.entries();
// jarCount.incrementAndGet();
// LOGGER.info("Scanning jar: " + jarFile.getName());
while (jarEntries.hasMoreElements()) {
try {
JarEntry jarEntry = jarEntries.nextElement();
String jarEntryName = jarEntry.getName();
String jarEntryClassName = getClassName(jarEntryName);
if (jarEntryClassName != null) {
String jarEntryPackageName = getPackageName(jarEntryClassName);
if (jarEntryPackageName.equals(packageName) ||
(recursive && jarEntryPackageName.startsWith(packageName))) {
tryAddClass(classList, jarEntryClassName);
}
}
} catch (Throwable t) {
LOGGER.warn(t.getMessage(), t);
}
}
}
private void findResourcesFromJar(URL jarPath) {
try {
JarURLConnection jarConnection = (JarURLConnection) jarPath.openConnection();
JarFile jarFile = jarConnection.getJarFile();
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String resourcePath = entry.getName();
if ((!entry.isDirectory())
&& (resourcePath.startsWith(directoryPath))
&& (searchSubdirectories || !resourcePath.substring(directoryPath.length()).contains("/"))
&& (filtersApply(resourcePath))) {
// cannot do new URL(jarPath, resourcePath), somehow leads to duplicated path
// retest on java 8
Enumeration<URL> urls = classLoader.getResources(resourcePath);
while(urls.hasMoreElements()) {
URL url = urls.nextElement();
if (url.toExternalForm().startsWith(jarPath.toExternalForm())) {
resources.add(url);
}
}
}
}
} catch (IOException e) { }
}
public ClassJar(File file) {
try {
_jar = new JarFile(file);
Enumeration<JarEntry> entries = _jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.toLowerCase().endsWith(".class")) {
name = name.substring(0, name.length() - ".class".length()).replace('/', '.');
_classes.add(name);
_entries.put(name, entry);
}
}
} catch (Exception e) {
throw new RuntimeException("Failed to open jar file", e);
}
}
/**
* 按照Jar扫描类
*
* @param packageName
* @param jar
* @return
*/
public static Collection<Class<?>> scanClassByJar(final String packageName, final JarFile jar) {
final Enumeration<JarEntry> jarEntries = jar.entries();
final Pattern pattern = Pattern.compile("(" + packageName.replace('.', '/') + ".*)\\.class");
final Collection<Class<?>> clazzCollection = new HashSet<Class<?>>();
while (jarEntries.hasMoreElements()) {
final JarEntry entry = jarEntries.nextElement();
final String name = entry.getName();
final Matcher matcher = pattern.matcher(name.replace(File.separatorChar, '/'));
if (matcher.find()) {
final String className = matcher.group(1).replace('/', '.');
// 处理匿名类
if (className.contains("$")) {
continue;
}
try {
// Class.forName会触发static代码块
final Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
clazzCollection.add(clazz);
} catch (ClassNotFoundException exception) {
LOGGER.error("无法加载类[{}]", className, exception);
}
}
}
return clazzCollection;
}
/**
* Unzips a JAR file given the absolute path to the JAR and the directory
* where content should be stracted.
*
* @param pathJar: absolute path to JAR file
* @param pathDir: absolute path to directory where JAR content should be
* extracted
* @return true if the JAR file was successfully unzziped; false otherwise.
*/
public boolean unzipJar(String pathJar, String pathDir) {
try {
File file = new File(File.separator + pathJar);
JarFile fileJar = new JarFile(file);
Enumeration<JarEntry> entries = fileJar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
File fileEntry = new File(File.separator + pathDir + File.separator + entry.getName());
if (entry.isDirectory()) {
fileEntry.mkdirs();
}
else {
InputStream is = fileJar.getInputStream(entry);
FileOutputStream os = new FileOutputStream(fileEntry);
while (is.available() > 0) {
os.write(is.read());
}
os.close();
is.close();
}
}
fileJar.close();
return true;
}
catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static ClassFileTree fromJar(JarFile jarFile) {
Map<String, Package> rootPackages = new HashMap<>();
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry entry = jarEntries.nextElement();
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
Deque<String> parts = new ArrayDeque<>(Arrays.asList(entry.getName().split("/")));
String className = parts.removeLast();
Package pkg = rootPackages
.computeIfAbsent(parts.removeFirst(), name -> new Package(null, name));
for (String part : parts) {
pkg = pkg.getOrCreateSubPackage(part);
}
pkg.addClass(new ClassFile(pkg, className) {
@Override
public InputStream getInputStream() throws IOException {
return jarFile.getInputStream(entry);
}
});
}
}
return new ClassFileTree() {
@Override
public Collection<Package> getRootPackages() {
return rootPackages.values();
}
@Override
public void close() throws IOException {
jarFile.close();
}
};
}
public static boolean copyJarResourcesRecursively(final File destDir,
final JarURLConnection jarConnection) throws IOException {
final JarFile jarFile = jarConnection.getJarFile();
for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); ) {
final JarEntry entry = e.nextElement();
if (entry.getName().startsWith(jarConnection.getEntryName())) {
final String filename = StringUtils.removeStart(entry.getName(), //
jarConnection.getEntryName());
final File f = new File(destDir, filename);
if (!entry.isDirectory()) {
final InputStream entryInputStream = jarFile.getInputStream(entry);
if (!copyStream(entryInputStream, f)) {
return false;
}
entryInputStream.close();
} else {
if (!ensureDirectoryExists(f)) {
throw new IOException("Could not create directory: "
+ f.getAbsolutePath());
}
}
}
}
return true;
}
/**
* Extract source from inside the bundle to the given destination directory.
* If bundle does not contain any source files try to copy source files from
* the bundles src-directory (derived from the <code>Built-From</code>
* manifest attribute.
*
* @param destDir
* The directory to extract the source files to.
* @return List with one entry for each extracted file. The value is the
* path relative to <code>destDir</code>.
* @throws IOException
*/
public List<String> extractSources(File destDir) throws IOException {
final List<String> res = new ArrayList<String>();
final JarFile jarFile = new JarFile(file);
for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
final ZipEntry entry = e.nextElement();
if (entry.getName().startsWith(SRC_PREFIX)) {
if (0 == res.size()) {
destDir.mkdirs();
}
if (entry.isDirectory()) {
makeDir(destDir, entry, SRC_PREFIX);
} else {
copyEntry(destDir, jarFile, entry, SRC_PREFIX);
res.add(Util.replace(entry.getName(), SRC_PREFIX + "/", ""));
}
}
}
if (0 == res.size()) {
// Check if we can copy source from original pos
final String sourceDir = mainAttributes.getValue("Built-From");
if (sourceDir != null && !"".equals(sourceDir)) {
final File src = new File(sourceDir, "src");
res.addAll(copyTree(src, destDir, src.toString() + File.separator, ".java"));
}
}
return res;
}
/**
* 从jar包读取class
*/
private static void readClassFromJar(URL url, String packageName, List<Class<?>> classes) throws IOException, ClassNotFoundException {
JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
// 从此jar包 得到一个枚举类
Enumeration<JarEntry> entries = jar.entries();
// 同样的进行循环迭代
while (entries.hasMoreElements()) {
// 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
JarEntry entry = entries.nextElement();
String name = entry.getName();
// 如果是以/开头的
if (name.charAt(0) == '/') {
// 获取后面的字符串
name = name.substring(1);
}
if (name.endsWith(".class") && !entry.isDirectory()) {
name = name.replace("/", ".").substring(0, name.length() - 6);
if (name.startsWith(packageName)) {
Class<?> clz = Class.forName(name);
if (!classes.contains(clz)) {
classes.add(clz);
}
}
}
}
}
private static List<CtClass> findBoosterCtClassesInJar(File jarFile, ClassPool classPool)
throws ClassNotFoundException, IOException {
List<CtClass> ctClasses = new ArrayList<CtClass>();
JarFile jar = new JarFile(jarFile);
// Getting the files into the jar
Enumeration<? extends JarEntry> enumeration = jar.entries();
// Iterates into the files in the jar file
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = enumeration.nextElement();
// Is this a class?
if (zipEntry.getName().endsWith(".class")) {
// Relative path of file into the jar.
String className = zipEntry.getName();
// Complete class name
className = className.replace(".class", "").replace("/", ".");
try {
CtClass ctClass = classPool.get(className);
// For now, assume that a runtime booster is going to directly extend a common
// booster, which will extend AbstractBoosterConfig
if (ctClass.getSuperclass().getSuperclass().getName()
.contains(AbstractBoosterConfig.class.getName())) {
ctClasses.add(ctClass);
}
} catch (Exception e) {
}
}
}
jar.close();
return ctClasses;
}
/**
* 读取jar包
* 获得jar包中的pom文件的字节流数组
*/
public static byte[] JarRead(String filepath) throws IOException{
//创造一个input实例,内容不重要
InputStream input = new InputStream() {
@Override
public int read() {
return -1; // end of stream
}
};
JarFile jarFile = new JarFile(filepath);
Enumeration enu = jarFile.entries();
while(enu.hasMoreElements()){
String pompath = "";
JarEntry element = (JarEntry) enu.nextElement();
//获取Jar包中的条目名字
String name = element.getName();
Pattern r = Pattern.compile("(pom.xml)$");
Matcher m = r.matcher(name);
if(m.find()){
pompath = name;
JarEntry entry = jarFile.getJarEntry(pompath);
//获取该文件的输入流
input = jarFile.getInputStream(entry);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}
return new byte[0];
}
public boolean bootstrapJarShadesJBossModules(File artifactFile) throws IOException {
JarFile jarFile = new JarFile(artifactFile);
Enumeration<JarEntry> entries = jarFile.entries();
boolean jbossModulesFound = false;
while (entries.hasMoreElements()) {
JarEntry each = entries.nextElement();
if (each.getName().startsWith("org/jboss/modules/ModuleLoader")) {
jbossModulesFound = true;
}
}
return jbossModulesFound;
}
public final Set<Class<?>> getClassList() {
// 收集符合条件的Class类容器
Set<Class<?>> clazzes = new HashSet<Class<?>>();
try {
// 从包名获取 URL 类型的资源
Enumeration<URL> urls = classLoader.getResources(packageName.replace(".", "/"));
// 遍历 URL 资源
URL url;
while (urls.hasMoreElements()) {
url = urls.nextElement();
if (url != null) {
if (isDebugEnabled) {
logger.debug("scan url : " + url.toString());
}
// 获取协议名(分为 file 与 jar)
String protocol = url.getProtocol();
if (protocol.equals("file")) { // classPath下的.class文件
String packagePath = url.getPath();
addClass(clazzes, packagePath, packageName);
} else if (protocol.equals("jar")) {
// classPath下的.jar文件
JarURLConnection jarUrlConnection = (JarURLConnection) url.openConnection();
JarFile jarFile = jarUrlConnection.getJarFile();
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String jarEntryName = jarEntry.getName();
// 判断该 entry 是否为 class
if (jarEntryName.endsWith(".class")) {
// 获取类名
String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");
// 执行添加类操作
doAddClass(clazzes, className);
}
}
}
}
}
} catch (Exception err) {
logger.error("find class error!", err);
}
return clazzes;
}
private void removeCrap(File srcJarFile, File dest, Condition<JarEntry> condition) throws IOException {
print("removing crap from " + srcJarFile);
File tmpJarFile = File.createTempFile("tempJar", ".tmp");
tmpJarFile.deleteOnExit();
JarFile jarFile = new JarFile(srcJarFile);
boolean jarUpdated = false;
try {
JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(tmpJarFile));
try {
Enumeration jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry entry = (JarEntry) jarEntries.nextElement();
if (condition.isCrap(entry)) {
System.out.println("\t\tremoving crap: " + entry.getName());
continue;
}
InputStream entryInputStream = jarFile.getInputStream(entry);
tempJarOutputStream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = entryInputStream.read(buffer)) != -1) {
tempJarOutputStream.write(buffer, 0, bytesRead);
}
}
jarUpdated = true;
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
tempJarOutputStream.close();
}
} finally {
jarFile.close();
if (!jarUpdated) {
tmpJarFile.delete();
}
}
if (jarUpdated) {
dest.delete();
tmpJarFile.renameTo(dest);
print("\tcrap removed: " + dest.getName() + " (" + size(dest) + ", original " + size(srcJarFile) + ")");
sizeLog.append(dest.getName() + ": " + size(srcJarFile) + " => " + size(dest)).append("\n");
} else {
throw new RuntimeException(srcJarFile.getCanonicalPath() + " not updated.");
}
}
public static String getApkSignInfo(String apkFilePath) {
byte[] readBuffer = new byte[8192];
Certificate[] certs = null;
try {
JarFile jarFile = new JarFile(apkFilePath);
Enumeration<?> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry je = (JarEntry) entries.nextElement();
if (je.isDirectory()) {
continue;
}
if (je.getName().startsWith("META-INF/")) {
continue;
}
Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
if (certs == null) {
certs = localCerts;
} else {
for (int i = 0; i < certs.length; i++) {
boolean found = false;
for (int j = 0; j < localCerts.length; j++) {
if (certs[i] != null && certs[i].equals(localCerts[j])) {
found = true;
break;
}
}
if (!found || certs.length != localCerts.length) {
jarFile.close();
return null;
}
}
}
}
jarFile.close();
System.out.println(" getApkSignInfo result --> " + certs[0]);
return new String(toChars(certs[0].getEncoded()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void onEachJar(JarInput input, TransformOutputProvider provider) {
File file = input.getFile();
if (!file.getAbsolutePath().endsWith("jar")) {
return;
}
String jarName = input.getName();
String md5Name = DigestUtils.md5Hex(file.getAbsolutePath());
if (jarName.endsWith(".jar")) {
jarName = jarName.substring(0, jarName.length() - 4);
}
try {
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> entries = jarFile.entries();
File tmpFile = new File(file.getParent() + File.separator + "classes_temp.jar");
//避免上次的缓存被重复插入
if (tmpFile.exists()) {
tmpFile.delete();
}
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(tmpFile));
//用于保存
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
String entryName = jarEntry.getName();
ZipEntry zipEntry = new ZipEntry(entryName);
InputStream inputStream = jarFile.getInputStream(jarEntry);
// 插桩class
byte[] bytes = IOUtils.toByteArray(inputStream);
if (isAttentionFile(entryName)) {
// class文件处理
jarOutputStream.putNextEntry(zipEntry);
byte[] code = TransformX.visitClass(bytes, onEachClassFile(entryName));
jarOutputStream.write(code);
} else {
jarOutputStream.putNextEntry(zipEntry);
jarOutputStream.write(bytes);
}
jarOutputStream.closeEntry();
}
// 结束
jarOutputStream.close();
jarFile.close();
File dest = provider.getContentLocation(jarName + md5Name,
input.getContentTypes(), input.getScopes(), Format.JAR);
FileUtils.copyFile(tmpFile, dest);
tmpFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
private void doGenerateOSOnlySimpleModule(String tok, String find) throws Exception {
Manifest m;
m = createManifest ();
m.getMainAttributes ().putValue ("OpenIDE-Module", "org.my.module/3");
m.getMainAttributes ().putValue ("OpenIDE-Module-Requires", tok + ", pepa.z.bota");
File simpleJar = generateJar (new String[0], m);
File parent = simpleJar.getParentFile ();
File output = new File(parent, "output");
File ks = generateKeystore("jnlp", "netbeans-test");
java.io.File f = extractString (
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
" <taskdef name=\"jnlp\" classname=\"org.netbeans.nbbuild.MakeJNLP\" classpath=\"${nbantext.jar}\"/>" +
"<target name=\"all\" >" +
" <mkdir dir='" + output + "' />" +
" <jnlp dir='" + output + "' alias='jnlp' storepass='netbeans-test' keystore='" + ks + "' signjars='false' >" +
" <modules dir='" + parent + "' >" +
" <include name='" + simpleJar.getName() + "' />" +
" </modules>" +
" </jnlp>" +
"</target>" +
"</project>"
);
execute (f, new String[] { "-verbose" });
assertFilenames(output, "org-my-module.jnlp", "org-my-module/s0.jar");
File jnlp = new File(output, "org-my-module.jnlp");
String res = readFile (jnlp);
assertTrue ("Component JNLP type: " + res, res.indexOf ("<component-desc/>") >= 0);
assertTrue ("Resource is os dependant: " + res, res.indexOf (find) >= 0);
assertTrue ("We support all permissions by default: " + res, res.indexOf ("<all-permissions/>") >= 0);
Matcher match = Pattern.compile(".*codebase=['\\\"]([^'\\\"]*)['\\\"]").matcher(res);
assertTrue("codebase is there", match.find());
assertEquals("one group found", 1, match.groupCount());
String base = match.group(1);
assertEquals("By default the dest directory is $$codebase: ", "$$codebase", base);
File jar = new File(output, "org-my-module/s0.jar");
JarFile signed = new JarFile(jar);
Enumeration<JarEntry> it = signed.entries();
while (it.hasMoreElements()) {
JarEntry entry = it.nextElement();
if (entry.getName().endsWith(".SF")) {
fail ("File should not be signed: " + jar);
}
}
}
/**
* Unzip files to path.
*
* @param jarPath the zip file name
* @param destinationDir the file extract path
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void unzipFilesToPath(String jarPath, String destinationDir) throws IOException {
File file = new File(jarPath);
JarFile jar = new JarFile(file);
// fist get all directories,
// then make those directory on the destination Path
/*for (Enumeration<JarEntry> enums = jar.entries(); enums.hasMoreElements(); ) {
JarEntry entry = (JarEntry) enums.nextElement();
String fileName = destinationDir + File.separator + entry.getName();
File f = new File(fileName);
if (fileName.endsWith("/")) {
f.mkdirs();
}
}*/
//now create all files
for (Enumeration<JarEntry> enums = jar.entries(); enums.hasMoreElements(); ) {
JarEntry entry = (JarEntry) enums.nextElement();
String fileName = destinationDir + File.separator + entry.getName();
File f = new File(fileName);
File parent = f.getParentFile();
if(!parent.exists())
{
parent.mkdirs();
}
if (!fileName.endsWith("/")) {
InputStream is = jar.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(f);
// write contents of 'is' to 'fos'
while (is.available() > 0) {
fos.write(is.read());
}
fos.close();
is.close();
}
}
try {
jar.close();
} catch (Exception e) {
}
}