0

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?

pepr
  • 20,112
  • 15
  • 76
  • 139

2 Answers2

2

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();
}
Sreenath S
  • 1,269
  • 1
  • 14
  • 21
  • 1
    I have the problem with `zipStream.getNextEntry()`. Because of that I have added the `catch (IOException e) { System.out.println(e.getMessage()); }`. It produces Cannot read local header version 45. (Apparently some bug between my chair and the keyboard.) I did save the `decodedContent` into Download/output.zip` and the Total Commander for Android is capable to extract the file. What could be the problem? – pepr Oct 03 '13 at 13:07
  • I have found the answer here http://stackoverflow.com/a/15566221/1346705. I am probably in the exactly same situation. Thanks for the code. – pepr Oct 03 '13 at 13:43
1

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

Community
  • 1
  • 1
Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85