RenameUtil.java 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
package com.kwan.springbootkwan.utils;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * zip文件重命名
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2024/2/5 14:50
 */
public class RenameUtil {

    /**
     * 根据需要修改前缀
     */
//    private static final String PREFIX = "毕业设计&课设--";
//    private static final String PREFIX = "Matlab系列--";
//    private static final String PREFIX = "yolov8系列--";
//    private static final String PREFIX = "管理系统系列--";
    private static final String PREFIX = "【Html5系列】";
//    private static final String PREFIX = "";

    public static void main(String[] args) throws IOException {
        for (int i = 1; i < 2; i++) {
            // 请替换为你需要改名的文件夹路径
//            String folderPath = "C:\\data\\resource\\05-待上传\\0" + i;
            String folderPath = "D:\\data\\resource\\02-资源批量下载工具\\output";
//            String folderPath = "C:\\data\\resource\\04-待改名";
//        String folderPath = "/Users/qinyingjie/Downloads/资源上传/output";
            // 请替换为你敏感词大全文件位置
            String sentitivePath = "D:\\data\\vue-springboot-kwan\\src\\main\\resources\\sensitive.txt";
            Path path = Paths.get(sentitivePath);
            List<String> words = Files.readAllLines(path);
            try {
                processZipFiles(folderPath, words);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private static void processZipFiles(String folderPath, List<String> words) throws IOException {
        Files.walk(Paths.get(folderPath)).filter(Files::isRegularFile).filter(path -> path.toString().toLowerCase().endsWith(".zip")).forEach(path -> {
            try {
                processSingleZipFile(path, words);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 处理单个文件
     *
     * @param filePath
     * @param words
     * @throws IOException
     */
    private static void processSingleZipFile(Path filePath, List<String> words) throws IOException {
        String oldFileName = filePath.getFileName().toString();
        String fileName = oldFileName;
        if (!oldFileName.contains(PREFIX)) {
            fileName = PREFIX + oldFileName;
        }
        try {
            // 判断是否为敏感文件
            Boolean tag = filterSensitiveFile(words, fileName);
            if (tag) {
                try {
                    // 敏感文件
                    System.out.println("敏感文件名称:" + fileName);
                    Files.delete(filePath);
                    System.out.println("文件删除成功: " + filePath);
                } catch (IOException e) {
                    System.err.println("无法删除文件: " + filePath);
                    e.printStackTrace();
                }
                return;
            }
            String emojiRegex = "[\\x{1F600}-\\x{1F64F}\\x{1F300}-\\x{1F5FF}\\x{1F680}-\\x{1F6FF}\\x{1F700}-\\x{1F77F}\\x{1F780}-\\x{1F7FF}\\x{1F800}-\\x{1F8FF}\\x{1F900}-\\x{1F9FF}\\x{1FA00}-\\x{1FA6F}\\x{2600}-\\x{26FF}\\x{2700}-\\x{27BF}\\x{2B50}]";
            // 使用正则表达式进行匹配和替换
            fileName = removeEmoji(fileName, emojiRegex).replace("2010", "")
                    .replace("2011", "")
                    .replace("2012", "")
                    .replace("2013", "")
                    .replace("2014", "")
                    .replace("2015", "")
                    .replace("2016", "")
                    .replace("2017", "")
                    .replace("2018", "")
                    .replace("2019", "")
                    .replace("2020", "")
                    .replace("2021", "")
                    .replace("2022", "")
                    .replace("2023", "")
                    .replace("2024", "")
                    .trim();
            // Step 2: Trim file name to 64 characters
            if (fileName.length() > 64) {
                fileName = fileName.substring(0, 58) + fileName.substring(fileName.length() - 4);
            }
            if (!StringUtils.equals(oldFileName, fileName)) {
                // 获取文件所在的目录
                Path directory = filePath.getParent();
                // 构建新的文件路径
                Path newPath = directory.resolve(fileName);
                // 使用Files.move()方法进行重命名
                Files.move(filePath, newPath);
                System.out.println("文件重命名成功!");
            }
        } catch (IOException e) {
            System.out.println("文件重命名失败: " + e.getMessage());
            if (e instanceof FileAlreadyExistsException) {
                // 使用Files.move()方法进行重命名
                // 获取文件所在的目录
                Path directory = filePath.getParent();
                // 构建新的文件路径
                // 生成0到100之间的随机数
                fileName = fileName.substring(0, fileName.length() - 4) + new Random().nextInt(101) + ".zip";
                Path newPath = directory.resolve(fileName);
                try {
                    Files.move(filePath, newPath);
                    System.out.println("文件重命名成功!");
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }

    /**
     * 过滤敏感文件
     *
     * @param words
     * @param fileName
     * @return
     */
    private static Boolean filterSensitiveFile(List<String> words, String fileName) {
        if (CollectionUtil.isNotEmpty(words)) {
            for (String word : words) {
                if (fileName.contains(word)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 使用正则表达式进行匹配和替换
     *
     * @param input
     * @param emojiRegex
     * @return
     */
    private static String removeEmoji(String input, String emojiRegex) {
        Pattern pattern = Pattern.compile(emojiRegex);
        Matcher matcher = pattern.matcher(input);
        return matcher.replaceAll("");
    }
}