创建ZIP压缩文件及文件夹

   /**
     * 创建zip附件文件夹
     * @param projectId 项目id
     * @param packageId 分包标段id
     * @return
     */
    public String createZipFolder(Integer projectId,Integer packageId){
        try {
            if(null!=projectId){
                //项目信息
                Project project = projectMapper.selectByPrimaryKey(projectId);
                if(null!=project) {
                    //项目路径
                    String projectName = project.getName();
                    String projectPath = filePath + File.separator + projectName;
                    List<ProjectSubpackage> packages = new ArrayList<>();
                    //查询项目分包列表
                    if(null!=packageId){
                        ProjectSubpackage subpackage = projectSubpackageMapper.selectByPrimaryKey(packageId);
                        packages.add(subpackage);
                    }else {
                        packages = projectSubpackageMapper.selectProjectSubByProgectId(projectId);
                    }
                    if (null!=packages) {
                        for (ProjectSubpackage subpackage : packages) {
                            String packageName = subpackage.getDeviceName();
                            Integer subpackageId = subpackage.getId();
                            //分包路径
                            String packagePath = projectPath + File.separator + packageName;
                            //查询分包标段下的供应商信息
                            List<Integer> supplierIdList = projectSupplierMapper.selectSupplierAccountByProjectId(subpackageId);
                            if(null!=supplierIdList){
                                for (Integer supplierId : supplierIdList) {
                                    Supplier supplier = supplierMapper.selectByPrimaryKey(supplierId);
                                    String supplierName = supplier.getUnitName();
                                    //供应商路径
                                    String folderpath = packagePath + File.separator + supplierName;
                                    //商务
                                    String businessFolderpath = folderpath + File.separator + "商务";
                                    //技术
                                    String technologyFolderpath = folderpath + File.separator + "技术";
                                    File businessFile = new File(businessFolderpath);
                                    if (!businessFile.exists()) {
                                        businessFile.mkdirs();
                                    }
                                    File technologyFile = new File(technologyFolderpath);
                                    if (!technologyFile.exists()) {
                                        technologyFile.mkdirs();
                                    }
                                    BusinessTechnologyAtta businessTechnologyAtta = new BusinessTechnologyAtta();
                                    businessTechnologyAtta.setSupplierId(supplierId.longValue());
                                    businessTechnologyAtta.setStatus(0);
                                    //查询附件信息
                                    List<BusinessTechnologyAtta> attalist = getBusinessTechnologyAttalist(businessTechnologyAtta);
                                    if(null!=attalist && attalist.size()>0) {
                                        for (BusinessTechnologyAtta atta : attalist) {
                                            String attaPath = atta.getAttachmentPath();
                                            String attaName = atta.getAttachmentName();
                                            Integer attaType = atta.getAttachmentType();
                                            //拷贝原文件到待压缩文件夹
                                            if (new File(attaPath).exists()) {
                                                String newfolderpath = null;
                                                if (1 == attaType) {
                                                    newfolderpath = businessFolderpath + File.separator + attaName;
                                                }
                                                if (2 == attaType) {
                                                    newfolderpath = technologyFolderpath + File.separator + attaName;
                                                }
                                                FileCopyUtils.copy(new File(attaPath), new File(newfolderpath));
                                                //Files.copy(Paths.get(attaPath), Paths.get(newfolderpath));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
                        String zipPath = projectPath + sdf.format(new Date()) + ".zip";
                        boolean zipAll = zipAll(zipPath, projectPath);
                        if(zipAll){
                            File file = new File(projectPath);
                            FileUtils.deleteDirectory(file);
                            //FileUtils.forceDelete(file);
                            return zipPath;
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建Zip文件
     */
    private static boolean zipAll(String folderName, String folder) {
        File folderParam = new File(folder);
        if(folderParam.isDirectory()){
            try{
                File[] files = folderParam.listFiles();
                // 生成的压缩文件
                ZipFile zipFile = new ZipFile(folderName);
                ZipParameters parameters = new ZipParameters();
                // 压缩方式
                parameters.setCompressionMethod(CompressionMethod.DEFLATE);
                // 压缩级别
                parameters.setCompressionLevel(CompressionLevel.NORMAL);
                // 遍历文件夹下所有的文件、文件夹
                for(File file: files){
                    if (file.isDirectory()) {
                        zipFile.addFolder(file, parameters);
                    } else {
                        zipFile.addFile(file, parameters);
                    }
                }
                return true;
            }catch (ZipException e){
                e.printStackTrace();
            }
        }
        return false;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值