Zip文件的读取处理

String batchImportPPAPFile() {
        String successStr;
        request.fileNames.each {
            //读取sheet页
            MultipartFile multipartFile = request.getFile(it);
            
            InputStream is = multipartFile.getInputStream();
            InputStream stream = new BufferedInputStream(is);
            
            //ZipInputStream zinstream = new ZipInputStream(stream);

            //解决zip导入时中文名问题

            ZipInputStream zinstream = new ZipInputStream(stream,Charset.forName("gbk"));

            
            try {
                ZipEntry ze;
            
                while ((ze = zinstream.getNextEntry()) != null) {
                    String zeName = ze.getName();
                    String[] arr = zeName.split("/");
                    int length = arr.length;
                    if (ze.isDirectory()) {
                        //具体业务
                    } else {
                        System.err.println("file - " + ze.getName() + " : "
                                + ze.getSize() + " bytes");
                        long size = ze.getSize();
                        if (size > 0) {
                            if(arr[length-1].lastIndexOf("_")<0&&arr[length-2].lastIndexOf("_")<0){
                                byte[] data = getByte(zinstream); // 获取当前条目的字节数组
                                InputStream ins = new ByteArrayInputStream(data); // 把当前条目的字节数据转换成Inputstream流
                                Workbook workbook = WorkbookFactory.create(ins);
                                Sheet sheet = workbook.getSheetAt(0)
                                if(sheet==null){
                                    throw new BusinessException("....")
                                }
                                Row row
                                Cell cell
                                int num
                                .......

                                //具体业务
                               
                                
                            }else if(arr[length-2].lastIndexOf("_")>0){
                                byte[] bytes = getByte(zinstream);
                                UploadFile uploadFile = new UploadFile()
                                uploadFile.name = arr[length-1]
                                uploadFile.data = bytes
                                uploadFile.extension = multipartFile.contentType
                                uploadFile.length = ze.size
                                if(dataMap.keySet().contains(arr[length-2])){
                                    List<String> existList = dataMap.get(arr[length-2]);
                                    FileEntity fileEntity = fileEntitySharedService.doSaveFiles(uploadFile, 0L, 0L,null);
                                    FileEntityDto fileEntityDto = new FileEntityDto()
                                    BeanUtils.copyProperties(fileEntityDto, fileEntity)
                                    String textContent=ResponseUtil.getSuccessOm().writeValueAsString(fileEntityDto);
                                    existList.add(textContent+"@"+fileEntity.getTrueName());
                                    dataMap.put(arr[length-2], existList);
                                }
                            }
                            
                        }
                    }
                }
                if(dataMap.size != 0 && dataList.size() != 0){
                    successStr = //具体业务
                }else{
                    throw new BusinessException("上传数据为空,请重新操作");
                }
            } finally {
                zinstream.closeEntry();
                is.close()
            }
        }
        webReturn successStr;
    }
    
    /**
     * 获取条目byte[]字节
     * @param zis
     * @return
     */
    def byte[] getByte(InflaterInputStream zis) {
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] temp = new byte[1024];
            byte[] buf = null;
            int length = 0;

            while ((length = zis.read(temp, 0, 1024)) != -1) {
                bout.write(temp, 0, length);
            }

            buf = bout.toByteArray();
            bout.close();
            return buf;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值