package com.cusc.nirvana.user.rnr.fp.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Description: 文件工具类
 * <br />
 * CreateDate 2022-06-09 11:23:19
 *
 * @author yuyi
 **/
public class CuscFileUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(CuscFileUtils.class);


    /**
     * Description: byte to file
     * <br />
     * CreateDate 2022-06-09 11:23:50
     *
     * @author yuyi
     **/
    public static File byte2File(byte[] bfile, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file;
        try {
            file = new File(fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            LOGGER.error("FileUtils.byte2File error ", e);
            return null;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    LOGGER.error("FileUtils.byte2File close BufferedOutputStream error", e1);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    LOGGER.error("FileUtils.byte2File close FileOutputStream error", e1);
                }
            }
        }
        return file;
    }

    /**
     * Description: folderPath 文件夹完整绝对路径
     * <br />
     * CreateDate 2022-06-10 17:09:53
     *
     * @author yuyi
     **/
    public static void delFolder(String folderPath) {
        try {
            //删除完里面所有内容
            delAllFile(folderPath);
            java.io.File myFilePath = new java.io.File(folderPath);
            //删除空文件夹
            myFilePath.delete();
        } catch (Exception e) {
            LOGGER.error("FileUtils.delFolder error", e);
        }
    }

    /**
     * Description: 删除指定文件夹下所有文件
     * <br />
     * CreateDate 2022-06-10 17:11:56
     *
     * @author yuyi
     **/
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                //先删除文件夹里面的文件
                delAllFile(path + "/" + tempList[i]);
                //再删除空文件夹
                delFolder(path + "/" + tempList[i]);
                flag = true;
            }
        }
        return flag;
    }
}
