diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java | 53 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java | 58 |
2 files changed, 80 insertions, 31 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java index 8c8a687..4b93d17 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java @@ -3,27 +3,60 @@ package com.keuin.kbackupfabric.util; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public final class PrintUtil { private static final Logger LOGGER = LogManager.getLogger(); + private static final boolean printDebugMessages = true; private static final boolean printErrorMessages = true; private static final boolean printInfoMessages = true; + private static final Object syncDebug = new Object(); private static final Object syncError = new Object(); private static final Object syncInfo = new Object(); private static final Object syncMessage = new Object(); - public static void message(CommandContext<ServerCommandSource> context, String messageText) { - message(context, messageText, false); + private static final Style infoStyle = new Style().setColor(Formatting.WHITE); + private static final Style debugStyle = new Style().setUnderline(true); + private static final Style warnStyle = new Style().setColor(Formatting.YELLOW); + private static final Style errorStyle = new Style().setColor(Formatting.DARK_RED); + + + public static CommandContext<ServerCommandSource> msgInfo(CommandContext<ServerCommandSource> context, String messageText) { + return msgInfo(context, messageText, false); + } + + public static CommandContext<ServerCommandSource> msgWarn(CommandContext<ServerCommandSource> context, String messageText) { + return msgWarn(context, messageText, false); + } + + public static CommandContext<ServerCommandSource> msgErr(CommandContext<ServerCommandSource> context, String messageText) { + return msgErr(context, messageText, false); + } + + public static CommandContext<ServerCommandSource> msgInfo(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) { + return message(context, messageText, broadcastToOps, infoStyle); + } + + public static CommandContext<ServerCommandSource> msgWarn(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) { + return message(context, messageText, broadcastToOps, warnStyle); + } + + public static CommandContext<ServerCommandSource> msgErr(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) { + return message(context, messageText, broadcastToOps, errorStyle); } - public static CommandContext<ServerCommandSource> message(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) { + public static CommandContext<ServerCommandSource> message(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps, Style style) { synchronized (syncMessage) { - context.getSource().sendFeedback(new LiteralText("[KBackup] " + messageText), broadcastToOps); + Text text = new LiteralText(messageText); + text.setStyle(style); + context.getSource().sendFeedback(text, broadcastToOps); } return context; } @@ -31,8 +64,8 @@ public final class PrintUtil { public static void debug(String message) { synchronized (syncDebug) { if (printDebugMessages) { - System.out.println(String.format("[DEBUG] [KBackup] %s", message)); - LOGGER.debug(message); + //System.out.println(String.format("[DBG] [KB] %s", message)); + LOGGER.debug("[KB][DEBUG] " + message); } } } @@ -40,8 +73,8 @@ public final class PrintUtil { public static void error(String message) { synchronized (syncError) { if (printErrorMessages) { - System.out.println(String.format("[ERROR] [KBackup] %s", message)); - LOGGER.error(message); + //System.out.println(String.format("[ERR] [KB] %s", message)); + LOGGER.error("[KB][ERROR]" + message); } } } @@ -49,8 +82,8 @@ public final class PrintUtil { public static void info(String message) { synchronized (syncInfo) { if (printInfoMessages) { - System.out.println(String.format("[INFO] [KBackup] %s", message)); - LOGGER.info(message); + //System.out.println(String.format("[INF] [KB] %s", message)); + LOGGER.info("[KB][INFO] " + message); } } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index fec4436..5297dda 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -1,5 +1,7 @@ package com.keuin.kbackupfabric.util; +import com.keuin.kbackupfabric.data.BackupMetadata; + import java.io.*; import java.util.Enumeration; import java.util.zip.*; @@ -9,12 +11,12 @@ public final class ZipUtil { /** * 递归压缩文件夹 * - * @param srcRootDir 压缩文件夹根目录的子路径 - * @param file 当前递归压缩的文件或目录对象 - * @param zos 压缩文件存储对象 + * @param srcRootDir 压缩文件夹根目录的子路径 + * @param file 当前递归压缩的文件或目录对象 + * @param zipOutputStream 压缩文件存储对象 * @throws IOException IO Error */ - private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws IOException { + private static void zip(String srcRootDir, File file, ZipOutputStream zipOutputStream) throws IOException { if (file == null) { return; } @@ -31,13 +33,13 @@ public final class ZipUtil { subPath = subPath.substring(srcRootDir.length() + File.separator.length()); } ZipEntry entry = new ZipEntry(subPath); - zos.putNextEntry(entry); - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); - while ((count = bis.read(data, 0, bufferLen)) != -1) { - zos.write(data, 0, count); + zipOutputStream.putNextEntry(entry); + BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); + while ((count = inputStream.read(data, 0, bufferLen)) != -1) { + zipOutputStream.write(data, 0, count); } - bis.close(); - zos.closeEntry(); + inputStream.close(); + zipOutputStream.closeEntry(); } // 如果是目录,则压缩整个目录 else { @@ -45,11 +47,16 @@ public final class ZipUtil { File[] childFileList = file.listFiles(); if (childFileList != null) { for (File value : childFileList) - zip(srcRootDir, value, zos); + zip(srcRootDir, value, zipOutputStream); } } } +// public static void makeZipBackup(String srcPath, String zipPath, String zipFileName) throws IOException, ZipUtilException { +// zip(srcPath, zipPath, zipFileName, null); +// } + + /** * 对文件或文件目录进行压缩 * @@ -59,12 +66,12 @@ public final class ZipUtil { * @throws IOException IO Error * @throws ZipUtilException General exception, such as loop recursion or invalid input. */ - public static void zip(String srcPath, String zipPath, String zipFileName) throws IOException, ZipUtilException { + public static void makeBackupZip(String srcPath, String zipPath, String zipFileName, BackupMetadata backupMetadata) throws IOException, ZipUtilException { if (srcPath.isEmpty() || zipPath.isEmpty() || zipFileName.isEmpty()) { throw new ZipUtilException("Parameter for zip() contains null."); } - CheckedOutputStream cos; - ZipOutputStream zos = null; + CheckedOutputStream checkedOutputStream; + ZipOutputStream zipOutputStream = null; try { File srcFile = new File(srcPath); @@ -94,8 +101,18 @@ public final class ZipUtil { } } - cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32()); - zos = new ZipOutputStream(cos); + checkedOutputStream = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32()); + zipOutputStream = new ZipOutputStream(checkedOutputStream); + + // If with backup metadata, we serialize it and write it into file "kbackup_metadata" + ZipEntry metadataEntry = new ZipEntry(BackupMetadata.metadataFileName); + zipOutputStream.putNextEntry(metadataEntry); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(backupMetadata); + oos.close(); + zipOutputStream.write(baos.toByteArray()); + zipOutputStream.closeEntry(); //如果只是压缩一个文件,则需要截取该文件的父目录 String srcRootDir = srcPath; @@ -106,12 +123,12 @@ public final class ZipUtil { } } //调用递归压缩方法进行目录或文件压缩 - zip(srcRootDir, srcFile, zos); - zos.flush(); + zip(srcRootDir, srcFile, zipOutputStream); + zipOutputStream.flush(); } finally { try { - if (zos != null) { - zos.close(); + if (zipOutputStream != null) { + zipOutputStream.close(); } } catch (Exception e) { e.printStackTrace(); @@ -126,7 +143,6 @@ public final class ZipUtil { * @param unzipFilePath 解压后的文件保存的路径 * @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含 */ - @SuppressWarnings("unchecked") public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws ZipUtilException, IOException { if (zipFilePath.isEmpty() || unzipFilePath.isEmpty()) { throw new ZipUtilException("Parameter for unzip() contains null."); |