如何为zipinputstream的每个条目获得多个输入流,以稍后从这些输入流读取?

发布于 2025-02-13 03:33:14 字数 424 浏览 3 评论 0原文

当我们创建zipinputstream z时,我们在其上调用getNextentry,然后从z读取,以获取下一个文件的内容。但是,我需要获取zipinputStream s以供这些文件从它们中读取。我该怎么做?只需创建n zipinputStreams,然后在第一个1次,在第二个1次上调用getNextentry,在nth上,...一次?有比这更优雅的方法吗?

我不能只将每个条目读为一个字节数组,因为当文件很大并且此类文件的最大大小只能是2个gig时,它会消耗太多RAM(我们不能创建byte []数组比Java中的数组大),但是我有更大的文件。

When we create a ZipInputStream z, we call getNextEntry on it and then read from z, in order to get the contents of the next file. I, however, need to get ZipInputStreams for those files to read from them later. How do I do that? Just create n ZipInputStreams, and call getNextEntry on the first one 1 time, on the second one 2 times, ..., on the nth one n times? Is there a more elegant way to do that than this?

I can't just read each entry into an array of bytes, because that will consume too much RAM, when the files are large and the max size of such a file can only be 2 gigs (we can't create byte[] arrays larger than that in Java), but I have larger files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2025-02-20 03:33:14

写这本书(不过是在Scala中,因为我使用Scala,而不是Java。

import java.io.{FileInputStream, InputStream}
import java.util.Scanner
import java.util.zip.{ZipEntry, ZipFile, ZipInputStream}
import scala.collection.mutable.ListBuffer

object ZipTest {
  def main(args: Array[String]): Unit = {
    val zipFile = new ZipFile("/path/to/file")
    val entries = zipFile.entries()
    val listBuffer = new ListBuffer[InputStream]
    while (entries.hasMoreElements) {
      val entry = entries.nextElement()
      if(!entry.isDirectory) {
        val stream = zipFile.getInputStream(entry)
        listBuffer.addOne(stream)
      }
    }
    // small usage example
    var scanner = new Scanner(listBuffer(2))
    while(scanner.hasNextLine){
      println(scanner.nextLine())
    }

    scanner = new Scanner(listBuffer(0))
    while(scanner.hasNextLine){
      println(scanner.nextLine())
    }
  }
}

我实际上在此处从这里找到了这种方法:读取zip文件中的文件中的内容

Wrote this (it's in scala though, because I use scala, not Java.

import java.io.{FileInputStream, InputStream}
import java.util.Scanner
import java.util.zip.{ZipEntry, ZipFile, ZipInputStream}
import scala.collection.mutable.ListBuffer

object ZipTest {
  def main(args: Array[String]): Unit = {
    val zipFile = new ZipFile("/path/to/file")
    val entries = zipFile.entries()
    val listBuffer = new ListBuffer[InputStream]
    while (entries.hasMoreElements) {
      val entry = entries.nextElement()
      if(!entry.isDirectory) {
        val stream = zipFile.getInputStream(entry)
        listBuffer.addOne(stream)
      }
    }
    // small usage example
    var scanner = new Scanner(listBuffer(2))
    while(scanner.hasNextLine){
      println(scanner.nextLine())
    }

    scanner = new Scanner(listBuffer(0))
    while(scanner.hasNextLine){
      println(scanner.nextLine())
    }
  }
}

I actually found this approach in the top answer from here: Read Content from Files which are inside Zip file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文