1 Star 0 Fork 0

ws/javm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
FileUtil.java 4.50 KB
一键复制 编辑 原始数据 按行查看 历史
YCKJ2817 提交于 4年前 . video encode tool
package cn.angs;
import sun.net.www.protocol.file.FileURLConnection;
import java.io.*;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* ClassName: FileUtil.java
* Description:
*
* @author wangshun@cloudwalk.cn
* @version 1.0.0
* @date 2021/07/22 17:49
*/
public class FileUtil {
public static void loadRecourseFromJarByFolder(String folderPath,String targetFolderPath ,Class clazz) {
URL url = clazz.getResource(folderPath);
URLConnection urlConnection;
try {
urlConnection = url.openConnection();
if (urlConnection instanceof FileURLConnection) {
copyFileResources(url, folderPath,targetFolderPath,clazz);
} else if (urlConnection instanceof JarURLConnection) {
copyJarResources((JarURLConnection) urlConnection,folderPath,targetFolderPath,clazz);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 当前运行环境资源文件是在文件里面的
*
* @param url
* @param folderPath
* @param clazz
* @throws IOException
*/
private static void copyFileResources(URL url, String folderPath,String targetFolderPath, Class clazz) throws IOException {
File root = new File(url.getPath());
if (root.isDirectory()) {
File[] files = root.listFiles();
for (File file : files) {
if (file.isDirectory()) {
loadRecourseFromJarByFolder(folderPath + "/" + file.getName(),targetFolderPath,clazz);
} else {
loadRecourseFromJar(folderPath + "/" + file.getName(),folderPath,clazz);
}
}
}
}
/**
* 当前运行环境资源文件是在jar里面的
*
* @param jarURLConnection
* @throws IOException
*/
private static void copyJarResources(JarURLConnection jarURLConnection, String folderPath, String targetFolderPath, Class clazz) throws IOException {
JarFile jarFile = jarURLConnection.getJarFile();
Enumeration<JarEntry> entrys = jarFile.entries();
while (entrys.hasMoreElements()) {
JarEntry entry = entrys.nextElement();
if (entry.getName().startsWith(jarURLConnection.getEntryName()) && !entry.getName().endsWith("/")) {
loadRecourseFromJar("/" + entry.getName(),targetFolderPath,clazz);
}
}
jarFile.close();
}
public static void loadRecourseFromJar(String path,String recourseFolder,Class clazz) throws IOException {
if (!path.startsWith("/")) {
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
}
if (path.endsWith("/")) {
throw new IllegalArgumentException("The path has to be absolute (cat not end with '/').");
}
int index = path.lastIndexOf('/');
String filename = path.substring(index + 1);
String folderPath = recourseFolder + path.substring(0, index + 1);
// If the folder does not exist yet, it will be created. If the folder
// exists already, it will be ignored
File dir = new File(folderPath);
if (!dir.exists()) {
dir.mkdirs();
}
// If the file does not exist yet, it will be created. If the file
// exists already, it will be ignored
filename = folderPath + filename;
File file = new File(filename);
if (!file.exists() && !file.createNewFile()) {
return;
}
// Prepare buffer for data copying
byte[] buffer = new byte[1024];
int readBytes;
// Open and check input stream
URL url = clazz.getResource(path);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();
if (is == null) {
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
}
OutputStream os = new FileOutputStream(file);
try {
while ((readBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, readBytes);
}
} finally {
os.close();
is.close();
}
}
public static void main(String[] args) throws IOException {
loadRecourseFromJarByFolder("/system/window", "C:\\Users\\YCKJ2817\\Desktop\\测试\\" ,FileUtil.class);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ws410/javm.git
git@gitee.com:ws410/javm.git
ws410
javm
javm
master

搜索帮助