文件处理工具类

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.params.HttpParams;
import org.eclipse.jgit.api.Git;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import static org.springframework.util.FileCopyUtils.BUFFER_SIZE;

@Slf4j
public class FileUtils {

    private static Integer id = 0;

    private static final String WORK_PATH = File.separator + "root" + File.separator + "work";

    public static String getGitPath(String rootPath, String aimsPath) {
        String gitPath = "";
        Map<String, String> map = new HashMap<>();
        try {
            getFilePath(rootPath, map, aimsPath);
            gitPath = map.get(aimsPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return gitPath;
    }

    public static void getFilePath(String rootPath, Map<String, String> map, String aimsPath) throws FileNotFoundException {
//        log.info("rootPath:" + rootPath);
        File file = new File(rootPath);
        //1.判断文件
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }
        if (file.isDirectory()) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            String temp = path.substring(path.length() - aimsPath.length());
            if (aimsPath.equals(temp)) {
                String gitPath = path.replace(aimsPath, "");
                try {
                    Git.open(new File(gitPath));
                    map.put(name, gitPath);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String[] lists = file.list();
            for (int i = 0; i < lists.length; i++) {
                String s = lists[i];
                String newFilePath = path + File.separator + s;//根据当前文件夹,拼接其下文文件形成新的路径
                getFilePath(newFilePath, map, aimsPath);
            }
        }
    }


    public static Tree bachFile(String filepath, int parentId) throws FileNotFoundException {
        File file = new File(filepath);
        Tree tree = new Tree();
        //1.判断文件
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }
        //2.是文件该怎么执行
        if (file.isFile() && !file.isHidden()) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            tree.setId(id++);
            tree.setName(name);
            tree.setPath(path);
            tree.setLeaf(true);
            tree.setParentId(parentId);
        }
        return tree;
    }

    public static List<Tree> bachFolderUpLevel(String filepath, Integer parentId, List<String> filePathList) throws FileNotFoundException {
        Integer sunId = parentId;
        File file = new File(filepath);
        List<Tree> sonList = new ArrayList<>();
        //1.判断文件
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }
        //2.是文件该怎么执行
        if (file.isFile() && !file.isHidden()) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            Tree tree = new Tree(sunId++, name, path, parentId);
            for (String filePath : filePathList) {
                if (StringUtils.contains(filePath, name)) {
                    tree.setDefect(true);
                    break;
                }
            }
            sonList.add(tree);
        }
        //3.获取文件夹路径下面的文件
        if (file.isDirectory() && !file.isHidden() && !".git".equals(file.getName()) && !".idea".equals(file.getName())) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            String[] lists = file.list();
            for (int i = 0; i < lists.length; i++) {
                String s = lists[i];
                String newFilePath = path + File.separator + s;
                Tree tree = new Tree(sunId++, name, newFilePath, parentId);
                sonList.add(tree);
            }
        }
        return sonList;
    }

    public static Boolean checkFilePrefix(File file) {
        Boolean flag = true;
        String fileName = file.getName();
        String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "zip") || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "tar") || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "gz")
                || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "tgz") || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "bz2") || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "tbz2")
                || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "rar") || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "jar") || org.eclipse.jgit.util.StringUtils.equalsIgnoreCase(prefix, "wat")) {
            flag = false;
        }
        return flag;
    }

