从输入流Java中读取多个XML文件

时间:2013-07-17 12:54:32

标签: java xml file input

如何从Java中的输入流中读取多个XML文件并将其作为XML文件写入?

我有这个:

InputStream is = new GZIPInputStream(new FileInputStream(file));

编辑:我有一个tar.gz文件说,xmls.tar.gz是包含多个XML文件的“文件”。当我使用:

将其转换为字符串时
public static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

我将所有XML文件链接在一起,同时包含文件信息。 在System.out.println我得到(这只是一个文件的开头):

blah.xml    60      0      0        2300 12077203627  10436 0ustar     0      0 <?xml version="1.0"...

解答:

根据Keith关于使用Apache Compress和io的建议,这对我很有用:

http://thinktibits.blogspot.com/2013/01/read-extract-tar-file-java-example.html

import java.io.*;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.io.IOUtils;
public class unTar {  
        public static void main(String[] args) throws Exception{
                /* Read TAR File into TarArchiveInputStream */
                TarArchiveInputStream myTarFile=new TarArchiveInputStream(new FileInputStream(new File("tar_ball.tar")));
                /* To read individual TAR file */
                TarArchiveEntry entry = null;
                String individualFiles;
                int offset;
                FileOutputStream outputFile=null;
                /* Create a loop to read every single entry in TAR file */
                while ((entry = myTarFile.getNextTarEntry()) != null) {
                        /* Get the name of the file */
                        individualFiles = entry.getName();
                        /* Get Size of the file and create a byte array for the size */
                        byte[] content = new byte[(int) entry.getSize()];
                        offset=0;
                        /* Some SOP statements to check progress */
                        System.out.println("File Name in TAR File is: " + individualFiles);
                        System.out.println("Size of the File is: " + entry.getSize());                  
                        System.out.println("Byte Array length: " + content.length);
                        /* Read file from the archive into byte array */
                        myTarFile.read(content, offset, content.length - offset);
                        /* Define OutputStream for writing the file */
                        outputFile=new FileOutputStream(new File(individualFiles));
                        /* Use IOUtiles to write content of byte array to physical file */
                        IOUtils.write(content,outputFile);              
                        /* Close Output Stream */
                        outputFile.close();
                }               
                /* Close TarAchiveInputStream */
                myTarFile.close();
        }
}

1 个答案:

答案 0 :(得分:2)

解压缩后(gzip)你还需要解压缩。 java JDK没有tar的内置API,但有几个可以从第三方获得。请参阅此答案:How do I extract a tar file in Java?