读取tar文件内容而无需使用java在磁盘上写入

时间:2017-04-18 02:46:54

标签: java apache-commons

我有tar文件。我需要读取tar文件内容并对这些内容做一些逻辑。这个tar文件可能包含不同的文件格式,如xml,xslt,txt。 需要只读取xml文件并使用该内容来执行一些业务逻辑。

我正在使用Apache Commons库来读取tar文件的内容。但它首先在本地磁盘上写入内容然后读取内容。是否可以只读取tar内容而无需在本地磁盘上写入。

public void untar(){
        final List<File> untaredFiles = new LinkedList<File>();
                InputStream is;
                try {
                    is = new FileInputStream(inputFile);

                final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
                TarArchiveEntry entry = null;
                int offset;
                 Map<String,Map<String,String>> filteredMap = new HashMap<String, Map<String,String>>();
                    while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
                            final File outputFile = new File(outputDir, entry.getName());
                            if (entry.isDirectory()) {

                                if (!outputFile.exists()) {

                                    if (!outputFile.mkdirs()) {
                                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                                    }
                                }
                            } else {

                                if(entry.getName().contains("test.xml")){

                                   final OutputStream outputFileStream = new FileOutputStream(outputFile); 
                                    IOUtils.copy(debInputStream, outputFileStream);
                                    outputFileStream.close();


                                    Map<String, String> defectXmlMap=   ConfigXMLParser.parseXML(outputFile.getAbsolutePath());
                                    if(!defectXmlMap.isEmpty()){
                                        filteredMap.put(outputFile.getAbsolutePath(), defectXmlMap);    
                                    }

                                }

                            }
                            untaredFiles.add(outputFile);
                        }
                    System.out.println("Final Filtered Map :"+filteredMap);
                    debInputStream.close(); 
                } catch (FileNotFoundException e) {
                    System.out.println("File Not Found Exception occured");
                    e.printStackTrace();
                } catch (IOException e) {
                    System.out.println("IOException Exception occured");
                    e.printStackTrace();
                } catch (ArchiveException e) {
                    System.out.println("ArchiveException Exception occured");
                    e.printStackTrace();
                }

        In above code if I comment below block I can't read the content of the xml.

        final OutputStream outputFileStream = new FileOutputStream(outputFile); 
        IOUtils.copy(debInputStream, outputFileStream);
        outputFileStream.close();
    }



public static Map<String, String> parseXML(String tarFilePath){

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        factory.setNamespaceAware(true);
                        DocumentBuilder builder;

                        Document xmlDoc = null;
                        Map<String, String> testMap = null;
                        Map<String, String> mismatchMap = null;

                        try{
                            builder = factory.newDocumentBuilder();
                            xmlDoc = builder.parse(tarFilePath);

                            XPathFactory xpathFactory = XPathFactory.newInstance();

                            XPath xpath = xpathFactory.newXPath();

                            NodeList nodeList = (NodeList) xpath.evaluate(
                                    "/tests/testg/@environment", xmlDoc,
                                    XPathConstants.NODESET);

                            testMap = new HashMap<String, String>();
                            mismatchMap = new HashMap<String, String>();

                            String environment = "";
                            String testURL  = gettestingURLByEnv("TEST",xmlDoc,xpath);
                            System.out.println("TEST URL :"+testURL);
                            String testEnvURL  ="";
                            for (int i = 0; i < nodeList.getLength(); i++) {
                                 environment = nodeList.item(i).getFirstChild().getNodeValue();
                                 if(!"TEST".equals(environment)){
                                     testEnvURL = gettestingURLByEnv(environment, xmlDoc, xpath);
                                     if(routEnvURL.equals(testURL)){
                                         mismatchMap.put(environment, testEnvURL);
                                     }
                                 }
                            }
                            System.out.println("Mismatched Entry :"+mismatchMap);
                        }catch(Exception exp){
                            exp.printStackTrace();
                        }
                        return mismatchMap;
        }

0 个答案:

没有答案