Java 导入zip文件。

  • Post author:
  • Post category:java


导入zip文件,也很简单。核心代码如下:

ZipInputStream zipInputStream = null;
try {
            zipInputStream = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry;
            while ((entry = zipInputStream.getNextEntry()) != null) {
                if(entry.getName().indexOf(".xml") != -1){//读取xml文件
                    SAXReader saxReader = new SAXReader();
                    //saxReader.setEncoding("UTF-8");
                    Document document = saxReader.read(new BufferedInputStream((zipInputStream)));
                    Element element =document.getRootElement();
                    List<Element> list = element.elements();
                    if(list != null && list.size() > 0){
                        for (Element archiveElement : list) {
                            try {
                                System.out.println(archiveElement.getName());
                                String oldarchiveNumber = archiveElement.attributeValue("archivenumber");
                                String archiveNumber = Producer.generateIdOfTen(); //新生成的
                                System.out.println(archiveElement.attributeValue("level"));
                                
                                TUfwArchive archive = new TUfwArchive();
                                archive.setId(Producer.generateIdOfTen());
                                archive.setArchiveNumber(archiveNumber);
                                archive.setDustbinStatus(0);
                                iArchiveDisplayDao.insertArchive(archive);  //插入T_UFW_ARCHIVE
                                TUfwArchiveFigureRel ufwArchiveFigureRel = new TUfwArchiveFigureRel();
                                ufwArchiveFigureRel.setId(Producer.generateIdOfTen());
                                ufwArchiveFigureRel.setArchiveNumber(archiveNumber);
                                ufwArchiveFigureRel.setBoundStatus(0);
                                ufwArchiveFigureRel.setFigureCategoryNumber(importArchiveInfo.getFigureCategoryCode());
                                iArchiveDisplayDao.insertArchiveFigureRel(ufwArchiveFigureRel); //插入t_ufw_archive_figure_rel
                                
                                archiveNumberMap.put(oldarchiveNumber, archiveNumber);
                                System.out.println("----------------子元素 begin---------------");
                                List<Element> allInfoElements = archiveElement.elements();
                                System.out.println("----------------子元素个数-------------"+allInfoElements.size());
                                for (Element infoElement : allInfoElements) {
                                    String infoNumber = infoElement.attributeValue("infonumber");
                                    String type = infoElement.attributeValue("type");
                                    String valueGroup = infoElement.attributeValue("valueGroup");
                                    String value = infoElement.getText();//不可用getTextTrim();否则:简历格式将被去掉
                                    if(type != null && !type.equals("")){
                                        if(type.equals("4")){
                                            String ddInfoNumber = infoElement.attributeValue("ddinfonumber");
                                            DataDictInfoValue dataDictInfoValue = new DataDictInfoValue();
                                            dataDictInfoValue.setId(Producer.generateIdOfTen());
                                            dataDictInfoValue.setArchiveNumber(archiveNumber);
                                            dataDictInfoValue.setDdInfoNumber(ddInfoNumber);
                                            dataDictInfoValue.setInfoNumber(infoNumber);
                                            if(valueGroup!=null && !valueGroup.equals(""))
                                                dataDictInfoValue.setValueGroup(Integer.parseInt(valueGroup));
                                            iArchiveDisplayDao.insertDataDictInfoValue(dataDictInfoValue); //插入T_UFW_DATA_DICT_INFO_VALUE
                                        }else{
                                            InfoValue infoValue = new InfoValue();
                                            infoValue.setId(Producer.generateIdOfTen());
                                            infoValue.setArchiveNumber(archiveNumber);
                                            infoValue.setInfoNumber(infoNumber);
                                            if(type.equals("5")){
                                                infoValue.setvClob(value);
                                            }else
                                                infoValue.setvVarchar(value);
                                            if(valueGroup!=null && !valueGroup.equals(""))
                                                infoValue.setValueGroup(Integer.parseInt(valueGroup));
                                            iArchiveDisplayDao.insertInfoValue(infoValue); //插入T_UFW_INFO_VALUE
                                        }
                                    }
                                    
                                    
                                }
                                System.out.println("----------------子元素 end---------------");
                                
                                //插入关联表:T_UFW_IMPORT_ARCHIVE_REL表
                                TUfwImportArchiveRel tUfwImportArchiveRel = new TUfwImportArchiveRel();
                                tUfwImportArchiveRel.setId(Producer.generateIdOfTen());
                                tUfwImportArchiveRel.setArchiveNumber(archiveNumber);
                                tUfwImportArchiveRel.setImportId(importArchiveInfo.getId());
                                iImportXMLDao.insertTUfwImportArchiveRel(tUfwImportArchiveRel);
                            } catch (Exception e) {
                                throw e;
                            }
                        }
                    }
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                zipInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw e;
            }
        }

共享一下,铭记一下下~~。

转载于:https://www.cnblogs.com/fengys-moving/archive/2012/05/31/2528488.html