Compare commits
	
		
			5 Commits 
		
	
	
		
			master
			...
			manual-ana
		
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								
									
								
								 | 
						d8e7045581 | 6 months ago | 
| 
							
							
								
									
								
								 | 
						1e30e94a99 | 1 year ago | 
| 
							
							
								
									
								
								 | 
						f58ab6d20e | 2 years ago | 
| 
							
							
								
									
								
								 | 
						c5c39bb23c | 2 years ago | 
| 
							
							
								
									
								
								 | 
						4b971be282 | 3 years ago | 
@ -0,0 +1,203 @@
 | 
				
			||||
package net.educoder.ecsonar.utils;
 | 
				
			||||
 | 
				
			||||
import cn.hutool.core.io.FileUtil;
 | 
				
			||||
import cn.hutool.core.util.RuntimeUtil;
 | 
				
			||||
import cn.hutool.core.util.ZipUtil;
 | 
				
			||||
 | 
				
			||||
import java.io.File;
 | 
				
			||||
import java.io.IOException;
 | 
				
			||||
import java.nio.charset.Charset;
 | 
				
			||||
import java.nio.file.*;
 | 
				
			||||
import java.nio.file.attribute.BasicFileAttributes;
 | 
				
			||||
 | 
				
			||||
/**
 | 
				
			||||
 * @Author: youys
 | 
				
			||||
 * @Date: 2023/5/19
 | 
				
			||||
 * @Description:
 | 
				
			||||
 */
 | 
				
			||||
public class RarUtil {
 | 
				
			||||
 | 
				
			||||
    public static void unrar(String sourceFilePath, String outputDirectory) {
 | 
				
			||||
        String s = RuntimeUtil.execForStr("unar", sourceFilePath,"-o", outputDirectory);
 | 
				
			||||
        System.out.println("result====" + s);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
    public static void un7Z(String sourceFilePath, String outputDirectory) {
 | 
				
			||||
        String s = RuntimeUtil.execForStr("unar", sourceFilePath,"-o", outputDirectory);
 | 
				
			||||
        System.out.println("result====" + s);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
    public static void main(String[] args) throws Exception{
 | 
				
			||||
        //        unrar("/tmp/20230301/201905962222/20230519153409_wuiol3vn.rar", "/Users/youyongsheng/Desktop/aa");
 | 
				
			||||
//        un7Z("/tmp/20230301/201905962241/20230519153506_i39sv5p2.7z", "/Users/youyongsheng/Desktop/aa");
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
        String path = "/tmp/202505142";
 | 
				
			||||
//        unzip();
 | 
				
			||||
//            valid(path);
 | 
				
			||||
//        removeFullCode(path);
 | 
				
			||||
        removeDirectory(Paths.get(path));
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
    public static void unzip(){
 | 
				
			||||
        String sourceDir = "/tmp/202505141";
 | 
				
			||||
        String targetDir = "/tmp/202505142";
 | 
				
			||||
 | 
				
			||||
        File directory = new File(sourceDir);
 | 
				
			||||
        File[] studentDirs = directory.listFiles();
 | 
				
			||||
        if (studentDirs == null) {
 | 
				
			||||
            System.out.println("No student directories found.");
 | 
				
			||||
            return;
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        int i=0;
 | 
				
			||||
        for (File studentDir : studentDirs) {
 | 
				
			||||
            if (!studentDir.isDirectory()) {
 | 
				
			||||
                System.out.println("111--------------------------------");
 | 
				
			||||
                continue;
 | 
				
			||||
            }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
            String studentId = studentDir.getName();
 | 
				
			||||
            String studentTargetDir = targetDir + "/" + studentId;
 | 
				
			||||
 | 
				
			||||
            File[] zipFiles = studentDir.listFiles();
 | 
				
			||||
//            File[] zipFiles = studentDir.listFiles((dir, name) -> name.endsWith(".zip"));
 | 
				
			||||
//            if (zipFiles == null) {
 | 
				
			||||
//                System.out.println("No zip files found for student: " + studentId);
 | 
				
			||||
//                continue;
 | 
				
			||||
//            }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
            for (File zipFile : zipFiles) {
 | 
				
			||||
                try {
 | 
				
			||||
                    if(zipFile.getName().endsWith(".zip")){
 | 
				
			||||
                        unzip(zipFile.getAbsolutePath(), studentTargetDir);
 | 
				
			||||
                    }else if(zipFile.getName().endsWith(".rar")){
 | 
				
			||||
                        unRar(zipFile.getAbsolutePath(), studentTargetDir);
 | 
				
			||||
                    }else{
 | 
				
			||||
                        RarUtil.un7Z(zipFile.getAbsolutePath(), studentTargetDir);
 | 
				
			||||
                    }
 | 
				
			||||
                } catch (Exception e) {
 | 
				
			||||
                    System.out.println("Error occurred while unzipping file: " + zipFile.getName());
 | 
				
			||||
                }
 | 
				
			||||
            }
 | 
				
			||||
 | 
				
			||||
            System.out.println("--------------------------------");
 | 
				
			||||
        }
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
    private static void unzip(String zipFilePath, String targetDir) throws IOException {
 | 
				
			||||
        System.out.println(zipFilePath + " to " + targetDir);
 | 
				
			||||
        ZipUtil.unzip(zipFilePath,targetDir, Charset.forName("GBK"));
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
    private static void unRar(String zipFilePath, String targetDir) throws Exception {
 | 
				
			||||
        System.out.println(zipFilePath + " to " + targetDir);
 | 
				
			||||
        RarUtil.unrar(zipFilePath,targetDir);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
    public static void valid(String path){
 | 
				
			||||
        File directory = new File(path);
 | 
				
			||||
        File[] studentDirs = directory.listFiles();
 | 
				
			||||
        if (studentDirs == null) {
 | 
				
			||||
            System.out.println("No student directories found.");
 | 
				
			||||
            return;
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        for (File studentDir : studentDirs) {
 | 
				
			||||
            if (studentDir.isDirectory()) {
 | 
				
			||||
 | 
				
			||||
                File[] files = studentDir.listFiles();
 | 
				
			||||
                if(files == null || files.length == 0){
 | 
				
			||||
                    System.out.println(studentDir.getName()+" -> null");
 | 
				
			||||
                }
 | 
				
			||||
                for (File file : files) {
 | 
				
			||||
                    System.out.println(studentDir.getName()+" ->" + file.getName());
 | 
				
			||||
                }
 | 
				
			||||
                System.out.println("--------------------------------");
 | 
				
			||||
            }
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
    public static void removeFullCode(String path){
 | 
				
			||||
        // 删除完整代码
 | 
				
			||||
        File directory = new File(path);
 | 
				
			||||
        File[] studentDirs = directory.listFiles();
 | 
				
			||||
        if (studentDirs == null) {
 | 
				
			||||
            System.out.println("No student directories found.");
 | 
				
			||||
            return;
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        for (File studentDir : studentDirs) {
 | 
				
			||||
            if (studentDir.isDirectory()) {
 | 
				
			||||
 | 
				
			||||
                String stuNo = studentDir.getName();
 | 
				
			||||
                File[] files = studentDir.listFiles();
 | 
				
			||||
                if(files.length == 2){
 | 
				
			||||
                    for (File file : files) {
 | 
				
			||||
                        if (file.getName().contains("完整")) {
 | 
				
			||||
                            System.out.println(stuNo + "==11===" + file.getAbsolutePath());
 | 
				
			||||
                            cn.hutool.core.io.FileUtil.del(file.getAbsolutePath());
 | 
				
			||||
                        }
 | 
				
			||||
                    }
 | 
				
			||||
 | 
				
			||||
                }else if(files.length == 1){
 | 
				
			||||
                    File file = files[0];
 | 
				
			||||
                    File[] files1 = file.listFiles();
 | 
				
			||||
                    if(files1 != null && files1.length == 2){
 | 
				
			||||
                        for (File f : files1) {
 | 
				
			||||
                            if (f.getName().contains("完整")) {
 | 
				
			||||
                                System.out.println(stuNo + "==22===" + f.getAbsolutePath());
 | 
				
			||||
                                FileUtil.del(f.getAbsolutePath());
 | 
				
			||||
                            }
 | 
				
			||||
                        }
 | 
				
			||||
 | 
				
			||||
                    }
 | 
				
			||||
                }
 | 
				
			||||
 | 
				
			||||
            }
 | 
				
			||||
        }
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
    private static void removeDirectory(Path rootPath) throws IOException {
 | 
				
			||||
        Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
 | 
				
			||||
            @Override
 | 
				
			||||
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
 | 
				
			||||
                String folderName = dir.getFileName().toString();
 | 
				
			||||
                if (folderName.equals("node_modules") || folderName.equals("target") || folderName.equals("dist")) {
 | 
				
			||||
                    System.out.println("Deleting directory: " + dir.toAbsolutePath());
 | 
				
			||||
                    deleteDirectoryRecursively(dir);
 | 
				
			||||
 | 
				
			||||
                    return FileVisitResult.SKIP_SUBTREE; // 不再进入已删除目录
 | 
				
			||||
                }
 | 
				
			||||
                return FileVisitResult.CONTINUE;
 | 
				
			||||
            }
 | 
				
			||||
        });
 | 
				
			||||
 | 
				
			||||
    }
 | 
				
			||||
    // 递归删除目录
 | 
				
			||||
    private static void deleteDirectoryRecursively(Path path) throws IOException {
 | 
				
			||||
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
 | 
				
			||||
            @Override
 | 
				
			||||
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
 | 
				
			||||
                Files.deleteIfExists(file);
 | 
				
			||||
                return FileVisitResult.CONTINUE;
 | 
				
			||||
            }
 | 
				
			||||
 | 
				
			||||
            @Override
 | 
				
			||||
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
 | 
				
			||||
                Files.deleteIfExists(dir);
 | 
				
			||||
                return FileVisitResult.CONTINUE;
 | 
				
			||||
            }
 | 
				
			||||
        });
 | 
				
			||||
    }
 | 
				
			||||
}
 | 
				
			||||
											
												Binary file not shown.
											
										
									
								
					Loading…
					
					
				
		Reference in new issue