java解析zip格式压缩包

java解析zip格式压缩包

做项目时遇到需要将zip格式的压缩包解析里面的图片 将里面的图片保存到文件夹 并且保存到数据库中关联起来

在上传时判断是否为zip格式的文件


    @RequestMapping(value = "/file", method = RequestMethod.POST)
    @ApiOperation(value = "文件上传")
    public Result<Object> upload(@RequestParam(required = false) MultipartFile file,
                                 @RequestParam(required = false) String base64,
                                 @RequestParam(required = false) String pid,
                                 HttpServletRequest request) {
        Setting setting = settingService.get(SettingConstant.OSS_USED);
        if (setting == null || StrUtil.isBlank(setting.getValue())) {
            return new ResultUtil<Object>().setErrorMsg(501, "您还未配置OSS存储服务");
        }

        // IP限流 在线Demo所需 5分钟限1个请求
//        String token = redisRaterLimiter.acquireTokenFromBucket("upload:"+ipInfoUtil.getIpAddr(request), 1, 300000);
//        if (StrUtil.isBlank(token)) {
//            throw new XbootException("上传那么多干嘛,等等再传吧");
//        }
        if (StrUtil.isNotBlank(base64)) {
            // base64上传
            file = Base64DecodeMultipartFile.base64Convert(base64);
        }
        String result = "";
        String fKey = CommonUtil.renamePic(file.getOriginalFilename());
        File f = new File();
        try {
            InputStream inputStream = file.getInputStream();
            // 上传至第三方云服务或服务器
            result = fileManageFactory.getFileManage(null).inputStreamUpload(inputStream, fKey, file);
            if (StrUtil.isNotBlank(pid)) {
                f.setProjectId(pid);
            }
            f.setLocation(getType(setting.getValue()));
            // 保存数据信息至数据库
            f.setName(file.getOriginalFilename());
            f.setSizes(file.getSize());
            f.setType(file.getContentType());
            f.setFKey(fKey);
            f.setUrl(result);
            fileService.save(f);
            // 上传的是zip格式文件时调用zip解析方法将里面的图片保存到文件夹并且保存到数据库关联
            if (f.getType().equals("application/x-zip-compressed")) {
                System.out.println("哈哈哈哈");
                companyEmissionController.fileImg(result);
            }
        } catch (Exception e) {
            log.error(e.toString());
            return new ResultUtil<Object>().setErrorMsg(e.toString());
        }
        if (setting.getValue().equals(SettingConstant.LOCAL_OSS)) {
            OssSetting os = new Gson().fromJson(settingService.get(SettingConstant.LOCAL_OSS).getValue(), OssSetting.class);
            result = os.getHttp() + os.getEndpoint() + "/" + f.getId();
        }
        return new ResultUtil<Object>().setData(result);
    }
@RequestMapping(value = "/fileImg", method = RequestMethod.GET)
    @ApiOperation(value = "文件上传")
    public Result<Object> fileImg(@RequestParam(required = false) String fileAddress) throws IOException {
        //zip文件解压路径
        String unZipAddress = "E:\\upload\\dcqy";
        //去目录下寻找文件
        java.io.File file = new java.io.File(fileAddress);
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(file, "GBK");//设置编码格式
        } catch (IOException exception) {
            exception.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("解压文件不存在!");
        }
        Enumeration e = zipFile.getEntries();
        while (e.hasMoreElements()) {
            org.apache.tools.zip.ZipEntry zipEntry = (ZipEntry) e.nextElement();
            String str = zipEntry.getName().substring(0, zipEntry.getName().indexOf("/"));
            String name = zipEntry.getName().substring(str.length() + 1, zipEntry.getName().length());
            CompanyEmission ce = new CompanyEmission();
            String uid = UUID.randomUUID().toString();
            if (StringUtils.isNotBlank(name)) {
                cn.jcs.xboot.modules.file.entity.File fe = new cn.jcs.xboot.modules.file.entity.File();
                String cName = name.substring(0, name.indexOf("-"));
                ce = companyEmissionService.findName(cName);
                if (StringUtils.isNotBlank(ce.getId())) {
                    fe.setName(name);
                    fe.setLocation(0);
                    fe.setUrl("E:/upload/dcqy" + "/" + uid + ".jpg");
                    fe.setProjectId(ce.getId());
                    fe.setFKey(uid + ".jpg");
                    fe.setSizes(zipEntry.getSize());
                    fe.setType("image/jpeg");
                    fileService.save(fe);
                }
            }
            if (zipEntry.isDirectory()) {
                File f = new File(unZipAddress + "/" + uid + ".jpg");
                f.mkdirs();
            } else {
                File f = new File(unZipAddress + "/" + uid + ".jpg");
                f.getParentFile().mkdirs();
                f.createNewFile();
                InputStream is = zipFile.getInputStream(zipEntry);
                FileOutputStream fos = new FileOutputStream(f);
                int length = 0;
                byte[] b = new byte[1024];

                while ((length = is.read(b, 0, 1024)) != -1) {
                    fos.write(b, 0, length);
                }
                is.close();
                fos.close();
            }
        }
        if (zipFile != null) {
            zipFile.close();
        }
//        file.deleteOnExit();//解压完以后将压缩包删除

        return new ResultUtil<Object>().setData(null);
    }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值