MOD:支持加载其他目录索引

This commit is contained in:
leosam1024
2023-06-25 08:12:29 +08:00
parent e53fbb9e37
commit 6a214d9cb3
12 changed files with 372 additions and 92 deletions
@@ -2,11 +2,19 @@ package com.leosam.tvbox.mv.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* @author admin
@@ -14,7 +22,71 @@ import java.nio.charset.StandardCharsets;
*/
public class ClassPathReaderUtils {
public static int getSize(String path) {
public static final String CLASS_PATH_PREFIX = "classpath:";
public static Map<String, String> getDirectoryFiles(String filePath) throws IOException {
Map<String, String> pathMap = new LinkedHashMap<>();
if (StringUtils.isEmpty(filePath) || filePath.startsWith(CLASS_PATH_PREFIX)) {
return pathMap;
}
File data = new File(filePath);
if (data.exists() && data.isFile()) {
pathMap.put(data.getCanonicalPath(), data.getName());
}
if (data.exists() && data.isDirectory()) {
File[] files = data.listFiles();
for (File file : files) {
if (file.isFile()) {
pathMap.put(filePath + file.getName(), file.getName());
}
}
}
return pathMap;
}
public static String extractFileNameWithoutExtension(String filePath) {
Path path = Paths.get(filePath);
String fileNameWithExtension = path.getFileName().toString();
int lastDotIndex = fileNameWithExtension.lastIndexOf(".");
String fileNameWithoutExtension = lastDotIndex == -1 ? fileNameWithExtension
: fileNameWithExtension.substring(0, lastDotIndex);
return fileNameWithoutExtension;
}
public static void deleteAllFile(File file) {
if (file == null) {
return;
}
if (file.isFile()) {
file.delete();
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isFile()) {
file1.delete();
}
}
}
}
public static long getAllFileSize(Set<String> dataPath) {
long size = 0;
for (String path : dataPath) {
if (path.startsWith(CLASS_PATH_PREFIX)) {
String pathName = path.replace(CLASS_PATH_PREFIX, "");
size += getClassPathFileSize(pathName);
} else {
File file = new File(path);
if (file.exists() && file.isFile()) {
size += file.length();
}
}
}
return size;
}
public static int getClassPathFileSize(String path) {
try {
ClassPathResource resource = new ClassPathResource(path);
InputStream inputStream = resource.getInputStream();
@@ -38,10 +110,14 @@ public class ClassPathReaderUtils {
public static BufferedReader getBufferedReader(String path) {
try {
ClassPathResource resource = new ClassPathResource(path);
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(),StandardCharsets.UTF_8);
InputStreamReader inputStreamReader = null;
if (path.startsWith(CLASS_PATH_PREFIX)) {
path = path.replace(CLASS_PATH_PREFIX, "");
ClassPathResource resource = new ClassPathResource(path);
inputStreamReader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
} else {
inputStreamReader = new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8);
}
BufferedReader reader = new BufferedReader(inputStreamReader);
return reader;
} catch (IOException ex) {
@@ -79,6 +155,9 @@ public class ClassPathReaderUtils {
private final ClassLoader classLoader;
public ClassPathResource(String path) {
if (path.startsWith(CLASS_PATH_PREFIX)) {
path = path.replace(CLASS_PATH_PREFIX, "");
}
this.path = path;
this.classLoader = getDefaultClassLoader();
}
@@ -96,8 +175,7 @@ public class ClassPathReaderUtils {
return is;
}
public static ClassLoader getDefaultClassLoader() {
private static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();