From 23e6ba6a40f3fb0bb933e5aced99d4971702f5ee Mon Sep 17 00:00:00 2001 From: Ariesly Date: Wed, 26 Jun 2024 08:07:48 +0000 Subject: [PATCH] fix: support file names with GBK encoding in compress files. --- .../main/java/cn/keking/service/CompressFileReader.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/cn/keking/service/CompressFileReader.java b/server/src/main/java/cn/keking/service/CompressFileReader.java index 0581f1697..95e82fe9c 100644 --- a/server/src/main/java/cn/keking/service/CompressFileReader.java +++ b/server/src/main/java/cn/keking/service/CompressFileReader.java @@ -20,6 +20,7 @@ import java.io.OutputStream; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -88,7 +89,11 @@ public String unRar(String filePath, String filePassword, String fileName, FileA private Path getFilePathInsideArchive(ISimpleInArchiveItem item, Path folderPath) throws SevenZipException, UnsupportedEncodingException { String insideFileName = RarUtils.getUtf8String(item.getPath()); if (RarUtils.isMessyCode(insideFileName)) { - insideFileName = new String(item.getPath().getBytes(StandardCharsets.ISO_8859_1), "gbk"); + insideFileName = item.getPath(); + if(Charset.forName("GBK").newEncoder().canEncode(insideFileName)) + insideFileName = new String(insideFileName.getBytes("GBK"), "gbk"); + else + insideFileName = new String(insideFileName.getBytes(StandardCharsets.ISO_8859_1), "gbk"); } // 正规化路径并验证是否安全