开发者

unzip file convert file to byte

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.setDoOutp开发者_运维技巧ut(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();
}


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.


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 ++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