文件工具类 - FileUtils

@Slf4j
@Component
public class FileUtils {
    /**
     * 文件夹复制到指定的文件夹
      */
    @SneakyThrows
    public static void copyDir(File source, File target) {
        if (!target.exists()) {
            boolean mkdirs = target.mkdirs();
        }
        if (source.isDirectory()) {
            File[] files = source.listFiles();
            if (files != null) {
                for (File file : files) {
                    File inFile = new File(target, file.getName());
                    if (file.isDirectory()) {
                        copyDir(file, inFile);
                    } else {
                        Files.copy(file.toPath(), inFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    }
                }
            }
        }
    }

    /**
     * 删除文件或文件夹(包括文件夹下的文件)
      */
    @SneakyThrows
    public static void removeFile(File target) {
        if (target.exists()) {
            File[] files = target.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        removeFile(file);
                    } else {
                        boolean delete = target.delete();
                    }
                }
            }
            boolean delete = target.delete();
        }
    }

    /**
     * 文件复制(如果目标文件存在就会替换)
     * source 和 target 都是全路径包括文件名(例如:C:\Users\干饭人.pdf)
     */
    @SneakyThrows
    public static void copyFile(File source, File target) {
        if (!target.exists()) {
            boolean newFile = target.createNewFile();
        }
        try (FileInputStream fileInputStream = new FileInputStream(source);
             FileOutputStream fileOutputStream = new FileOutputStream(target)) {
            int len;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, len);
            }
            fileOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件夹压缩成 zip 包
     * @param source 要压缩的文件夹
     * @param target 压缩后存放路径
     */
    public static String zipDir(File source, File target) {
        try {
            if (!target.exists()) {
                boolean mkdirs = target.mkdirs();
            }
            File targetFile = new File(target, source.getName() + ".zip");
            FileOutputStream fos = new FileOutputStream(targetFile);
            ZipOutputStream zipOutputStream = new ZipOutputStream(fos);
            zipSource(source, source.getName(), zipOutputStream);
            zipOutputStream.close();
            fos.close();
            return targetFile.getCanonicalPath();
        } catch (Exception e) {
            log.error("压缩文件夹失败:{}", ExceptionUtil.stacktraceToString(e));
            return "";
        }
    }

    // 将目标文件夹中的一个个文件进行压缩,保留其目录结构
    private static void zipSource(File source, String name, ZipOutputStream zipOutputStream) throws IOException {
        if (source.isDirectory()) {
            // 继续遍历
            for (File file : Objects.requireNonNull(source.listFiles())) {
                zipSource(file, name + "/" + file.getName(), zipOutputStream);
            }
        } else {
            FileInputStream fileInputStream = new FileInputStream(source);
            zipOutputStream.putNextEntry(new ZipEntry(name));
            int len;
            byte[] bytes = new byte[4096];
            while ((len = fileInputStream.read(bytes)) != -1) {
                zipOutputStream.write(bytes, 0, len);
            }
            zipOutputStream.closeEntry();
            fileInputStream.close();
        }
    }

    public static void main(String[] args) {
        File source = new File("C:\\Users\\16372\\Documents\\笔记");
        File target = new File("C:\\Users\\16372");
        // copyDir(source, target);
        // removeFile(target);
        //copyDir(new File("C:\\Users\\16372\\Documents\\干饭人.pdf"),new File("C:\\Users\\16372"));
        String s = zipDir(source, target);
        System.out.println(s);
    }
}
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值