Java实现.zip文件的解压和重新上传

本文介绍了如何在SpringBoot应用中处理用户表导入功能,涉及文件上传、验证文件类型、解压ZIP文件以及将解压内容保存到数据库的过程。
摘要由CSDN通过智能技术生成
    @PostMapping("/importUserTable")
    @Anonymous
    public AjaxResult importUserTable(@RequestParam("file") MultipartFile file) throws IOException {
        return AjaxResult.success(bidService.importUserTable(file));
    }

解压zip文件并存到项目的相对路径 方便文件的上传之后 直接访问静态资源 

 @Override
    public String importUserTable(MultipartFile zipFile) {
        //检查文件是否为空
        Optional.ofNullable(zipFile).orElseThrow(() -> new IllegalArgumentException("上传的文件为空"));

        // 检查文件扩展名是否为.zip
        if (!zipFile.getOriginalFilename().toLowerCase().endsWith(ZIP_FILE_EXTENSION)) {
            throw new IllegalArgumentException("上传的文件必须是.zip格式");
        }

        // 解压ZIP文件
        String relativePath = "profile"; // 相对于项目根目录的路径
        Path uploadDirPath = Paths.get(relativePath);
        unzip(zipFile, uploadDirPath.toString());
        log.info("解压成功到:" + uploadDirPath);
        // 上传解压后的文件
        File destDir = new File(uploadDirPath.toString());
        uploadUnzippedFiles(destDir);
        return "导入成功";
    }
 // 定义一个方法来解压ZIP文件
    public void unzip(MultipartFile zipFile, String destDirPath) {
        try {
            // 创建目标目录
            // 使用当前日期创建目录结构
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String currentDateStr = dateFormat.format(new Date());
            String uploadPath = Paths.get(destDirPath, currentDateStr).toString();
            File destDir = new File(uploadPath);
            if (!destDir.exists()) {
                destDir.mkdir();
            }
            // 使用 Apache Commons Compress 来解压 ZIP 文件
            try (InputStream is = zipFile.getInputStream();
                 ZipArchiveInputStream zis = new ZipArchiveInputStream(is)) {
                ZipArchiveEntry entry = zis.getNextZipEntry();
                while (entry != null) {
                    File filePath = new File(destDir, entry.getName());
                    if (entry.isDirectory()) {
                        filePath.mkdirs();
                    } else {
                        File parent = filePath.getParentFile();
                        if (!parent.exists()) {
                            parent.mkdirs();
                        }
                        try (OutputStream outputStream = new FileOutputStream(filePath)) {
                            IOUtils.copy(zis, outputStream);
                        }
                    }
                    String fileName = entry.getName();
                    String[] parts = fileName.split("-");
                    if (parts.length == 4) {
                       .....
                        //保存到数据库中
                        saveToDatabase();
                    }
                    entry = zis.getNextZipEntry();
                }
            }
        } catch (IOException e) {
            log.error("IO error: " + e.getMessage());
            // 处理其他可能的I/O错误
        }
    }

    // 定义一个方法来上传解压后的文件
    public void uploadUnzippedFiles(File destDir) {
        // 这里只是一个占位符,你需要实现遍历目录并上传文件的逻辑
        File[] files = destDir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (!file.isDirectory()) {
                    // 这里上传每个文件
                    log.info("文件上传成功:" + file.getAbsolutePath());
                }
            }
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值