简体   繁体   English

从byte [](Java)读取时提取ZipFile条目的内容

[英]extracting contents of ZipFile entries when read from byte[] (Java)

I have a zip file whose contents are presented as byte[] but the original file object is not accessible . 我有一个zip文件,其内容显示为byte [] 但原始文件对象不可访问 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. 我能够从字节的ByteArrayInputStream创建一个ZipInputStream,并可以读取条目及其名称。 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). (我看过Apache Commons,但也看不到那么简单的方法)。

UPDATE @Rich's code seems to solve the problem, thanks UPDATE @ Rich的代码似乎解决了这个问题,谢谢

QUERY why do both examples have a multiplier of * 4 (128/512 and 1024*4) ? QUERY为什么两个例子的乘数都是* 4(128/512和1024 * 4)?

If you want to process nested zip entries from a stream, see this answer for ideas. 如果要处理流中的嵌套zip条目,请参阅此答案以获取提示。 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;
    }
}

它实际上使用ZipInputStream作为InputStream (但不要在每个条目的末尾关闭它)。

It's a little bit tricky to calculate the start of next ZipEntry. 计算下一个ZipEntry的开头有点棘手。 Please see this example included in JDK 6, 请参阅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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM