java代码上传压缩包解压文件

传入压缩包文件路径,我的参数是因为处理业务需求。

public Map<String, String> unZipFiles(File zipFile, String descDir, String dataType,String markType) throws IOException {
        System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));
        File pathFile = new File(descDir);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }

        // 解决zip文件中有中文目录或者中文文件
        ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
        Map<String, String> map = new HashMap<>();
        for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) { // 排除目录,因为可能把目录解析了导致上传文件多了一个,后台解压失败。
                // 文件名重新赋值
                String picName = UUID.randomUUID().toString();
                String zipEntryName = entry.getName();
                int lastIndexOf = zipEntryName.lastIndexOf(".");
                // 获取文件的后缀名
                String suffix = zipEntryName.substring(lastIndexOf);

                InputStream in = zip.getInputStream(entry);
                if (suffix.equals(".json")) {
                    // 处理json文件逻辑
                    InputStreamReader inputStreamReader2=new InputStreamReader(in);
                    String charsetName = inputStreamReader2.getEncoding();
                    if(charsetName.equals("UTF8")){
                        charsetName="UTF-8";
                    }
                    InputStreamReader inputStreamReader=new InputStreamReader(in,charsetName);
                    BufferedReader bufferedReader=new BufferedReader(inputStreamReader);

                    String line;
                    StringBuffer sb = new StringBuffer();
                    while ((line=bufferedReader.readLine())!=null){
                        sb.append(line);
                    }
                    bufferedReader.close();
                    inputStreamReader2.close();
                    inputStreamReader.close();
                    in.close();
                    JSONObject jsonObject = JSONObject.parseObject(sb.toString());
                    String shapes = jsonObject.getString("shapes");
                    String imageHeight = jsonObject.getString("imageHeight");
                    String imageWidth = jsonObject.getString("imageWidth");
                    List<ShapesInfo> list = JSONArray.parseArray(jsonObject.getString("shapes"), ShapesInfo.class);
                    JSONObject jsonObject1 = new JSONObject();
                    JSONObject json = new JSONObject();
                    HashSet<String> set = new HashSet<>();
                    for (ShapesInfo shapesInfo : list) {
                        String label = shapesInfo.getLabel();
                        List<List<Double>> points = shapesInfo.getPoints();
                        //判断是不是图像分割类型,如果是则返回点位信息
                        if (markType.equals("3")){
                            json.put("label", label);
                            json.put("points", points);
                            set.add(json.toString());
                        }else {
                            String x1 = points.get(0).get(0).toString();
                            BigDecimal x11 = new BigDecimal(x1);
                            String y1 = points.get(0).get(1).toString();
                            BigDecimal y11 = new BigDecimal(y1);
                            String x2 = points.get(1).get(0).toString();
                            BigDecimal x22 = new BigDecimal(x2);
                            String y2 = points.get(1).get(1).toString();
                            BigDecimal y22 = new BigDecimal(y2);

                            // 比较x1和x2,取较小值
                            BigDecimal minX = x11.min(x22);
                            // 比较y1和y2,取较小值
                            BigDecimal minY = y11.min(y22);

                            BigDecimal width = x11.subtract(x22, MathContext.DECIMAL32).abs();
                            BigDecimal height = y11.subtract(y22, MathContext.DECIMAL32).abs();

                            int x = minX.intValue();
                            int y = minY.intValue();
                            int w = width.intValue();
                            int h = height.intValue();
                            jsonObject1.put("x", x);
                            jsonObject1.put("y", y);
                            jsonObject1.put("width", w);
                            jsonObject1.put("height", h);
                            json.put("label", label);
                            json.put("coordinates", jsonObject1);
                            set.add(json.toString());
                        }

                    }


                    map.put(entry.getName(),set.toString());
                } else {
                    String outPath = (descDir + picName + suffix).replaceAll("\\*", "/");
                    map.put(entry.getName(), picName + suffix);
                    if (new File(outPath).isDirectory()) {
                        continue;
                    }
                    OutputStream out = new FileOutputStream(outPath);
                    byte[] buf1 = new byte[1024];
                    int len;
                    while ((len = in.read(buf1)) > 0) {
                        out.write(buf1, 0, len);
                    }
                    out.close();
                }
                in.close();
            }
        }
        return map;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值