From 18b58c03052be9b8ab82e9ed9d075d80139b1f0f Mon Sep 17 00:00:00 2001 From: 13405411873 <1994398261@qq.com> Date: Mon, 31 Mar 2025 22:57:50 +0800 Subject: [PATCH] =?UTF-8?q?bug=20=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-admin/pom.xml | 19 ++- .../impl/PatientScriptServiceImpl.java | 111 ++++++++++++++---- 2 files changed, 106 insertions(+), 24 deletions(-) diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index 50cedab..7de9a9b 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -21,11 +21,24 @@ commons-compress 1.21 + - com.github.junrar - junrar - 7.4.0 + org.tukaani + xz + 1.5 + + + net.sf.sevenzipjbinding + sevenzipjbinding + 16.02-2.01 + + + net.sf.sevenzipjbinding + sevenzipjbinding-all-platforms + 16.02-2.01 + + org.apache.ant ant diff --git a/ruoyi-admin/src/main/java/com/ruoyi/script/service/impl/PatientScriptServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/script/service/impl/PatientScriptServiceImpl.java index c85a398..313148f 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/script/service/impl/PatientScriptServiceImpl.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/script/service/impl/PatientScriptServiceImpl.java @@ -3,9 +3,7 @@ package com.ruoyi.script.service.impl; import cn.hutool.core.date.DateUtil; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.data.Pictures; -import com.github.junrar.Archive; -import com.github.junrar.exception.RarException; -import com.github.junrar.rarfile.FileHeader; + import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfChunk; @@ -18,8 +16,16 @@ import com.ruoyi.script.service.PatientScriptService; import com.ruoyi.script.util.ShellUtil; import com.ruoyi.script.util.Word2PdfUtil; import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import net.sf.sevenzipjbinding.ExtractOperationResult; +import net.sf.sevenzipjbinding.IInArchive; +import net.sf.sevenzipjbinding.SevenZip; +import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; +import net.sf.sevenzipjbinding.simple.ISimpleInArchive; +import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; @@ -48,6 +54,7 @@ import static com.ruoyi.system.util.pdfUtil.PdfUtil.getImageFromInputStream; * @since 2023-06-29 09:47:24 */ @Service("patientScriptService") +@Slf4j public class PatientScriptServiceImpl implements PatientScriptService { @Autowired private PatientScriptMapper scriptMapper; @@ -96,33 +103,86 @@ public class PatientScriptServiceImpl implements PatientScriptService { } - public static void unrar(String rarFilePath, String destDir) throws IOException, RarException { - File rarFile = new File(rarFilePath); - File destDirectory = new File(destDir); + /** + * rar解压缩 + * @param rarFile rar文件的全路径 + * @param outPath 解压路径 + * @return 压缩包中所有的文件 + */ + private static String unRar(String rarFile, String outPath, String passWord) { + RandomAccessFile randomAccessFile = null; + IInArchive inArchive = null; + try { + // 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile + randomAccessFile = new RandomAccessFile(rarFile, "r"); + if (StringUtils.isNotBlank(passWord)) { + inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord); + } else { + inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile)); + } - if (!destDirectory.exists()) { - destDirectory.mkdirs(); - } + ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface(); + for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { + final int[] hash = new int[]{0}; + if (!item.isFolder()) { + ExtractOperationResult result; + final long[] sizeArray = new long[1]; - try (Archive archive = new Archive(rarFile)) { - FileHeader fileHeader; - while ((fileHeader = archive.nextFileHeader()) != null) { - if (!fileHeader.isDirectory()) { - File outFile = new File(destDirectory, fileHeader.getFileNameString()); + File outFile = new File(outPath + File.separator+ item.getPath()); File parent = outFile.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); + if ((!parent.exists()) && (!parent.mkdirs())) { + continue; } - try (OutputStream out = new FileOutputStream(outFile)) { - archive.extractFile(fileHeader, out); - } catch (IOException e) { - throw new RuntimeException("解压文件时出错: " + fileHeader.getFileNameString(), e); + if (StringUtils.isNotBlank(passWord)) { + result = item.extractSlow(data -> { + try { + IOUtils.write(data, new FileOutputStream(outFile, true)); + } catch (Exception e) { + e.printStackTrace(); + } + hash[0] ^= Arrays.hashCode(data); // Consume data + sizeArray[0] += data.length; + return data.length; // Return amount of consumed + }, passWord); + } else { + result = item.extractSlow(data -> { + try { + IOUtils.write(data, new FileOutputStream(outFile, true)); + } catch (Exception e) { + e.printStackTrace(); + } + hash[0] ^= Arrays.hashCode(data); // Consume data + sizeArray[0] += data.length; + return data.length; // Return amount of consumed + }); + } + + if (result == ExtractOperationResult.OK) { + log.error("解压rar成功...." + String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath())); + } else if (StringUtils.isNotBlank(passWord)) { + log.error("解压rar成功:密码错误或者其他错误...." + result); + return "password"; + } else { + return "rar error"; } } } + + } catch (Exception e) { + log.error("unRar error", e); + return e.getMessage(); + } finally { + try { + inArchive.close(); + randomAccessFile.close(); + } catch (Exception e) { + e.printStackTrace(); + } } + return ""; } + @Override public Boolean save(PatientScript patientScript) throws IOException, InterruptedException { patientScript.setCreateId(1110L); @@ -143,7 +203,7 @@ public class PatientScriptServiceImpl implements PatientScriptService { if (patientScript.getFilePath().endsWith(".zip")){ unzip(patientScript.getFilePath(),"/data/" +format+"/"+ playground+"/个体数据"); }else if (patientScript.getFilePath().endsWith(".rar")){ - unrar(patientScript.getFilePath(),"/data/" +format+"/"+ playground+"/个体数据"); + unRar(patientScript.getFilePath(),"/data/" +format+"/"+ playground+"/个体数据",null); } Map shMap = new LinkedHashMap<>(); shMap.put("outPath",format+"/output-"+unique_id); @@ -1135,5 +1195,14 @@ public class PatientScriptServiceImpl implements PatientScriptService { } + public static void main(String[] args) { + try { + unRar("D:\\elevator-web\\WX-mb.rar", "D:\\elevator-web\\output",null); + System.out.println("解压成功!"); + } catch (Exception e) { + e.printStackTrace(); + } + } + }