mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add method
This commit is contained in:
parent
f2e54a0f93
commit
df2147cd7c
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.7.5 (2021-07-14)
|
||||
# 5.7.5 (2021-07-16)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒
|
||||
@ -13,6 +13,7 @@
|
||||
* 【core 】 StrUtil.insert支持负数index
|
||||
* 【core 】 Calculator类支持取模运算(issue#I40DUW@Gitee)
|
||||
* 【core 】 增加Base64.isBase64方法(issue#1710@Github)
|
||||
* 【core 】 ManifestUtil新增方法getManifest(Class<?> cls)(pr#370@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复FileUtil.normalize处理上级路径的问题(issue#I3YPEH@Gitee)
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@ -22,44 +22,45 @@ public class ManifestUtil {
|
||||
private static final String[] MANIFEST_NAMES = {"Manifest.mf", "manifest.mf", "MANIFEST.MF"};
|
||||
|
||||
/**
|
||||
* 根据 class 获取 jar 包文件的 Manifest
|
||||
* 根据 class 获取 所在 jar 包文件的 Manifest<br>
|
||||
* 此方法主要利用class定位jar包,如引入hutool-all,则传入hutool中任意一个类即可获取这个jar的Manifest信息<br>
|
||||
* 如果这个类不在jar包中,返回{@code null}
|
||||
*
|
||||
* @param cls 类
|
||||
* @return Manifest
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static Manifest getManifest(Class<?> cls) throws IOException {
|
||||
URL url = ClassUtil.getResourceUrl("", cls);
|
||||
JarFile jarFile = null;
|
||||
public static Manifest getManifest(Class<?> cls) throws IORuntimeException {
|
||||
URL url = ResourceUtil.getResource(null, cls);
|
||||
URLConnection connection;
|
||||
try {
|
||||
URLConnection connection = url.openConnection();
|
||||
if (connection instanceof JarURLConnection) {
|
||||
JarURLConnection urlConnection = (JarURLConnection) connection;
|
||||
jarFile = urlConnection.getJarFile();
|
||||
return jarFile.getManifest();
|
||||
}
|
||||
} finally {
|
||||
IoUtil.close(jarFile);
|
||||
connection = url.openConnection();
|
||||
}catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
|
||||
if (connection instanceof JarURLConnection) {
|
||||
JarURLConnection conn = (JarURLConnection) connection;
|
||||
return getManifest(conn);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 jar 包文件的 Manifest
|
||||
* 获取 jar 包文件或项目目录下的 Manifest
|
||||
*
|
||||
* @param classpathItem 文件路径
|
||||
* @return Manifest
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static Manifest getManifest(File classpathItem) {
|
||||
public static Manifest getManifest(File classpathItem) throws IORuntimeException{
|
||||
Manifest manifest = null;
|
||||
|
||||
if (classpathItem.isFile()) {
|
||||
JarFile jar = null;
|
||||
try {
|
||||
jar = new JarFile(classpathItem);
|
||||
manifest = jar.getManifest();
|
||||
} catch (final IOException ignore) {
|
||||
} finally {
|
||||
IoUtil.close(jar);
|
||||
try (JarFile jarFile = new JarFile(classpathItem)){
|
||||
manifest = getManifest(jarFile);
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
final File metaDir = new File(classpathItem, "META-INF");
|
||||
@ -73,18 +74,47 @@ public class ManifestUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (manifestFile != null) {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(manifestFile);
|
||||
if (null != manifestFile) {
|
||||
try(FileInputStream fis = new FileInputStream(manifestFile)){
|
||||
manifest = new Manifest(fis);
|
||||
} catch (final IOException ignore) {
|
||||
} finally {
|
||||
IoUtil.close(fis);
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return manifest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 {@link JarURLConnection} 获取 jar 包文件的 Manifest
|
||||
*
|
||||
* @param connection {@link JarURLConnection}
|
||||
* @return Manifest
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static Manifest getManifest(JarURLConnection connection) throws IORuntimeException{
|
||||
final JarFile jarFile;
|
||||
try {
|
||||
jarFile = connection.getJarFile();
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
return getManifest(jarFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 {@link JarURLConnection} 获取 jar 包文件的 Manifest
|
||||
*
|
||||
* @param jarFile {@link JarURLConnection}
|
||||
* @return Manifest
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static Manifest getManifest(JarFile jarFile) throws IORuntimeException {
|
||||
try {
|
||||
return jarFile.getManifest();
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -174,11 +174,12 @@ public class ResourceUtil {
|
||||
/**
|
||||
* 获得资源相对路径对应的URL
|
||||
*
|
||||
* @param resource 资源相对路径
|
||||
* @param resource 资源相对路径,{@code null}和""都表示classpath根路径
|
||||
* @param baseClass 基准Class,获得的相对路径相对于此Class所在路径,如果为{@code null}则相对ClassPath
|
||||
* @return {@link URL}
|
||||
*/
|
||||
public static URL getResource(String resource, Class<?> baseClass) {
|
||||
resource = StrUtil.nullToEmpty(resource);
|
||||
return (null != baseClass) ? baseClass.getResource(resource) : ClassLoaderUtil.getClassLoader().getResource(resource);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,15 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
public class ManifestUtilTest {
|
||||
|
||||
@Test
|
||||
public void getManiFestTest(){
|
||||
final Manifest manifest = ManifestUtil.getManifest(Test.class);
|
||||
Assert.assertNotNull(manifest);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user