init/push

This commit is contained in:
2026-08-03 16:36:45 +08:00
commit d0371663d9
11 changed files with 834 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
package top.gtb520.java.gnu.java.utils.library;
import top.gtb520.java.gnu.java.utils.library.utils.print.ColorNameEnum;
public interface main {
// 压缩包操作相关接口与方法
interface compress {
// 解压Tar.Gz压缩包
static void UnTarGz(String sourceFilePanth, String destFilePanth) throws Exception {
top.gtb520.java.gnu.java.utils.library.utils.compress.compress.UnTarGz(sourceFilePanth, destFilePanth);
}
// 压缩文件或目录为Tar.Gz压缩包
static void TarGz(String sourceFilePanth, String destFilePanth) throws Exception {
top.gtb520.java.gnu.java.utils.library.utils.compress.compress.MakeTarGz(sourceFilePanth, destFilePanth);
}
}
// 终端打印接口与方法
interface echo {
// 使用预定义的颜色名称在终端打印彩色文本
static void Println(String Message, ColorNameEnum ColorName) {
top.gtb520.java.gnu.java.utils.library.utils.print.echo.ColorPrintln(Message, ColorName);
}
// 使用自定义十六进制颜色码在终端打印彩色文本
static void ColorHexPrintln(String Message, String ColorCode) {
top.gtb520.java.gnu.java.utils.library.utils.print.echo.ColorHexPrintln(Message, ColorCode);
}
}
}

View File

