views:

273

answers:

3

I have a zip file whose contents are presented as byte[] but the original file object is not accessible. I want to read the contents of each of the entries. I am able to create a ZipInputStream from a ByteArrayInputStream of the bytes and can read the entries and their names. However I cannot see an easy way to extract the contents of each entry.

(I have looked at Apache Commons but cannot see an easy way there either).

UPDATE @Rich's code seems to solve the problem, thanks

QUERY why do both examples have a multiplier of * 4 (128/512 and 1024*4) ?

+2  A: 

If you want to process nested zip entries from a stream, see this answer for ideas. Because the inner entries are listed sequentially they can be processed by getting the size of each entry and reading that many bytes from the stream.

Updated with an example that copies each entry to Standard out:

ZipInputStream is;//obtained earlier

ZipEntry entry = is.getNextEntry();

while(entry != null) {
    copyStream(is, out, entry);

    entry = is.getNextEntry();
}
...

private static void copyStream(InputStream in, OutputStream out,
        ZipEntry entry) throws IOException {
    byte[] buffer = new byte[1024 * 4];
    long count = 0;
    int n = 0;
    long size = entry.getSize();
    while (-1 != (n = in.read(buffer)) && count < size) {
        out.write(buffer, 0, n);
        count += n;
    }
}
Rich Seller
@Rich. I specified that the zipfile is not accessible. I don't have nested zips.
peter.murray.rust
I've updated my answer to explain how it can still help. You have a single stream that can read the size of each entry from the stream
Rich Seller
@Rich. Thanks I will try reading the size.
peter.murray.rust
@Rich thanks I posted by example while you were posting yours. I will try yours as mine doesn't seem to work.
peter.murray.rust
Why is the array size 1024*4?
Casebash
A: 

It actually uses the ZipInputStream as the InputStream (but don't close it at the end of each entry).

Tom Hawtin - tackline
A: 

It's a little bit tricky to calculate the start of next ZipEntry. Please see this example included in JDK 6,

public static void main(String[] args) {
    try {
        ZipInputStream is = new ZipInputStream(System.in);
        ZipEntry ze;
        byte[] buf = new byte[128];
        int len;

        while ((ze = is.getNextEntry()) != null) {
            System.out.println("----------- " + ze);

            // Determine the number of bytes to skip and skip them.
            int skip = (int)ze.getSize() - 128;
            while (skip > 0) {
                skip -= is.skip(Math.min(skip, 512));
            }

            // Read the remaining bytes and if it's printable, print them.
            out: while ((len = is.read(buf)) >= 0) {
                for (int i=0; i<len; i++) {
                    if ((buf[i]&0xFF) >= 0x80) {
                        System.out.println("**** UNPRINTABLE ****");

                        // This isn't really necessary since getNextEntry()
                        // automatically calls it.
                        is.closeEntry();

                        // Get the next zip entry.
                        break out;
                    }
                }
                System.out.write(buf, 0, len);
            }
        }
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
ZZ Coder