zip 中每个条目的 InputStream 作为 Java 中的字节数组传递
InputStream of each entry in a zip passed as a byte array in Java
我的 zip(包含各种文件和文件夹)的每个条目都需要 InputStream 作为字节数组传递。
这是我目前拥有的:
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()
做两件事:它 returns 下一个 ZIP 文件条目,但它也将当前流定位在当前条目的开头。
Reads the next ZIP file entry and positions the stream at the
beginning of the entry data.
因此只需调用 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();
}
我的 zip(包含各种文件和文件夹)的每个条目都需要 InputStream 作为字节数组传递。
这是我目前拥有的:
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()
做两件事:它 returns 下一个 ZIP 文件条目,但它也将当前流定位在当前条目的开头。
Reads the next ZIP file entry and positions the stream at the beginning of the entry data.
因此只需调用 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();
}