mirror of
https://github.com/CarmJos/MineSQL.git
synced 2026-06-05 00:48:16 +08:00
添加JarResourceUtils 用于复制示例文件。
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ import co.aikar.commands.annotation.*;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static cc.carm.plugin.easysql.util.ResourceReadUtil.getVersion;
|
||||
import static cc.carm.plugin.easysql.util.MavenReadUtil.getVersion;
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
+18
-8
@@ -1,5 +1,7 @@
|
||||
package cc.carm.plugin.easysql.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -8,26 +10,34 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class PropertiesUtil {
|
||||
public class DBPropertiesUtil {
|
||||
|
||||
public static Map<String, Properties> readDBProperties(File propertiesFolder) {
|
||||
public static Map<String, Properties> readFromFolder(File propertiesFolder) {
|
||||
Map<String, Properties> propertiesMap = new HashMap<>();
|
||||
if (!propertiesFolder.isDirectory()) return propertiesMap;
|
||||
|
||||
File[] files = propertiesFolder.listFiles();
|
||||
if (files == null || files.length == 0) return propertiesMap;
|
||||
for (File file : files) {
|
||||
if (file.getName().startsWith(".") || !file.getName().endsWith(".properties")) continue;
|
||||
if (!validateName(file.getName())) continue;
|
||||
String name = file.getName().substring(0, file.getName().lastIndexOf("."));
|
||||
try (InputStream stream = new FileInputStream(file)) {
|
||||
Properties properties = new Properties();
|
||||
properties.load(stream);
|
||||
propertiesMap.put(name, properties);
|
||||
try (InputStream is = new FileInputStream(file)) {
|
||||
propertiesMap.put(name, read(is));
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return propertiesMap;
|
||||
}
|
||||
|
||||
public static @NotNull Properties read(InputStream stream) throws IOException {
|
||||
Properties properties = new Properties();
|
||||
properties.load(stream);
|
||||
return properties;
|
||||
}
|
||||
|
||||
public static boolean validateName(String name) {
|
||||
return !name.contains(" ") && !name.startsWith(".") && name.endsWith(".properties");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package cc.carm.plugin.easysql.util;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public class JarResourceUtils {
|
||||
public static final char JAR_SEPARATOR = '/';
|
||||
|
||||
public static @Nullable String[] readResource(@Nullable InputStream resourceStream) {
|
||||
if (resourceStream == null) return null;
|
||||
try (Scanner scanner = new Scanner(resourceStream, "UTF-8")) {
|
||||
List<String> contents = new ArrayList<>();
|
||||
while (scanner.hasNextLine()) {
|
||||
contents.add(scanner.nextLine());
|
||||
}
|
||||
return contents.toArray(new String[0]);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyFolderFromJar(String folderName, File destFolder, CopyOption option)
|
||||
throws IOException {
|
||||
copyFolderFromJar(folderName, destFolder, option, null);
|
||||
}
|
||||
|
||||
public static void copyFolderFromJar(String folderName, File destFolder,
|
||||
CopyOption option, PathTrimmer trimmer) throws IOException {
|
||||
if (!destFolder.exists())
|
||||
destFolder.mkdirs();
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
File fullPath;
|
||||
String path = JarResourceUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
|
||||
if (trimmer != null)
|
||||
path = trimmer.trim(path);
|
||||
try {
|
||||
if (!path.startsWith("file"))
|
||||
path = "file://" + path;
|
||||
|
||||
fullPath = new File(new URI(path));
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(fullPath));
|
||||
|
||||
ZipEntry entry;
|
||||
while ((entry = zis.getNextEntry()) != null) {
|
||||
if (!entry.getName().startsWith(folderName + JAR_SEPARATOR))
|
||||
continue;
|
||||
|
||||
String fileName = entry.getName();
|
||||
|
||||
if (fileName.charAt(fileName.length() - 1) == JAR_SEPARATOR) {
|
||||
File file = new File(destFolder + File.separator + fileName);
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
}
|
||||
file.mkdirs();
|
||||
continue;
|
||||
}
|
||||
|
||||
File file = new File(destFolder + File.separator + fileName);
|
||||
if (option == CopyOption.COPY_IF_NOT_EXIST && file.exists())
|
||||
continue;
|
||||
|
||||
if (!file.getParentFile().exists())
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
if (!file.exists())
|
||||
file.createNewFile();
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
|
||||
int len;
|
||||
while ((len = zis.read(buffer)) > 0) {
|
||||
fos.write(buffer, 0, len);
|
||||
}
|
||||
fos.close();
|
||||
}
|
||||
|
||||
zis.closeEntry();
|
||||
zis.close();
|
||||
}
|
||||
|
||||
public enum CopyOption {
|
||||
COPY_IF_NOT_EXIST, REPLACE_IF_EXIST
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PathTrimmer {
|
||||
String trim(String original);
|
||||
}
|
||||
}
|
||||
+1
-18
@@ -4,26 +4,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ResourceReadUtil {
|
||||
|
||||
|
||||
public static @Nullable String[] readResource(@Nullable InputStream resourceStream) {
|
||||
if (resourceStream == null) return null;
|
||||
try (Scanner scanner = new Scanner(resourceStream, "UTF-8")) {
|
||||
List<String> contents = new ArrayList<>();
|
||||
while (scanner.hasNextLine()) {
|
||||
contents.add(scanner.nextLine());
|
||||
}
|
||||
return contents.toArray(new String[0]);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public class MavenReadUtil {
|
||||
|
||||
public static String getMavenPropertiesPath(@NotNull String groupID, @NotNull String artifactID) {
|
||||
return String.format("/META-INF/maven/%s/%s/pom.properties", groupID, artifactID);
|
||||
Reference in New Issue
Block a user