//    public static void file(String filepath, int parentId, List<Tree> list, List<String> filePathList) throws FileNotFoundException {
//        ArrayList<Tree> parentNode = new ArrayList<>();
//        file(filepath, parentId, list, filePathList, parentNode);
//    }

    public static Boolean file(String filepath, int parentId, List<Tree> list, List<String> filePathList) throws FileNotFoundException {
        File file = new File(filepath);
        Boolean ifDefect = false;
//        ArrayList<Tree> newParentNode =(ArrayList<Tree>) parentNode.clone();
        //1.判断文件
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }
        //2.是文件该怎么执行
        if (file.isFile() && !file.isHidden() && checkFilePrefix(file)) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            Tree tree = new Tree(id++, name, path, parentId);
            for (String filPath : filePathList) {
                if (StringUtils.contains(filPath, name)) {
                    tree.setDefect(true);
                    ifDefect = true;
                }
            }
            tree.setLeaf(true);
            list.add(tree);
        }
        //3.获取文件夹路径下面的所有文件递归调用;
        if (file.isDirectory() && !file.isHidden() && !".git".equals(file.getName()) && !".idea".equals(file.getName()) && !"target".equals(file.getName())) {
            log.info("fileName===" + file.getName());
            String name = file.getName();
            String path = file.getAbsolutePath();
            Tree tree = new Tree(id++, name, path, parentId);
            list.add(tree);
            String[] lists = file.list();
            String parent = file.getParent();
            for (int i = 0; i < lists.length; i++) {
                String s = lists[i];
                String newFilePath = path + File.separator + s;//根据当前文件夹,拼接其下文文件形成新的路径
                Boolean childIfDefect = file(newFilePath, tree.getId(), list, filePathList);
                if (childIfDefect) {
                    ifDefect = true;
                }
            }
            tree.setDefect(ifDefect);
        }
        return ifDefect;
    }

    /**
     * - listToTree
     * - <p>方法说明<p>
     * - 将JSONArray数组转为树状结构
     * - @param arr 需要转化的数据
     * - @param id 数据唯一的标识键值
     * - @param pid 父id唯一标识键值
     * - @param children 子节点键值
     * - @return JSONArray
     */
    public static JSONArray listToTree(JSONArray arr, String id, String pid, String children) {
        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        //将数组转为Object的形式,key为数组中的id
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(id), json);
        }
        //遍历结果集
        for (int j = 0; j < arr.size(); j++) {
            //单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            //在hash中取出key为单条记录中pid的值
            JSONObject hashVP = (JSONObject) hash.get(aVal.get(pid).toString());
            //如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
            if (hashVP != null) {
                //检查是否有child属性
                if (hashVP.get(children) != null) {
                    JSONArray ch = (JSONArray) hashVP.get(children);
                    ch.add(aVal);
                    hashVP.put(children, ch);
                } else {
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put(children, ch);
                }
            } else {
                r.add(aVal);
            }
        }
        return r;
    }


    public static List<File> getAllFile(String filePath) {
        File f = new File(filePath);
        File[] fileInF = f.listFiles(); // 得到f文件夹下面的所有文件。
        List<File> list = new ArrayList<>();
        for (File file : fileInF) {
            list.add(file);
        }
        return list;
    }

    public static String readTxt(String filePath) throws IOException {
        StringBuilder result = new StringBuilder();
        BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "UTF-8"));
        String lineTxt = null;
        while ((lineTxt = bfr.readLine()) != null) {
            result.append(lineTxt).append("
");
        }
        bfr.close();
        return result.toString();
    }

    /**
     * 下载文件
     *
     * @param url 下载地址
     * @return 本地保存地址
     */
    private static boolean download(String url, String fileName) {
        String[] instructions = new String[3];
        instructions[1] = "-c";

        String osName = System.getProperty("os.name");
        // 判断系统是否为windows操作系统
        String systemName = "Windows";
        if (osName.contains(systemName)) {
            instructions[0] = "bash";
        } else {
            instructions[0] = "sh";
        }
        String instruction = String.format("wget -q %s -O %s && echo $?", url, fileName);
        instructions[2] = instruction;
        String execute = ExecuteScriptUtils.execute(instructions);
        return "0".equals(execute);
    }

    /**
     * 解压文件
     *
     * @param filePath        文件地址
     * @param destDirPath     目标路径
     * @param scanApplication 文件名称
     * @return
     */
    public static Boolean unZipOpt(String filePath, String destDirPath, String scanApplication) {
        String[] instructions = new String[3];
        instructions[1] = "-c";
        String outPath = scanApplication == null ? destDirPath : destDirPath + scanApplication + File.separator;
        String osName = System.getProperty("os.name");
        // 判断系统是否为windows操作系统
        String systemName = "Windows";
        if (osName.contains(systemName)) {
            instructions[0] = "bash";
        } else {
            instructions[0] = "sh";
        }
        String instruction = String.format("unzip -o %s -d %s && echo $?", filePath, outPath);
        instructions[2] = instruction;
        String execute = ExecuteScriptUtils.execute(instructions);
        return "0".equals(execute);
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static Boolean deletefile(String filePath) {
        String[] instructions = new String[3];
        instructions[1] = "-c";

        String osName = System.getProperty("os.name");
        // 判断系统是否为windows操作系统
        String systemName = "Windows";
        if (osName.contains(systemName)) {
            instructions[0] = "bash";
        } else {
            instructions[0] = "sh";
        }
        String instruction = String.format("rm -rf %s && echo $?", filePath);
        instructions[2] = instruction;
        String execute = ExecuteScriptUtils.execute(instructions);
        return "0".equals(execute);
    }

    public static Boolean unZip(File srcFile, String destDirPath, String scanApplication) throws RuntimeException {
        Boolean flag = false;
        long start = System.currentTimeMillis();
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
        }
        // 开始解压
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(srcFile, Charset.forName("GBK"));
            Enumeration<?> entries = zipFile.entries();
            if (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                log.info("rootFile:" + entry.getName() + "scanApplication:" + scanApplication + File.separator);
                String name = entry.getName().substring(0, entry.getName().length() - 1);
                /*if (!StringUtils.equals(scanApplication, name)) {
                    log.info("zip包中 不带服务名 文件夹的: " + scanApplication);
                    destDirPath = destDirPath + File.separator + scanApplication;
                }*/
            }
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
//                log.info("解压" + entry.getName());
                // 如果是文件夹,就创建个文件夹
                if (entry.isDirectory()) {
                    String dirPath = destDirPath + "/" + entry.getName();
                    File dir = new File(dirPath);
                    dir.mkdirs();
                } else {
                    // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                    File targetFile = new File(destDirPath + "/" + entry.getName());
                    // 保证这个文件的父文件夹必须要存在
                    if (!targetFile.getParentFile().exists()) {
                        targetFile.getParentFile().mkdirs();
                    }
                    targetFile.createNewFile();
                    // 将压缩文件内容写入到这个文件中
                    InputStream is = zipFile.getInputStream(entry);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    int len;
                    byte[] buf = new byte[BUFFER_SIZE];
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    // 关流顺序,先打开的后关闭
                    fos.close();
                    is.close();
                }
            }
            long end = System.currentTimeMillis();
            log.info("解压完成,耗时:" + (end - start) + " ms");
            flag = true;
        } catch (Exception e) {
            log.error("下载的文件格式不正确,将文件删除");
            e.printStackTrace();
            CodeLineQuantityUtils.delFolder(destDirPath);
            flag = false;
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }

    /**
     * 下载文件
     *
     * @param url 下载地址
     * @return 本地保存地址
     */
    public static boolean downloadBack(String url, String fileName) {
        HttpClient client = new HttpClient();
        GetMethod get = null;
        FileOutputStream output = null;
        String localFileName = "";
        Boolean result = false;
        try {
            get = new GetMethod(url);
            int i = client.executeMethod(get);
            localFileName = fileName;
            if (200 == i) {
                File storeFile = new File(localFileName);
                output = new FileOutputStream(storeFile);
                output.write(get.getResponseBody());
                result = true;
            } else {
                log.info("DownLoad file occurs exception, the error code is :" + i);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            assert get != null;
            get.releaseConnection();
            client.getHttpConnectionManager().closeIdleConnections(0);
        }
        return result;
    }

    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        return readFileContent(file);
    }

    public static String readFileContent(File file) {
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
            reader = new BufferedReader(inputReader);
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr + "
");
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

    public static Boolean downLoadAndUnZipFile(String scanApplication, String newRepo, String codePath, String fileName) {
        Boolean downloadFlag = FileUtils.download(newRepo, codePath + fileName);
        Boolean unZipFlag = false;
        String filePath = codePath + fileName;
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (downloadFlag && new File(filePath).exists()) {
                log.info("下载 zip成功 开始解压");
//                File file1 = new File(filePath);
//                unZipFlag = FileUtils.unZip(file1, codePath, scanApplication);
                unZipFlag = unZipOpt(filePath, codePath, scanApplication);
                break;
            }
        }
        if (unZipFlag) {
            deletefile(filePath);
            log.info("解压成功 删除源文件");
        }
        return (downloadFlag && unZipFlag);
    }

    public static void deleteStoreFile(String path, String token, String timestamp) {
        String url = UrlUtil.STONED + "?path=" + path + "&token=" + token + "&timestamp=" + timestamp;
        HttpRequestUtil.doDelete(url);
    }

}
博客园:https://www.cnblogs.com/xianquan
Copyright ©2020 l-coil
【转载文章务必保留出处和署名,谢谢!】
【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/xianquan/p/15138903.html