ReactNative图片引用扫描

App的图片越来越多,发现很多,这时候需要对图片进行扫描,剔除无效的图片。实现思路很简单:1.扫描js文件;2.查找png文件,确认被引用的图片;3.删除不在集合的图片。代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * flow:{
 * 1.查找所有的js文件;
 * 2.查找png文件,确认被引用的图片;
 * 3.存入集合(不重复);
 * 4.删除不在集合的图片;
 * }
 */
public class LintImage implements PatternPng.FindCallback {

    public static void main(String[] args) {

//        LintImage lintImage = new LintImage(
//                "E:\\Working3\\Air\\app\\Module\\air_purifier_131",
//                "E:\\Working3\\Air\\app\\Module\\air_purifier_131\\Components\\Images",
//                false);

        if (args.length == 3) {
            new LintImage(args[0], args[1], Boolean.valueOf(args[2]));
        } else {
            new LintImage(args[0], args[1], false);
        }

//        System.out.println(Boolean.valueOf("true"));

    }

    private String projectPath;
    private String projectImagesPath;
    private boolean delete = false;

    private ConcurrentSkipListSet<String> allImagesMap;

    private List<String> allJSFiles = new ArrayList<>();
    public static AtomicInteger counter = new AtomicInteger(0);
    private ExecutorService threadPool = Executors.newFixedThreadPool(5);

    public LintImage(String projectPath, String projectImagesPath, boolean delete) {
        this.projectPath = projectPath;
        this.projectImagesPath = projectImagesPath;
        this.delete = delete;

        allImagesMap = new ConcurrentSkipListSet<>();

        System.out.println("\n\n");
        System.out.println("==========扫描js文件===========");
        //获取js文件
        getJSFiles(this.projectPath);

        counter.set(this.allJSFiles.size());

        System.out.println("\n\n");
        System.out.println("===========查找png===========");
        //读取文件,查找png
        for (String path : allJSFiles) {
            String doc = readJSFile(path);
            PatternPng patternPng = new PatternPng(doc, this);
            threadPool.execute(patternPng);
        }

        threadPool.shutdown();
    }

    private void getJSFiles(String path) {
        File parent = new File(path);
        if (parent.exists()) {
            File[] files = parent.listFiles();
            for (File f : files) {
                if (f.isFile()) {
                    if (f.getName().endsWith(".js") || f.getName().endsWith(".ccs")) {
                        allJSFiles.add(f.getAbsolutePath());
                        System.out.println("add file:\t" + f.getAbsolutePath());
                    }
                } else {
                    getJSFiles(f.getPath());
                }
            }
        } else {
            System.err.println("parent path not exits!");
        }
    }

    public String readJSFile(String path) {
        StringBuffer content = new StringBuffer();
        File jsFile = new File(path);
        if (jsFile.exists()) {
            try {
                FileInputStream inputStream = new FileInputStream(jsFile);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(buffer)) != -1) {
                    content.append(new String(buffer, 0, len));
                }
                inputStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content.toString();
    }

    @Override
    public void onTarget(String png) {
        System.out.println("find png:\t" + png);
        if (!allImagesMap.contains(png)) {
            allImagesMap.add(png);
        }
    }

    @Override
    public void onFilter() {
        filter();
    }

    //过滤不在集合的图片
    private void filter() {
        System.out.println("\n\n");
        System.out.println("==========需要删除的图片=========");

        //进行images下的图片进行操作
        File sourceImage = new File(this.projectImagesPath);
        if (sourceImage.exists()) {
            File[] fs = sourceImage.listFiles();

            for (File item : fs) {
                if (item.getName().endsWith(".png") || item.getName().endsWith(".PNG")) {
                    if (!allImagesMap.contains(item.getName())) {
                        if (this.delete) {
                            System.out.println("delete file:\t" + item.getAbsolutePath());
                            item.deleteOnExit();
                        } else {
                            System.out.println("should delete:\t" + item.getAbsolutePath());
                        }
                    }
                }

            }
        } else {
            System.err.println("file not exits:" + this.projectImagesPath);
        }
    }

}

class PatternPng implements Runnable {

    private String doc;
    private FindCallback callback;

    PatternPng(String doc, FindCallback callback) {
        this.doc = doc;
        this.callback = callback;
    }

    private void clear() {
        this.doc = null;
    }

    @Override
    public void run() {
        Pattern pattern = Pattern.compile("[\"'].[^:\\s,\"']*\\.png[\"']");
        Matcher matcher = pattern.matcher(this.doc);

        while (matcher.find()) {
            String target = doc.substring(matcher.start(), matcher.end());
            if (this.callback != null) {
                target = target.substring(target.lastIndexOf("/") + 1, target.length() - 1);
                this.callback.onTarget(target);
            }
        }

        if (LintImage.counter.decrementAndGet() == 0) {
            this.callback.onFilter();
        }
    }

    interface FindCallback {

        void onTarget(String png);

        void onFilter();
    }
}

java -jar LintImage.jar project_path image_path ture/false
打包成jar,大家可以下载用用
http://download.csdn.net/download/u012131702/10194721

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值