java 合成zip

将多张照片合成zip文件,并上传sftp服务器,具体代码如下:      

     SFTPClientUtils sftpClientUtils = new SFTPClientUtils();
            sftpClientUtils.makeConnection();
            InputStream phtotZip=this.doZipPhoto(list,sftpClientUtils);
            if (phtotZip == null) {
                throw new RuntimeException();
            }else{
                boolean falg = sftpClientUtils.upload(upload, photo, phtotZip, fileName);
            }
            sftpClientUtils.logout();

 

 

 

 

 

/**
     * 创建压缩文件
     * @author zhangyajuan
     * @Date 2020年3月15日 下午2:49:09
     * tag@param list
     * tag@param zipFilePath
     * tag@param id
     * tag@return
     */
    private InputStream doZipPhoto(List<RecommQueryPhotoVO> list,SFTPClientUtils sftpClientUtils) {
        try {
            ByteArrayOutputStream baoss = null;
            baoss = new ByteArrayOutputStream();
            ZipOutputStream zipOutputStream=new ZipOutputStream(baoss);
            byte[] buf = new byte[8192];
            int len;
             for (int i = 0; i < list.size(); i++) {
                 RecommQueryPhotoVO vo=list.get(i);
                 String title = StringUtils.isBlank(vo.getProjectName()) ? " " : vo.getProjectName();//项目名称
                 int num=i+1;
                 String folderName = title + "_" + String.valueOf(num) ;//文件夹名称
                 ZipEntry zipEntry =  new ZipEntry(folderName + "/");
                 zipOutputStream.putNextEntry(zipEntry);
                 List<CompletePersonVO> personList = vo.getList();
                 for (int j = 0; j < personList.size(); j++) {
                     CompletePersonVO completePersonVO=personList.get(j);
                     String name = completePersonVO.getName();
                     String allName = completePersonVO.getAllName();
                     String phototype=completePersonVO.getFileType();//获取照片类型
                     String rank=completePersonVO.getPersonRank();//排名(正常唯一不重复)
                     if(StringUtils.isBlank(rank)){
                         rank=UUIDUtils.getUUID();
                     }
                     String picName=folderName + "/" +UUIDUtils.getUUID()+ "."+phototype;
                     if (StringUtils.isNotBlank(allName) ) {
                         if(StringUtils.isNotBlank(name) ){
                             picName=folderName + "/" + name +"_"+rank+ "."+phototype;
                         }
                         InputStream download = sftpClientUtils.download(allName);
                         zipEntry = new ZipEntry(picName);
                         zipOutputStream.putNextEntry(zipEntry);
                         BufferedInputStream bis = new BufferedInputStream(download);
                         while ((len = bis.read(buf)) > 0) {
                             zipOutputStream.write(buf, 0, len);
                         }
                         bis.close();
                         zipOutputStream.closeEntry();
                         logger.info("doZipPhoto:" + "压缩完成");
                     }
                }
             }
             zipOutputStream.closeEntry();
             zipOutputStream.close();
            byte[] byteArray = baoss.toByteArray();
             InputStream bis = new ByteArrayInputStream(byteArray);
             return bis;
        } catch (Exception e) {
            logger.error("doZipPhoto压缩文件失败 : " + e);
            return null;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圈圈的博客

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值