Convert inputStream from ZipFile to String

Here, the main functions of my web application are to download the .zip file and save it on the server side, but before doing this I need to perform the following task: 1.). The zip file contains an XML file, I must first check the XML file using a schema. 2.) if the xml file is valid, than I need to convert the contents of the xml file to a string without unzipping the file, that is, from the input stream.

I successfully check the xml file, but get the following exception when converting a string from the input stream: "java.io.EOFException: Unexpected end of the ZLIB input stream

I tried all the solutions provided in Qaru and another forum, but I have not had time yet. Any help would be really appreciated:

Below is the code:

try
    {
        ZipFile zipFileUtil = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> zipFileContents = zipFileUtil.entries();
        while(zipFileContents.hasMoreElements())
        {

            ZipEntry zipEntry = zipFileContents.nextElement();
            if(zipEntry.getName().equals("File.xml"))
            {
                InputStream sourceFile = zipFileUtil.getInputStream(zipEntry);
                if(isValidAsPerXSD(sourceFile))
                {
                    //At this line Exception is Generated
                    String xmlContent = IOUtils.toString(sourceFile);
                }
            }
        }

    }
    catch(Throwable t)
    {
        System.out.println("Exception: "+t.getMessage());
    }
+3
1

"sourceFile" ! , . .

InputStream sourceFile = zipFileUtil.getInputStream(zipEntry);
if(isValidAsPerXSD(sourceFile))
{
    sourceFile.close();
    sourceFile = zipFileUtil.getInputStream(zipEntry);
    //At this line Exception is Generated
    String xmlContent = IOUtils.toString(sourceFile);
}
sourceFile.close();
+3

All Articles