【修改】 解压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>
<version>12.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.0</version> <!-- 或使用最新版本 -->
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>pc.exam.gg</groupId>

View File

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