Browse Source

解决分卷文件没有复制的相关问题

wukai 1 year ago
parent
commit
9401fd877e

+ 21 - 4
sync-in/src/main/java/com/jjt/in/service/impl/InProcessServiceImpl.java

@@ -90,15 +90,32 @@ public class InProcessServiceImpl extends InBaseService implements IInProcessSer
                 try {
                     ObjectMapper mapper = new ObjectMapper();
                     FileDesc desc = mapper.readValue(file, FileDesc.class);
-                    File zipFile = new File(ftpDir + desc.getName());
+                    File descFile = new File(ftpDir + desc.getName());
                     String md5 = "";
-                    try (InputStream is = Files.newInputStream(zipFile.toPath())) {
+                    try (InputStream is = Files.newInputStream(descFile.toPath())) {
                         md5 = DigestUtils.md5Hex(is);
                     }
 
                     if (md5.equals(desc.getMd5())) {
-                        Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
-                        Files.move(zipFile.toPath(), Paths.get(syncDir + zipFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                        //分卷zip处理
+                        if (desc.getName().endsWith(".zip")) {
+                            ZipFile zipFile = new ZipFile(descFile);
+                            if (zipFile.isValidZipFile()) {
+                                if (zipFile.isSplitArchive()) {
+                                    Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                    List<File> splitFiles = zipFile.getSplitZipFiles();
+                                    for (File f : splitFiles) {
+                                        Files.move(f.toPath(), Paths.get(syncDir + f.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                    }
+                                } else {
+                                    Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                    Files.move(descFile.toPath(), Paths.get(syncDir + descFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                }
+                            }
+                        } else {
+                            Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                            Files.move(descFile.toPath(), Paths.get(syncDir + descFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                        }
                     }
                 } catch (Exception e) {
                     e.printStackTrace();

+ 98 - 0
sync-out/src/test/java/test/ZipTest.java

@@ -0,0 +1,98 @@
+package test;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.jjt.common.constant.Constants;
+import com.jjt.common.domain.FileDesc;
+import com.jjt.common.utils.CompressZip;
+import net.lingala.zip4j.ZipFile;
+import net.lingala.zip4j.exception.ZipException;
+import org.apache.commons.codec.digest.DigestUtils;
+
+import java.io.File;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * ZipTest$
+ *
+ * @author wukai
+ * @date 2024/4/11 14:09
+ */
+public class ZipTest {
+    public static void main(String[] args) throws ZipException {
+        cpFile();
+//        String xx = "zip哈哈.zip.z01";
+//        System.err.println(xx.endsWith(".zip"));
+//        long splitSize = 20 * 1024 * 1024;
+//        ZipFile zipFile = new ZipFile("D:\\SYSTEM\\Desktop\\temp\\mongo\\test.zip");
+////        CompressZip.splitZip(new File("D:\\SYSTEM\\Desktop\\temp\\mongo\\test.mp4"), zipFile, splitSize);
+//        if (zipFile.isSplitArchive()) {
+//            System.err.println(zipFile.isValidZipFile());
+//        }
+////        System.err.println(zipFile.isSplitArchive());
+//        ZipFile test = new ZipFile("D:\\SYSTEM\\Desktop\\temp\\mongo\\test.json");
+//        System.err.println(test.isValidZipFile());
+////        System.err.println(test.isSplitArchive());
+//        zipFile.getSplitZipFiles().forEach(f->{
+//            System.err.println(f.getName());
+//        });
+
+    }
+
+    public static void cpFile() {
+        String ftpDir = "D:\\SYSTEM\\Desktop\\temp\\sync\\";
+        //获取内网同步正式目录
+        String syncDir = "D:\\SYSTEM\\Desktop\\temp\\sync\\cp\\";
+        // 创建File对象
+        File dirFile = new File(ftpDir);
+        if (dirFile.isDirectory()) {
+            //先获取目录下所有json格式文件
+            File[] files = dirFile.listFiles(((dir, name) -> name.endsWith(".json")));
+            if (files != null && files.length > 0) {
+                Arrays.sort(files, Comparator.comparing(File::getName));
+            }
+
+            for (File file : files) {
+                try {
+                    ObjectMapper mapper = new ObjectMapper();
+                    FileDesc desc = mapper.readValue(file, FileDesc.class);
+                    File descFile = new File(ftpDir + desc.getName());
+                    String md5 = "";
+                    try (InputStream is = Files.newInputStream(descFile.toPath())) {
+                        md5 = DigestUtils.md5Hex(is);
+                    }
+
+                    if (md5.equals(desc.getMd5())) {
+                        //分卷zip处理
+                        if (desc.getName().endsWith(".zip")) {
+                            ZipFile zipFile = new ZipFile(descFile);
+                            if (zipFile.isValidZipFile()) {
+                                if (zipFile.isSplitArchive()) {
+                                    Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                    List<File> splitFiles = zipFile.getSplitZipFiles();
+                                    for (File f : splitFiles) {
+                                        Files.move(f.toPath(), Paths.get(syncDir + f.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                    }
+                                } else {
+                                    Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                    Files.move(descFile.toPath(), Paths.get(syncDir + descFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                                }
+                            }
+                        } else {
+                            Files.move(file.toPath(), Paths.get(syncDir + file.getName()), StandardCopyOption.REPLACE_EXISTING);
+                            Files.move(descFile.toPath(), Paths.get(syncDir + descFile.getName()), StandardCopyOption.REPLACE_EXISTING);
+                        }
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+
+        }
+    }
+}