@@ -0,0 +1,187 @@
package top.gtb520.java.gnu.java.utils.library.utils.compress;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
/**
* 压缩包
*/
public class compress {
/**
* 解压Tar.Gz压缩包
*
* @param sourceFilePanth 压缩包路径
* @param destFilePanth 解压路径
* @throws Exception
*/
public static void UnTarGz(String sourceFilePanth, String destFilePanth) throws Exception {
Path destPath = Paths.get(destFilePanth);
if (!Files.isDirectory(destPath)) {
throw new IllegalArgumentException("目标路径必须是有效的目录");
}
try (InputStream fileIn = Files.newInputStream(Paths.get(sourceFilePanth));
BufferedInputStream bufferedIn = new BufferedInputStream(fileIn);
GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(bufferedIn);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
TarArchiveEntry entry;
while ((entry = tarIn.getNextTarEntry()) != null) {
Path targetPath = destPath.resolve(entry.getName());
if (entry.isDirectory()) {
Files.createDirectories(targetPath);
} else {
Files.createDirectories(targetPath.getParent());
Files.copy(tarIn, targetPath);
}
}
}
}
/**
* 压缩Tar.Gz压缩包
*
* @param sourceFilePanth 源文件或目录路径
* @param destFilePanth 压缩包路径
* @throws Exception
*/
public static void MakeTarGz(String sourceFilePanth, String destFilePanth) throws Exception {
Path sourcePath = Paths.get(sourceFilePanth);
if (!Files.exists(sourcePath)) {
throw new IllegalArgumentException("源路径不存在");
}
try (OutputStream fileOut = Files.newOutputStream(Paths.get(destFilePanth));
BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOut);
GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(bufferedOut);
TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) {
tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
if (Files.isDirectory(sourcePath)) {
Files.walkFileTree(sourcePath, new java.nio.file.SimpleFileVisitor<Path>() {
@Override
public java.nio.file.FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws java.io.IOException {
String entryName = sourcePath.relativize(file).toString().replace('\\', '/');
TarArchiveEntry entry = new TarArchiveEntry(file.toFile(), entryName);
tarOut.putArchiveEntry(entry);
Files.copy(file, tarOut);
tarOut.closeArchiveEntry();
return java.nio.file.FileVisitResult.CONTINUE;
}
@Override
public java.nio.file.FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws java.io.IOException {
if (!dir.equals(sourcePath)) {
String entryName = sourcePath.relativize(dir).toString().replace('\\', '/') + "/";
TarArchiveEntry entry = new TarArchiveEntry(entryName);
tarOut.putArchiveEntry(entry);
tarOut.closeArchiveEntry();
}
return java.nio.file.FileVisitResult.CONTINUE;
}
});
} else {
TarArchiveEntry entry = new TarArchiveEntry(sourcePath.toFile(), sourcePath.getFileName().toString());
tarOut.putArchiveEntry(entry);
Files.copy(sourcePath, tarOut);
tarOut.closeArchiveEntry();
}
}
}
/**
* 压缩.zip压缩包
*
* @param sourceFilePanth 源文件或目录路径
* @param destFilePanth 压缩包路径
* @throws Exception
*/
public static void MakeZip(String sourceFilePanth, String destFilePanth) throws Exception {
Path sourcePath = Paths.get(sourceFilePanth);
if (!Files.exists(sourcePath)) {
throw new IllegalArgumentException("源路径不存在");
}
try (OutputStream fileOut = Files.newOutputStream(Paths.get(destFilePanth));
BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOut);
ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(bufferedOut)) {
if (Files.isDirectory(sourcePath)) {
Files.walkFileTree(sourcePath, new java.nio.file.SimpleFileVisitor<Path>() {
@Override
public java.nio.file.FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws java.io.IOException {
String entryName = sourcePath.relativize(file).toString().replace('\\', '/');
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
zipOut.putArchiveEntry(entry);
Files.copy(file, zipOut);
zipOut.closeArchiveEntry();
return java.nio.file.FileVisitResult.CONTINUE;
}
@Override
public java.nio.file.FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws java.io.IOException {
if (!dir.equals(sourcePath)) {
String entryName = sourcePath.relativize(dir).toString().replace('\\', '/') + "/";
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
zipOut.putArchiveEntry(entry);
zipOut.closeArchiveEntry();
}
return java.nio.file.FileVisitResult.CONTINUE;
}
});
} else {
ZipArchiveEntry entry = new ZipArchiveEntry(sourcePath.getFileName().toString());
zipOut.putArchiveEntry(entry);
Files.copy(sourcePath, zipOut);
zipOut.closeArchiveEntry();
}
}
}
/**
* 解压.zip压缩包
*
* @param sourceFilePanth 压缩包路径
* @param destFilePanth 解压路径
* @throws Exception
*/
public static void UnZip(String sourceFilePanth, String destFilePanth) throws Exception {
Path destPath = Paths.get(destFilePanth);
if (!Files.isDirectory(destPath)) {
throw new IllegalArgumentException("目标路径必须是有效的目录");
}
try (InputStream fileIn = Files.newInputStream(Paths.get(sourceFilePanth));
BufferedInputStream bufferedIn = new BufferedInputStream(fileIn);
ZipArchiveInputStream zipIn = new ZipArchiveInputStream(bufferedIn)) {
ZipArchiveEntry entry;
while ((entry = zipIn.getNextEntry()) != null) {
Path targetPath = destPath.resolve(entry.getName());
if (entry.isDirectory()) {
Files.createDirectories(targetPath);
} else {
Files.createDirectories(targetPath.getParent());
Files.copy(zipIn, targetPath);
}
}
}
}
}

View File

@@ -0,0 +1,5 @@
package top.gtb520.java.gnu.java.utils.library.utils.print;
public enum ColorNameEnum {
RED, GREEN, YELLOW, BLUE, PURPLE, CYAN;
}

View File

@@ -0,0 +1,52 @@
package top.gtb520.java.gnu.java.utils.library.utils.print;
/**
* 终端彩色输出工具类
* 提供基于 ANSI 转义序列的终端彩色打印功能
*/
public class echo {
// 终端彩色打印
/**
* 使用自定义十六进制颜色码在终端打印彩色文本
*
* @param Message 要打印的文本内容
* @param HexColor ANSI 颜色代码(如 "31" 表示红色,"38;5;208" 表示扩展色)
*/
public static void ColorHexPrintln(String Message, String HexColor) {
System.out.println("\033[" + HexColor + "m" + Message + "\033[0m");
}
/**
* 使用预定义的颜色名称在终端打印彩色文本
*
* @param Message 要打印的文本内容
* @param ColorName 颜色名称支持RED、GREEN、YELLOW、BLUE、PURPLE、CYAN传入不支持的名称则按默认颜色输出
*/
public static void ColorPrintln(String Message, ColorNameEnum ColorName) {
// 根据颜色名称映射对应的 ANSI 颜色转义序列
switch (ColorName) {
case RED:
System.out.println("\033[31m" + Message + "\033[0m");
break;
case GREEN:
System.out.println("\033[32m" + Message + "\033[0m");
break;
case YELLOW:
System.out.println("\033[33m" + Message + "\033[0m");
break;
case BLUE:
System.out.println("\033[34m" + Message + "\033[0m");
break;
case PURPLE:
System.out.println("\033[35m" + Message + "\033[0m");
break;
case CYAN:
System.out.println("\033[36m" + Message + "\033[0m");
break;
// 未匹配到已知颜色名称时,以默认颜色输出
default:
System.out.println(Message);
}
}
}

View File

