从字节[](Java)读取时提取ZipFile项的内容

从字节[](Java)读取时提取ZipFile项的内容,java,zip,zipfile,zipinputstream,Java,Zip,Zipfile,Zipinputstream,我有一个zip文件,其内容显示为字节[],但无法访问原始文件对象。我想阅读每个条目的内容。我能够从字节的ByteArrayInputStream创建ZipInputStream,并且可以读取条目及其名称。然而,我看不到一个简单的方法来提取每个条目的内容 (我看过ApacheCommons,但也看不到一个简单的方法) 更新@Rich的代码似乎解决了这个问题,谢谢 查询为什么这两个示例的乘数都是*4(128/512和1024*4)?如果要处理流中的嵌套zip条目,请参阅以获取想法。因为内部条目是按顺

我有一个zip文件,其内容显示为字节[],但无法访问原始文件对象。我想阅读每个条目的内容。我能够从字节的ByteArrayInputStream创建ZipInputStream,并且可以读取条目及其名称。然而,我看不到一个简单的方法来提取每个条目的内容

(我看过ApacheCommons,但也看不到一个简单的方法)

更新@Rich的代码似乎解决了这个问题,谢谢


查询为什么这两个示例的乘数都是*4(128/512和1024*4)?

如果要处理流中的嵌套zip条目,请参阅以获取想法。因为内部条目是按顺序列出的,所以可以通过获取每个条目的大小并从流中读取那么多字节来处理它们

更新示例,将每个条目复制到标准输出:

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是//提前获得
ZipEntry entry=is.getNextEntry();
while(条目!=null){
copyStream(输入、输出、输入);
entry=is.getnextery();
}
...
私有静态void copyStream(输入流输入、输出流输出、,
ZipEntry条目)引发IOException{
字节[]缓冲区=新字节[1024*4];
长计数=0;
int n=0;
long size=entry.getSize();
而(-1!=(n=in.read(buffer))&&count
它实际上使用了
ZipInputStream
作为
InputStream
(但不要在每个条目的末尾关闭它)。

计算下一个ZipEntry的开始有点棘手。请参见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();
    }
}
publicstaticvoidmain(字符串[]args){
试一试{
ZipInputStream is=新的ZipInputStream(System.in);
紫丁香;
字节[]buf=新字节[128];
内伦;
而((ze=is.getnextery())!=null){
System.out.println(“--------------”+ze);
//确定要跳过的字节数并跳过它们。
int skip=(int)ze.getSize()-128;
while(跳过>0){
skip-=is.skip(Math.min(skip,512));
}
//读取剩余的字节,如果可以打印,则打印它们。
out:while((len=is.read(buf))>=0){
对于(int i=0;i=0x80){
System.out.println(“****不可打印****”);
//由于getNextEntry()的存在,这实际上是不必要的
//自动调用它。
is.closeEntry();
//获取下一个zip条目。
爆发
}
}
系统输出写入(buf,0,len);
}
}
is.close();
}捕获(例外e){
e、 printStackTrace();
}
}

@Rich。我指定zipfile不可访问。我没有嵌套的拉链。我已经更新了我的答案来解释它如何仍然有用。您有一个单独的流,可以从stream@Rich谢谢我在你发帖的时候通过示例发帖。我会试试你的,因为我的似乎不起作用。为什么数组大小是1024*4?