FileContainsUtil.java 5.5 KB
Newer Older
1 2
package com.kwan.springbootkwan.utils;

3
import cn.hutool.core.collection.CollectionUtil;
4 5 6 7 8 9 10 11
import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson2.JSON;
import com.google.common.base.Charsets;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
12
import java.util.Arrays;
13 14 15
import java.util.List;

/**
16
 * 删除博客中不存在的照片
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2023/2/8 10:45
 */
public class FileContainsUtil {
    /**
     * 默认不存在
     */
    private static boolean IS_EXIST = false;
    /**
     * 图片路径
     */
    private static final String PIC_PATH = "/Users/qinyingjie/Documents/idea-workspace/blogimg/";
    /**
32
     * 博客路径1
33
     */
34 35 36 37 38
    private static final String BLOG_FOLDER1 = "/Users/qinyingjie/Documents/idea-workspace/blog/";
    /**
     * 博客路径2
     */
    private static final String BLOG_FOLDER2 = "/Users/qinyingjie/Documents/idea-workspace/study/blog/";
39 40 41
    /**
     * 白名单
     */
42 43 44 45 46
    private static final List<String> WHITELISTS = Arrays.asList(
            "http://qinyingjie.top/blogImg/image-20230324112725149.png"
            , "http://qinyingjie.top/blogImg/logo.png"
            , "http://qinyingjie.top/blogImg/image-20230601124308164.png"
    );
47

48
    public static void main(String[] args) throws Exception {
49
        StopWatch stopWatch = new StopWatch();
50 51
        stopWatch.start("删除未用到的图片");
        //不存在的图片集合
52
        final List<String> isNotExist = new ArrayList<>();
53
        //获取所有图片名称
54 55
        final List<String> picNames = getPicName(PIC_PATH);
        System.out.println("图片总数为" + picNames.size());
56 57 58
        if (CollectionUtil.isNotEmpty(picNames)) {
            for (String picName : picNames) {
                //是白名单里面的图片,直接忽略
59
                if (WHITELISTS.contains(picName)) {
60 61 62 63 64 65 66 67
                    continue;
                }
                //默认不存在
                IS_EXIST = false;
                //指定类型的文件
                List<String> suffix = new ArrayList<>();
                suffix.add(".md");
                //包含某个字符串
68 69
                traverseFolder(BLOG_FOLDER1, suffix, picName);
                traverseFolder(BLOG_FOLDER2, suffix, picName);
70 71 72 73 74
                //文件不存在
                if (!IS_EXIST) {
                    isNotExist.add(picName);
                    deletePic(PIC_PATH + picName);
                }
75 76 77 78 79 80 81 82 83 84 85 86
            }
        }
        System.out.println("不存在图片总数为" + isNotExist.size());
        stopWatch.stop();
        //毫秒输出
        System.out.println(JSON.toJSONString(stopWatch.getTaskInfo()));
    }

    /**
     * 获取文件
     *
     * @param path
87
     * @param suffixs
88 89 90
     * @param word
     * @throws IOException
     */
91
    public static void traverseFolder(String path, List<String> suffixs, String word) throws Exception {
92 93 94 95 96 97 98 99
        File file = new File(path);
        if (file.exists()) {
            //获取文件夹下的文件
            File[] files = file.listFiles();
            if (null != files && files.length != 0) {
                for (File file2 : files) {
                    //是否是文件夹
                    if (file2.isDirectory()) {
100
                        traverseFolder(file2.getAbsolutePath(), suffixs, word);
101
                    } else {
102 103 104 105 106
                        for (String suffix : suffixs) {
                            //包含md结尾的文件
                            if (file2.getAbsolutePath().contains(suffix)) {
                                getParams(file2.getAbsolutePath(), word);
                            }
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
                        }
                    }
                }
            }
        }
    }

    /**
     * 判断文件是否存在
     *
     * @param classPath
     * @param word
     * @throws IOException
     */
    public static void getParams(String classPath, String word) throws IOException {
        File file = new File(classPath);
        //每行作为一个字符串,存为列表元素
        List<String> strings = Files.readLines(file, Charsets.UTF_8);
        for (String string : strings) {
            //判断是否包含方法名称,即指定字符串
            if (string.contains(word)) {
                //文件存在
                IS_EXIST = true;
            }
        }
    }

    /**
     * 获取图片名称
     *
     * @param path
     * @return
     */
    public static List<String> getPicName(String path) {
        List<String> picNames = new ArrayList<>();
        File file = new File(path);
        if (file.exists()) {
            //获取文件夹下的文件
            File[] files = file.listFiles();
            if (null != files && files.length != 0) {
                for (File file2 : files) {
                    //是否是文件夹
                    if (!file2.isDirectory()) {
                        //包含md结尾的文件
                        final String name = file2.getName();
                        picNames.add(name);
                    }
                }
            }
        }
        return picNames;
    }

    /**
     * 删除文件
     *
     * @param picPath
     */
    public static void deletePic(String picPath) {
        File file = new File(picPath);
        try {
            file.delete();
            System.out.printf("删除文件成功:%s%n", picPath);
        } catch (Exception e) {
            System.err.printf("无法删除的路径 %s%n%s", picPath, e);
        }
    }
}