@@ -0,0 +1,281 @@
import top.gtb520.java.gnu.java.utils.library.utils.compress.compress;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class compressTest {
static int passed = 0;
static int failed = 0;
public static void main(String[] args) throws Exception {
System.out.println("========== compress 测试开始 ==========");
testMakeZipAndUnZipSingleFile();
testMakeZipAndUnZipDirectory();
testMakeTarGzAndUnTarGzSingleFile();
testMakeTarGzAndUnTarGzDirectory();
testMakeZipSourceNotExist();
testUnZipDestNotDirectory();
testMakeTarGzSourceNotExist();
testUnTarGzDestNotDirectory();
System.out.println("========================================");
System.out.println("测试完成: 通过 " + passed + ", 失败 " + failed);
if (failed > 0) {
System.exit(1);
}
}
/**
* 测试单文件的 Zip 压缩与解压
*/
static void testMakeZipAndUnZipSingleFile() throws Exception {
String testName = "testMakeZipAndUnZipSingleFile";
Path tempDir = Files.createTempDirectory("compress_test_");
try {
// 准备源文件
Path sourceFile = tempDir.resolve("source.txt");
Files.writeString(sourceFile, "Hello, Zip!");
// 压缩
Path zipFile = tempDir.resolve("output.zip");
compress.MakeZip(sourceFile.toString(), zipFile.toString());
assertCondition(testName + " - 压缩文件应存在", Files.exists(zipFile));
// 解压
Path unzipDir = tempDir.resolve("unzip");
Files.createDirectories(unzipDir);
compress.UnZip(zipFile.toString(), unzipDir.toString());
Path resultFile = unzipDir.resolve("source.txt");
assertCondition(testName + " - 解压文件应存在", Files.exists(resultFile));
assertCondition(testName + " - 内容应一致",
"Hello, Zip!".equals(Files.readString(resultFile)));
} finally {
deleteTempDir(tempDir);
}
}
/**
* 测试目录的 Zip 压缩与解压
*/
static void testMakeZipAndUnZipDirectory() throws Exception {
String testName = "testMakeZipAndUnZipDirectory";
Path tempDir = Files.createTempDirectory("compress_test_");
try {
// 准备源目录结构
Path sourceDir = tempDir.resolve("source");
Files.createDirectories(sourceDir.resolve("sub"));
Files.writeString(sourceDir.resolve("a.txt"), "file a");
Files.writeString(sourceDir.resolve("sub/b.txt"), "file b");
// 压缩
Path zipFile = tempDir.resolve("dir_output.zip");
compress.MakeZip(sourceDir.toString(), zipFile.toString());
assertCondition(testName + " - 压缩文件应存在", Files.exists(zipFile));
// 解压
Path unzipDir = tempDir.resolve("unzip_dir");
Files.createDirectories(unzipDir);
compress.UnZip(zipFile.toString(), unzipDir.toString());
assertCondition(testName + " - 文件a应存在",
Files.exists(unzipDir.resolve("a.txt")));
assertCondition(testName + " - 文件b应存在",
Files.exists(unzipDir.resolve("sub/b.txt")));
assertCondition(testName + " - 文件a内容应一致",
"file a".equals(Files.readString(unzipDir.resolve("a.txt"))));
assertCondition(testName + " - 文件b内容应一致",
"file b".equals(Files.readString(unzipDir.resolve("sub/b.txt"))));
} finally {
deleteTempDir(tempDir);
}
}
/**
* 测试单文件的 Tar.Gz 压缩与解压
*/
static void testMakeTarGzAndUnTarGzSingleFile() throws Exception {
String testName = "testMakeTarGzAndUnTarGzSingleFile";
Path tempDir = Files.createTempDirectory("compress_test_");
try {
// 准备源文件
Path sourceFile = tempDir.resolve("source.txt");
Files.writeString(sourceFile, "Hello, TarGz!");
// 压缩
Path tarGzFile = tempDir.resolve("output.tar.gz");
compress.MakeTarGz(sourceFile.toString(), tarGzFile.toString());
assertCondition(testName + " - 压缩文件应存在", Files.exists(tarGzFile));
// 解压
Path untarDir = tempDir.resolve("untar");
Files.createDirectories(untarDir);
compress.UnTarGz(tarGzFile.toString(), untarDir.toString());
Path resultFile = untarDir.resolve("source.txt");
assertCondition(testName + " - 解压文件应存在", Files.exists(resultFile));
assertCondition(testName + " - 内容应一致",
"Hello, TarGz!".equals(Files.readString(resultFile)));
} finally {
deleteTempDir(tempDir);
}
}
/**
* 测试目录的 Tar.Gz 压缩与解压
*/
static void testMakeTarGzAndUnTarGzDirectory() throws Exception {
String testName = "testMakeTarGzAndUnTarGzDirectory";
Path tempDir = Files.createTempDirectory("compress_test_");
try {
// 准备源目录结构
Path sourceDir = tempDir.resolve("source");
Files.createDirectories(sourceDir.resolve("sub"));
Files.writeString(sourceDir.resolve("a.txt"), "file a");
Files.writeString(sourceDir.resolve("sub/b.txt"), "file b");
// 压缩
Path tarGzFile = tempDir.resolve("dir_output.tar.gz");
compress.MakeTarGz(sourceDir.toString(), tarGzFile.toString());
assertCondition(testName + " - 压缩文件应存在", Files.exists(tarGzFile));
// 解压
Path untarDir = tempDir.resolve("untar_dir");
Files.createDirectories(untarDir);
compress.UnTarGz(tarGzFile.toString(), untarDir.toString());
assertCondition(testName + " - 文件a应存在",
Files.exists(untarDir.resolve("a.txt")));
assertCondition(testName + " - 文件b应存在",
Files.exists(untarDir.resolve("sub/b.txt")));
assertCondition(testName + " - 文件a内容应一致",
"file a".equals(Files.readString(untarDir.resolve("a.txt"))));
assertCondition(testName + " - 文件b内容应一致",
"file b".equals(Files.readString(untarDir.resolve("sub/b.txt"))));
} finally {
deleteTempDir(tempDir);
}
}
/**
* 测试压缩不存在的源路径应抛出异常
*/
static void testMakeZipSourceNotExist() {
String testName = "testMakeZipSourceNotExist";
try {
compress.MakeZip("not_exist_path", "output.zip");
assertCondition(testName + " - 应抛出异常", false);
} catch (IllegalArgumentException e) {
assertCondition(testName + " - 应抛出IllegalArgumentException", true);
} catch (Exception e) {
assertCondition(testName + " - 异常类型不正确: " + e.getClass().getName(), false);
}
}
/**
* 测试解压到非目录路径应抛出异常
*/
static void testUnZipDestNotDirectory() throws Exception {
String testName = "testUnZipDestNotDirectory";
Path tempDir = Files.createTempDirectory("compress_test_");
try {
// 创建一个文件作为解压目标(非目录)
Path notDir = tempDir.resolve("not_a_dir.txt");
Files.writeString(notDir, "I am a file");
// 先创建一个合法zip
Path sourceFile = tempDir.resolve("src.txt");
Files.writeString(sourceFile, "test");
Path zipFile = tempDir.resolve("test.zip");
compress.MakeZip(sourceFile.toString(), zipFile.toString());
try {
compress.UnZip(zipFile.toString(), notDir.toString());
assertCondition(testName + " - 应抛出异常", false);
} catch (IllegalArgumentException e) {
assertCondition(testName + " - 应抛出IllegalArgumentException", true);
} catch (Exception e) {
assertCondition(testName + " - 异常类型不正确: " + e.getClass().getName(), false);
}
} finally {
deleteTempDir(tempDir);
}
}
/**
* 测试TarGz压缩不存在的源路径应抛出异常
*/
static void testMakeTarGzSourceNotExist() {
String testName = "testMakeTarGzSourceNotExist";
try {
compress.MakeTarGz("not_exist_path", "output.tar.gz");
assertCondition(testName + " - 应抛出异常", false);
} catch (IllegalArgumentException e) {
assertCondition(testName + " - 应抛出IllegalArgumentException", true);
} catch (Exception e) {
assertCondition(testName + " - 异常类型不正确: " + e.getClass().getName(), false);
}
}
/**
* 测试TarGz解压到非目录路径应抛出异常
*/
static void testUnTarGzDestNotDirectory() throws Exception {
String testName = "testUnTarGzDestNotDirectory";
Path tempDir = Files.createTempDirectory("compress_test_");
try {
Path notDir = tempDir.resolve("not_a_dir.txt");
Files.writeString(notDir, "I am a file");
// 先创建一个合法tar.gz
Path sourceFile = tempDir.resolve("src.txt");
Files.writeString(sourceFile, "test");
Path tarGzFile = tempDir.resolve("test.tar.gz");
compress.MakeTarGz(sourceFile.toString(), tarGzFile.toString());
try {
compress.UnTarGz(tarGzFile.toString(), notDir.toString());
assertCondition(testName + " - 应抛出异常", false);
} catch (IllegalArgumentException e) {
assertCondition(testName + " - 应抛出IllegalArgumentException", true);
} catch (Exception e) {
assertCondition(testName + " - 异常类型不正确: " + e.getClass().getName(), false);
}
} finally {
deleteTempDir(tempDir);
}
}
// ========== 辅助方法 ==========
static void assertCondition(String message, boolean condition) {
if (condition) {
System.out.println("[PASS] " + message);
passed++;
} else {
System.out.println("[FAIL] " + message);
failed++;
}
}
static void deleteTempDir(Path dir) {
try {
Files.walk(dir)
.sorted(java.util.Comparator.reverseOrder())
.forEach(path -> {
try {
Files.deleteIfExists(path);
} catch (IOException ignored) {
}
});
} catch (IOException ignored) {
}
}
}

13
src/test/java/echo.java Normal file
View File

@@ -0,0 +1,13 @@
import top.gtb520.java.gnu.java.utils.library.main;
import top.gtb520.java.gnu.java.utils.library.utils.print.ColorNameEnum;
public class echo {
static void main(String[] args) {
main.echo.Println("YELLOW", ColorNameEnum.YELLOW);
main.echo.Println("BLUE", ColorNameEnum.BLUE);
main.echo.Println("CYAN", ColorNameEnum.CYAN);
main.echo.Println("PURPLE", ColorNameEnum.PURPLE);
main.echo.Println("GREEN", ColorNameEnum.GREEN);
main.echo.Println("RED", ColorNameEnum.RED);
}
}