Bladeren bron

添加解压功能(暂未实现完成,先完成其他任务)

wzl 6 maanden geleden
bovenliggende
commit
de6363d3dc

+ 6 - 0
pom.xml

@@ -120,6 +120,12 @@
             <version>1.0.3</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.github.junrar</groupId>
+            <artifactId>junrar</artifactId>
+            <version>7.4.0</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 21 - 0
src/main/java/com/dskj/znzn/importData/executor/utils/ReadWriteDir.java

@@ -0,0 +1,21 @@
+package com.dskj.znzn.importData.executor.utils;
+
+import java.io.File;
+import java.nio.file.Files;
+
+public class ReadWriteDir {
+
+    public static boolean dirExists(String dirPath) {
+        //验证文件夹是否存在
+        return Files.exists(new File(dirPath).toPath());
+    }
+
+
+    public static boolean createDir(String path) {
+        if (dirExists((path))) {
+            return true;
+        }
+        //创建文件夹
+        return new File(path).mkdirs();
+    }
+}

+ 29 - 0
src/main/java/com/dskj/znzn/importData/executor/utils/UnrarFile.java

@@ -0,0 +1,29 @@
+package com.dskj.znzn.importData.executor.utils;
+
+import com.github.junrar.Archive;
+import com.github.junrar.exception.RarException;
+import com.github.junrar.rarfile.FileHeader;
+
+import java.io.*;
+
+public class UnrarFile {
+
+    public static void unrar(String rarFilePath, String destDirectory) {
+        try (Archive archive = new Archive(new File(rarFilePath))) {
+            FileHeader fileHeader;
+            while ((fileHeader = archive.nextFileHeader()) != null) {
+                String fileName = fileHeader.getFileNameString().trim();
+                String filePath = destDirectory + File.separator + fileName;
+                if (fileHeader.isDirectory()) {
+                    new File(filePath).mkdirs();
+                } else {
+                    try (FileOutputStream fos = new FileOutputStream(filePath)) {
+                        archive.extractFile(fileHeader, fos);
+                    }
+                }
+            }
+        } catch (IOException | RarException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 37 - 0
src/main/java/com/dskj/znzn/importData/executor/utils/UnzipFile.java

@@ -0,0 +1,37 @@
+package com.dskj.znzn.importData.executor.utils;
+
+import java.io.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class UnzipFile {
+
+    public static void unzip(String zipFilePath, String destDirectory) {
+        try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
+            ZipEntry entry = zipIn.getNextEntry();
+            while (entry != null) {
+                String filePath = destDirectory + File.separator + entry.getName();
+                if (!entry.isDirectory()) {
+                    extractFile(zipIn, filePath);
+                } else {
+                    File dir = new File(filePath);
+                    dir.mkdirs();
+                }
+                zipIn.closeEntry();
+                entry = zipIn.getNextEntry();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
+        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
+            byte[] bytesIn = new byte[4096];
+            int read = 0;
+            while ((read = zipIn.read(bytesIn)) != -1) {
+                bos.write(bytesIn, 0, read);
+            }
+        }
+    }
+}

+ 31 - 0
src/main/java/com/dskj/znzn/importData/executor/utils/UzipAndCopyToTmp.java

@@ -0,0 +1,31 @@
+package com.dskj.znzn.importData.executor.utils;
+
+import java.io.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class UzipAndCopyToTmp {
+
+
+    public static void unzipRecursive(String readDir, String destDirectory) {
+        File destDir = new File(readDir);
+        File[] files = destDir.listFiles();
+        if (files != null) {
+            for (File file : files) {
+                if (file.isFile() && (file.getName().endsWith(".zip") || file.getName().endsWith(".rar") || file.getName().endsWith(".tar"))) {
+                    String newDestDirectory = destDirectory + File.separator + file.getName().replaceAll("\\.[^.]+$", "");
+                    new File(newDestDirectory).mkdirs();
+                    if (file.getName().endsWith(".zip")) {
+                        unzipRecursive(file.getAbsolutePath(), newDestDirectory);
+                    }
+//                    else if (file.getName().endsWith(".rar")) {
+//                        unrar(file.getAbsolutePath(), newDestDirectory);
+//                    } else if (file.getName().endsWith(".tar")) {
+//                        untar(file.getAbsolutePath(), newDestDirectory);
+//                    }
+                }
+            }
+        }
+    }
+
+}

+ 35 - 0
src/test/java/TestReadBin.java

@@ -0,0 +1,35 @@
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+public class TestReadBin {
+
+
+    public static void main(String[] args) {
+        String filePath = "C:\\Users\\Administrator\\Documents\\WeChat Files\\anmox-\\FileStorage\\File\\2024-11\\11.bin";
+
+        try (FileInputStream fis = new FileInputStream(filePath)) {
+            while (fis.available() > 0) {
+                // 读取部分长度(4字节整数)
+                byte[] lengthBytes = new byte[4];
+                fis.read(lengthBytes);
+                int length = ByteBuffer.wrap(lengthBytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
+
+                // 读取部分数据
+                byte[] data = new byte[length];
+                fis.read(data);
+
+                System.out.println("部分长度: " + length);
+                for (byte b : data) {
+                    System.out.printf("0x%02X ", b);
+                }
+                System.out.println();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}
+