2

I have application to download .zip file from url and convert each file from .zip file into byte array. At the moment I am able to download the file read the .zip file and convert the whole .zip file into byte,But struck up at converting each file inside .zip to byte array. Any help would be grateful. I have attached my code below:

try {
    URL url = new URL(Url);
    //create the new connection
    HttpURLConnection urlConnection = (HttpURLConnection)                                url.openConnection();

    //set up some things on the connection
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true); 
    //and connect!
    urlConnection.connect();
    //set the path where we want to save the file
    //in this case, going to save it on the root directory of the
    //sd card.
    InputStream inputStream = urlConnection.getInputStream();
    dis = new DataInputStream(new BufferedInputStream(inputStream));
    System.out.println("INput connection done>>>>>");

    zis = new ZipInputStream(new BufferedInputStream(dis));

    String targetFolder="/sdcard/";

    System.out.println("zip available is"+zis.available());

    int extracted = 0;

    while ((entry = zis.getNextEntry()) != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int count;

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

        String filename = entry.getName();
        System.out.println("File name is>>"+filename);

        byte[] bytes = baos.toByteArray();
        System.out.println("Bytes is >>>>"+bytes.toString());
        // do something with 'filename' and 'bytes'...
        zis.closeEntry();

        extracted ++;
    }

    zis.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
cdmckay
  • 31,832
  • 25
  • 83
  • 114
naveen
  • 21
  • 1
  • 2
  • I don't understand what problem you have. You seem to be successfully retrieving each zip file entry as a byte array, but that's what you say you need help with. – Ted Hopp Aug 26 '11 at 14:22
  • My zip file contains 2 files,each file should be converted to byte[] and returned as 2 seperate byte array to other application – naveen Aug 26 '11 at 14:33
  • This is the good answer to extract zip file I hope you will get solution [Click Here](http://stackoverflow.com/questions/5028421/android-unzip-a-folder/5028521#5028521) – Dharmendra Aug 26 '11 at 14:43

2 Answers2

1

Your line while ((count = zis.read(buffer)) != -1) loops through reading the entire zip file. What you want to do is count = zis.read(buffer, 0, entry.getSize()). This will, all in one command, dump the contents of each zip file entry into your buffer.

And you'll want to make that byte array a lot bigger.

Alternatively, you can keep your small buffer, but just make sure that for every iteration of the main loop, you only read entry.getSize() bytes, or you'll end up reading the entire file.

Jon7
  • 7,165
  • 2
  • 33
  • 39
  • 1
    This is the right idea, but one call to `read` isn't guaranteed to read the full data, particularly since the data is arriving over an http connection. A loop is still needed, but the terminating condition needs to be the number of bytes read. – Ted Hopp Aug 26 '11 at 14:54
  • I think,I am missing some more iteration for each file converting to byte,any code help would be appreciated. – naveen Aug 26 '11 at 15:35
0

Based on the answer by Jon7 (who really should get the credit if this solves your problem), try this:

while ((entry = zis.getNextEntry()) != null) {
    String filename = entry.getName();
    int needed = entry.getSize();
    byte[] bytes = new byte[needed];
    int pos = 0;
    while (needed > 0) {
        int read = zis.read(bytes, pos, needed);
        if (read == -1) {
            // end of stream -- OOPS!
            throw new IOException("Unexpected end of stream after " + pos + " bytes for entry " + filename);
        }
        pos += read;
        needed -= read;
    }

    System.out.println("File name is>>"+filename);
    System.out.println("Bytes is >>>>"+bytes.toString());
    // do something with 'filename' and 'bytes'...
    zis.closeEntry();

    extracted ++;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I worked this,it showed negative array size exception,then I changed my prog byte[] bytes = new byte[1024];it works fine,even it worked before fine,but my concept is I should save the 1st file byte somewhere,and 2nd file somewhere.I think I should use one more iteration to read 2 files seperately. – naveen Aug 26 '11 at 15:46