【修改】 解压zip,功能修改

This commit is contained in:
RENWEIBING\letre
2025-05-06 20:52:47 +08:00
parent 3a0992bc0d
commit d2c9c399d7
2 changed files with 27 additions and 30 deletions

View File

@@ -43,7 +43,11 @@
<artifactId>Saxon-HE</artifactId> <artifactId>Saxon-HE</artifactId>
<version>12.5</version> <version>12.5</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.0</version> <!-- 或使用最新版本 -->
</dependency>
<!-- Web 相关 --> <!-- Web 相关 -->
<dependency> <dependency>
<groupId>pc.exam.gg</groupId> <groupId>pc.exam.gg</groupId>

View File

@@ -1,6 +1,8 @@
package pc.exam.pp.module.judgement.service.auto_tools; package pc.exam.pp.module.judgement.service.auto_tools;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import pc.exam.pp.framework.common.exception.ErrorCode; import pc.exam.pp.framework.common.exception.ErrorCode;
import pc.exam.pp.framework.common.pojo.CommonResult; import pc.exam.pp.framework.common.pojo.CommonResult;
@@ -18,9 +20,8 @@ import java.math.BigDecimal;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@Service @Service
public class AutoToolsServiceImpl implements AutoToolsService{ public class AutoToolsServiceImpl implements AutoToolsService{
@@ -74,21 +75,18 @@ public class AutoToolsServiceImpl implements AutoToolsService{
return null; return null;
} }
// 提取 zip 文件名(无扩展)
String fileNameNoExt = zipFile.getName().replaceAll("(?i)\\.zip$", ""); String fileNameNoExt = zipFile.getName().replaceAll("(?i)\\.zip$", "");
// 自动拼接系统适配路径
File extractDir = new File(zipFile.getParentFile(), fileNameNoExt); File extractDir = new File(zipFile.getParentFile(), fileNameNoExt);
if (!extractDir.exists()) extractDir.mkdirs();
if (!extractDir.exists()) { // 指定编码GBK 用于支持中文文件名UTF-8 用于通用 ZIP
extractDir.mkdirs(); try (ZipFile zf = new ZipFile(zipFile, "GBK")) {
} Enumeration<ZipArchiveEntry> entries = zf.getEntries();
while (entries.hasMoreElements()) {
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) { ZipArchiveEntry entry = entries.nextElement();
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
File outFile = new File(extractDir, entry.getName()); File outFile = new File(extractDir, entry.getName());
// 防止 Zip 路径穿越攻击 // 防止 Zip 穿越攻击
if (!outFile.getCanonicalPath().startsWith(extractDir.getCanonicalPath())) { if (!outFile.getCanonicalPath().startsWith(extractDir.getCanonicalPath())) {
throw new IOException("非法路径: " + entry.getName()); throw new IOException("非法路径: " + entry.getName());
} }
@@ -99,21 +97,22 @@ public class AutoToolsServiceImpl implements AutoToolsService{
File parent = outFile.getParentFile(); File parent = outFile.getParentFile();
if (!parent.exists()) parent.mkdirs(); if (!parent.exists()) parent.mkdirs();
try (FileOutputStream fos = new FileOutputStream(outFile)) { try (InputStream is = zf.getInputStream(entry);
OutputStream os = new FileOutputStream(outFile)) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int len; int len;
while ((len = zis.read(buffer)) > 0) { while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len); os.write(buffer, 0, len);
} }
} }
} }
zis.closeEntry();
} }
System.out.println("✅ 解压完成,目录:" + extractDir.getAbsolutePath()); System.out.println("✅ 解压完成,目录:" + extractDir.getAbsolutePath());
return extractDir.getAbsolutePath(); return extractDir.getAbsolutePath();
} catch (IOException e) { } catch (IOException e) {
System.err.println("❌ 解压出错: " + e.getMessage()); System.err.println("❌ 解压失败: " + e.getMessage());
return null; return null;
} }
} }
@@ -176,23 +175,17 @@ public class AutoToolsServiceImpl implements AutoToolsService{
if (qu_file_list == null) continue; if (qu_file_list == null) continue;
// --- 7.2、通过文件名称进行判分 // --- 7.2、通过文件名称进行判分
for (File file_one : qu_file_list) { for (File file_one : qu_file_list) {
// 判断名称 类似于 C语言程序设计。 // 判断名称 类似于 C语言程序设计。 课程+题型
if (file_one.getName().split("\\.")[0].equals(examQuestion.getCourseName()+examQuestion.getSubjectName())) { if (file_one.getName().split("\\.")[0].equals(examQuestion.getSubjectName()+examQuestion.getCourseName())) {
double c_score = judgementService.ProgrammingC(15.0, file.getPath(), file_one.getName(), examQuestion); double c_score = judgementService.ProgrammingC(15.0, file.getPath(), file_one.getName(), examQuestion);
score += c_score; score += c_score;
break; break;
} }
// wps 类型存在多级文文件夹,需要个性化设置 // wps 类型存在多级文文件夹,需要个性化设置
if (file_one.getName().equals("结果")) { if (file_one.getName().split("\\.")[0].equals("文档")) {
// 进入到学生作答得文件,直接判断文件是否存在 double wps_word_score = judgementWpsWordService.judgementWpsWord(15.0, file_one.getPath(), examQuestion);
File wps_file = new File(file_one.getPath());
File[] wps_file_list = wps_file.listFiles();
if (wps_file_list == null) break;
if (wps_file_list[0].getName().equals("结果.docx")) {
// 开始调用wps_word判分
double wps_word_score = judgementWpsWordService.judgementWpsWord(15.0, wps_file_list[0].getPath(), examQuestion);
score += wps_word_score; score += wps_word_score;
} break;
} }
} }
} }