Question

I want to extract data returned by a third party web service. The response is parsed by the XmlPullParser. The block of data is the Base64 decoded TEXT of a single element. So far, my parser contains the code:

                assert eventType == XmlPullParser.TEXT;
                content = xpp.getText();

The content is the mentioned block of data. It works, but it can be 100+ kBytes long. I need to parse the content using another parser that would:

  1. Decode the block of data encoded via base64. The result is the image of a zip file with a single zipped file inside.

  2. Extract the content of the zipped file -- it is in CSV format.

  3. Parse the lines of the CSV file and extract the data.

If I know the name of the file in the zip archive image, is it possible to process it on the fly using Android/Java objects? (On the fly -- I mean without storing it into the file first.) Or, how and where can I create a temporary file extracted from the zip file content?

Was it helpful?

Solution

Yes, you can parse those files on the fly.

byte[] decodedContent = Base64.decode(content, Base64.DEFAULT);

ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(decodedContent));

try{
    ZipEntry entry = null;

    while ((entry = zipStream.getNextEntry()) != null) {

        String fileName = entry.getName();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int count;

        while ((count = zipStream.read(buffer)) != -1) {
            baos.write(buffer, 0, count);
        }

        baos.close();
        zipStream.closeEntry();

        byte[] bytes = baos.toByteArray();  

        //Your own code to parse the CSV
        parseCsvFile(fileName, bytes);
    }

}finally{
    zipStream.close();
}

OTHER TIPS

Use this to decode from base64: http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html

If you're developing for SDK 8 or higher you can also use: http://developer.android.com/reference/android/util/Base64.html

Use this to unzip the decoded base64: http://developer.android.com/reference/java/util/zip/ZipInputStream.html

Use a ByteArrayInputStrean to use the unzip with the decoded base64: http://developer.android.com/reference/java/io/ByteArrayInputStream.html

And here's more to parsing a cvs file: CSV API for Java

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top