zip中每个条目的输入流以Java的字节数组的形式传递



我需要zip的每个条目(包含各种文件和文件夹(作为字节数组。

这是我到目前为止所拥有的:

private void accessEachFileInZip (byte[] zipAsByteArray) throws IOException{
    ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(zipAsByteArray));
    ZipEntry entry = null;
    while ((entry = zipStream.getNextEntry()) != null) {
        ZipEntry currentEntry = entry;  
        InputStream inputStreamOfCurrentEntry = ???
        zipStream.closeEntry();
    }
    zipStream.close(); 
}

使用zipfile实例有一种简单的方法,就像在此示例中调用 getInputStream("EnrtryImLookingFor")

ZipFile zipFile = new ZipFile("d:\data\myzipfile.zip");
ZipEntry zipEntry = zipFile.getEntry("fileName.txt");       
InputStream inputStream = zipFile.getInputStream(zipEntry);

由于我无法轻松创建一个实例,所以我正在寻找另一种方式。

您靠近。

ZipInputStream.getNextEntry()做两件事:它返回下一个zip文件条目,但也将当前流定位在当前条目的开头。

读取下一个zip文件输入,并将流定位在 输入数据的开始。

所以只调用getNextEntry(),然后您可以使用ZipinputStream对象,read((方法将读取当前条目的内容。

您可以写类似:

private void accessEachFileInZip (byte[] zipAsByteArray) throws IOException{
    ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(zipAsByteArray));
    while ((entry = zipStream.getNextEntry()) != null) {
        // The zipStream state refers now to the stream of the current entry
       ...
    }
    zipStream.close(); 
}

最新更新