文件操作工具类 FileUtils--读文件写文件压缩文件解压文件

注:文章皆为个人纪录,可用性请以最终结果为准,若有错还请大佬们指出,谢谢!

 工具方法直接见代码

package com.jxz.core.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com..core.exception.business.CommonException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * Created by IDEA.
 * User: jiangXueZhi
 * Date: 2021/9/27
 * Time: 3:40 下午
 */
@Slf4j
public class FileUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 获取文件的后缀名
     *
     * @param appendDot 是否拼接.
     * @return
     */
    public static String getFileSuffix(String fullFileName, boolean appendDot) {
        if (fullFileName == null || fullFileName.indexOf(".") < 0 || fullFileName.length() <= 1) {
            return "";
        }

        return (appendDot ? "." : "") + fullFileName.substring(fullFileName.lastIndexOf(".") + 1);
    }

    /**
     * 往本地文件中写入内容
     * 若本地文件不存在,则自动创建,反之则覆盖
     *
     * @param filePath       本地文件地址
     * @param content        写入的内容
     * @param uniqueFileName 是否要求文件名唯一
     * @return 本地文件地址
     */
    public static String writeStrToFile(String filePath, String content, boolean uniqueFileName) {
        if (uniqueFileName) {
            String[] split = filePath.split("\\.");
            filePath = split[0] + "_" + NumberUtils.getRandomNickname(5) + "." + split[1];
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filePath);
            fos.write(content.getBytes());
            LOGGER.info("文件写入成功,文件地址:{}", filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filePath;
    }

    /**
     * 读取文件的内容
     *
     * @param path 文件地址
     * @return 文件内容
     */
    public static String readStrFromFile(String path) {
        StringBuilder sb = new StringBuilder();
        Reader reader = null;
        try {
            File file = new File(path);
            reader = new InputStreamReader(new FileInputStream(file));
            int ch;
            while ((ch = Objects.requireNonNull(reader).read()) != -1) {
                sb.append((char) ch);
            }
            LOGGER.info("文件读取成功,文件地址:{}", path);
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 删除单个文件
     *
     * @param path 被删除文件的文件名
     * @return 单个文件删除成功返回true,否则返回false
     */
    public static boolean deleteFile(String path) {
        File file = new File(path);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            LOGGER.info("文件删除成功,文件地址:{}", path);
            return file.delete();
        }
        return false;
    }

    /**
     * 删除目录(文件夹)以及目录下的文件
     *
     * @param   dirPath 被删除目录的文件路径
     * @return  目录删除成功返回true,否则返回false
     */
    public static boolean deleteDirectory(String dirPath) {
        //如果sPath不以文件分隔符结尾,自动添加文件分隔符
        if (!dirPath.endsWith(File.separator)) {
            dirPath = dirPath + File.separator;
        }
        File dirFile = new File(dirPath);
        //如果dir对应的文件不存在,或者不是一个目录,则退出
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        boolean flag = true;
        //删除文件夹下的所有文件(包括子目录)
        File[] files = dirFile.listFiles();
        if (files != null) {
            for (File file : files) {
                //删除子文件
                if (file.isFile()) {
                    flag = deleteFile(file.getAbsolutePath());
                    if (!flag) break;
                } //删除子目录
                else {
                    flag = deleteDirectory(file.getAbsolutePath());
                    if (!flag) break;
                }
            }
        }
        if (!flag) return false;
        //删除当前目录
        LOGGER.info("目录删除成功,目录地址:{}", dirPath);
        return dirFile.delete();
    }

    /**
     * 将网络资源文件-转化成输入流
     * @param strUrl
     * @return
     */
    public static InputStream getInputStreamByUrl(String strUrl) {
        HttpURLConnection conn = null;
        try {
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(20 * 1000);
            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(conn.getInputStream(), output);
            return new ByteArrayInputStream(output.toByteArray());
        } catch (Exception e) {
            log.error("url get inputStream Exception => url:[{}]", strUrl, e);
            return null;
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }
            } catch (Exception e) {
                log.error("url get inputStream conn close Exception => url:[{}]", strUrl, e);
            }
        }
    }

    public static File getFileFromUrl(String url) throws Exception {
        //对本地文件命名
        String fileName = url.substring(url.lastIndexOf("."), url.lastIndexOf("?"));
        log.info("file name:" + fileName);
        File file = null;
        URL urlfile;
        InputStream inStream = null;
        OutputStream os = null;
        try {
            file = File.createTempFile("net_url", fileName);
            //下载
            urlfile = new URL(url);
            inStream = urlfile.openStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            log.error("FileUtils getFileFromUrl =>", e);
            return null;
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }
            } catch (Exception e) {
                log.error("FileUtils getFileFromUrl =>", e);
            }
        }
        return file;
    }

    /**
     * File 文件类型转为 MultipartFile类型
     *
     * @param file 文件
     * @return MultipartFile 对象
     */
    public static MultipartFile getMultipartFile(File file) {
        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, file.getName());

        try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
            IOUtils.copy(input, os);
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid file: " + e, e);
        }
        return new CommonsMultipartFile(fileItem);
    }

    /**
     * 将InputStream写入本地文件
     * @param destination 写入本地目录
     * @param input 输入流
     */
    public static File getFile(String destination, InputStream input) {
        if (input == null) {
            return null;
        }
        int index = 0;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = null;
        try {
            downloadFile = new FileOutputStream(destination);
        } catch (FileNotFoundException e) {
            log.error("FileUtils getFile =>", e);
        }
        while (true) {
            try {
                if ((index = input.read(bytes)) == -1) break;
            } catch (IOException e) {
                log.error("FileUtils getFile =>", e);
                return null;
            }
            try {
                if (downloadFile != null) {
                    downloadFile.write(bytes, 0, index);
                }
            } catch (IOException e) {
                log.error("FileUtils getFile =>", e);
                return null;
            }
            try {
                if (downloadFile != null) {
                    downloadFile.flush();
                }
            } catch (IOException e) {
                log.error("FileUtils getFile =>", e);
                return null;
            }
        }
        try {
            input.close();
        } catch (IOException e) {
            log.error("FileUtils getFile =>", e);
            return null;
        }
        try {
            if (downloadFile != null) {
                downloadFile.close();
            }
        } catch (IOException e) {
            log.error("FileUtils getFile =>", e);
            return null;
        }
        return new File(destination);
    }

    /**
     * 将文件夹转为zip压缩包
     *
     * @param zipFileName 最终的zip文件
     * @param sourceFileName 源文件夹地址
     * @param keepDirStructure 是否保留源文件夹的结构
     */
    public static void zipCompressPackage(String sourceFileName, String zipFileName, boolean keepDirStructure) throws Exception{
        long start = System.currentTimeMillis(); // 开始
        ZipOutputStream zos = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(zipFileName);
            zos = new ZipOutputStream(fileOutputStream);
            File sourceFile = new File(sourceFileName);
            compress(sourceFile, zos, sourceFile.getName(), keepDirStructure);
            long end = System.currentTimeMillis();//结束
            System.out.println("压缩完成,耗时:" + (end - start) + " 毫秒");
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    LOGGER.error("Failed to get the zip file", e);
                }
            }
        }
    }

    // 压缩文件和文件夹
    public static void compress(File sourceFile, ZipOutputStream zos, String name, boolean keepDirStructure) throws Exception {
        byte[] buf = new byte[1024];
        if (sourceFile.isFile()) {
            // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy文件到zip输出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            // Complete the entry
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 需要保留原来的文件结构时,需要对空文件夹进行处理
                if (keepDirStructure) {
                    // 空文件夹的处理
                    zos.putNextEntry(new ZipEntry(name + "/"));
                    // 没有文件,不需要文件的copy
                    zos.closeEntry();
                }
            } else {
                for (File file : listFiles) {
                    // 判断是否需要保留原来的文件结构
                    if (keepDirStructure) {
                        // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                        // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                        compress(file, zos, name + "/" + file.getName(), keepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), keepDirStructure);
                    }
                }
            }
        }
    }

    /**
     * Zip文件解压缩
     *
     * @param packagePath 压缩包路径
     * @param dirPath 解压出来的文件夹路径
     * @return 解压出包括子文件夹中的所有文件(不包括文件夹)
     */
    public static List<String> zipUnCompressPackage(String packagePath, String dirPath) {
        List<String> fileNames = new ArrayList<>();
        String fileEncoding;
        try {
            fileEncoding = checkEncoding(packagePath);
        } catch (IOException e) {
            log.error("FileUtils zipUnCompressPackage =>", e);
            return null;
        }
        String fileEncoding1 = fileEncoding;
        try (ZipArchiveInputStream zais = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(packagePath), 4096), fileEncoding1)) {
            ZipArchiveEntry entry = null;
            while ((entry = zais.getNextZipEntry()) != null) {
                //遍历压缩包,如果进行有选择解压,可在此处进行过滤
                File tmpFile = new File(dirPath, entry.getName());
                if (entry.isDirectory()) {
                    tmpFile.mkdirs();
                } else {
                    fileNames.add(entry.getName());
                    File file = new File(tmpFile.getAbsolutePath());
                    if (!file.exists()) {
                        if (!file.getParentFile().exists()) {
                            file.getParentFile().mkdirs();
                        }
                    }
                    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), 4096)) {
                        IOUtils.copy(zais, os);
                    }
                }
            }
        } catch (Exception e) {
            log.error("FileUtils zipUnCompressPackage =>", e);
            return null;
        }
        return fileNames;
    }

    //判断字符编码
    private static String checkEncoding(String file) throws IOException {
        InputStream in = new FileInputStream(file);
        byte[] b = new byte[3];
        try {
            int i = in.read(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            in.close();
        }
        if (b[0] == -1 && b[1] == -2) {
            return "UTF-16";
        } else if (b[0] == -2 && b[1] == -1) {
            return "Unicode";
        } else if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
            return "UTF-8";
        } else {
            return "GBK";
        }
    }

    /**
     *  获取指定文件夹下所有文件
     *
     * @param dirFilePath 文件夹路径
     * @return 指定文件夹下所有文件
     */
    public static List<File> getAllFile(String dirFilePath){
        if(StringUtils.isBlank(dirFilePath))
            return null;
        return getAllFile(new File(dirFilePath));
    }

    /**
     *  获取指定文件夹下所有文件
     *
     * @param dirFile 文件夹
     * @return 指定文件夹下所有文件
     */
    public static List<File> getAllFile(File dirFile){
        // 如果文件夹不存在或着不是文件夹,则返回 null
        if(Objects.isNull(dirFile) || !dirFile.exists() || dirFile.isFile())
            return null;

        File[] childrenFiles =  dirFile.listFiles();
        if(Objects.isNull(childrenFiles) || childrenFiles.length == 0)
            return null;

        List<File> files = new ArrayList<>();
        for(File childFile : childrenFiles) {

            // 如果时文件,直接添加到结果集合
            if(childFile.isFile()) {
                files.add(childFile);
            }else {
                // 如果是文件夹。则先将其添加到结果集合,再将其内部文件添加进结果集合。
                List<File> cFiles =  getAllFile(childFile);
                if(Objects.isNull(cFiles) || cFiles.isEmpty()) continue;
                files.addAll(cFiles);
            }
        }
        return files;
    }

    /**
     * 将一批文件打包成Zip压缩包
     *
     * @param ossPathList 一批文件的oss相对路径集合
     * @return Zip压缩包相对路径
     */
    public static String getZipByDataList(List<String> ossPathList) throws Exception{
        if (CollectionUtil.isEmpty(ossPathList)) {
            return null;
        }
        String tempZipPath = "logs/" + DateTimeUtils.getCurrentDateTime() + ".zip"; // zip 文件输出的地址
        String tempPath = "logs/TempDir"; // 文件夹地址
        File dirFile = new File(tempPath);
        if(!dirFile.exists() || !dirFile.isDirectory()) {
            boolean success = dirFile.mkdir();
            if (!success) return null;
        } else {
            FileUtil.del(dirFile);
            dirFile = new File(tempPath);
            boolean success = dirFile.mkdir();
            if (!success) return null;
        }

        for (String ossPath : ossPathList) {
            if (StringUtils.isNotBlank(ossPath)) {
                String fileName = CollectionUtil.reverse(Arrays.asList(ossPath.split("/"))).get(0);
                String filePath = tempPath + "/" + fileName;
                FileUtils.getFile(filePath, OssUtils.getInputStream(ossPath, null)); // 文件存入文件夹
            }
        }
        zipCompressPackage(tempPath, tempZipPath, true); // zip 打包
        FileUtils.deleteDirectory(tempPath); // 删除文件夹及其文件
        return tempZipPath;
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值