文件打包和下载

一、递归获取指定目录下的文件

/**
     * 递归获取指定目录下的所有文件
     *
     * @param parentPath 指定的目录
     * @return 指定的目录下的所有文件集合
     */
    public static List<File> getFilePaths(File parentPath) {
        List<File> fileList = new ArrayList<>();
        if (parentPath.isDirectory()) {
            File[] files = parentPath.listFiles();
            for (File file1 : Objects.requireNonNull(files)) {
                fileList.addAll(getFilePaths(file1));
            }
        } else {
            fileList.add(parentPath);
        }
        return fileList;
    }

二、批量文件打包

/**
     * 批量打包
     *
     * @param fileList         要打包的问价集合
     * @param fileSaveRootPath 要打包的文文件指定目录(压缩包存放目录)
     * @param zipName          打包后的压缩包名称
     * @param b                是否保持相对目录结构
     * @return 压缩包的绝对路径
     */
    public static String createZipAndReturnPath(List<File> fileList, String fileSaveRootPath, String zipName, boolean b) {
        //zip文件保存路径
        String zipPath = fileSaveRootPath + zipName;

        try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));) {
            for (File file : fileList) {
                //获得下载文件完整路径
                String downloadPath = file.getAbsolutePath();

                try (FileInputStream fis = new FileInputStream(downloadPath);) {
                    if (b) {
                        out.putNextEntry(new ZipEntry(file.getAbsolutePath().replace(fileSaveRootPath, "")));
                    } else {
                        out.putNextEntry(new ZipEntry(file.getName()));
                    }

                    //写入压缩包
                    int len;
                    byte[] buffer = new byte[8192];
                    while ((len = fis.read(buffer)) > 0) {
                        out.write(buffer, 0, len);
                    }
                    out.flush();
                    out.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            out.close();
            return zipPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

三、文件下载

/**
     * 文件下载
     * @param sourcePath 要下载的文件
     * @param targetPath 要下载到哪里
     * @throws Exception
     */
    public static void downloadFile(String sourcePath, String targetPath) throws Exception {
        //开始下载
        try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(sourcePath)));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(targetPath)));) {
            byte[] buff = new byte[8192];
            int len = 0;
            while ((len = is.read(buff, 0, buff.length)) != -1) {
                out.write(buff, 0, len);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

四、调用示例

public static void main(String[] args) throws Exception {
        String basePath = "D:\\U盘\\文件\\教师文档\\";
        List<File> filePaths = getFilePaths(new File(basePath));
        filePaths.forEach(file -> System.out.println(file.getAbsolutePath()));
        String zipAndReturnPath = createZipAndReturnPath(filePaths, basePath, "测试.zip", true);
        downloadFile(zipAndReturnPath, basePath + "下载.zip");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值