简体   繁体   中英

Reading tar file content without writing on disk using java

I have tar file.I need to read the tar file content and do some logic on those content.This tar file might contains different file formats like xml,xslt,txt. Need to read only xml files and use that content to do some business logic.

I am using Apache Commons library to read content of tar file.But its first writing the content on local disk and then read the content.Is it possible to only read tar content without writing on local disk.

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;
        }

Here is the code that would read contents of all the files from the archive:

TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
        new GzipCompressorInputStream(inputStream));
TarArchiveEntry tarArchiveEntry = null;
BufferedReader bufferedReader = null;
try {
    while ((tarArchiveEntry = tarArchiveInputStream.getNextTarEntry()) != null) {
        System.out.println(tarArchiveEntry.getName() + ": " + (tarArchiveEntry.isFile() ? "F" : "D"));
        if (tarArchiveEntry.isFile()) {
            bufferedReader = new BufferedReader(new InputStreamReader(tarArchiveInputStream));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(tarArchiveEntry.getName() + ": " + line);
            }
        }
    }
} finally {
    if (bufferedReader != null) {
        try {
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM