【问题标题】:ZipInputStream.read in ZipEntryZipInputStream.read 中的 ZipEntry
【发布时间】:2017-06-15 23:35:13
【问题描述】:

我正在使用 ZipInputStream 读取 zip 文件。 Zip 文件有 4 个 csv 文件。有些文件是完全写入的,有些是部分写入的。请帮助我找到以下代码的问题。从 ZipInputStream.read 方法读取缓冲区是否有任何限制?

val zis = new ZipInputStream(inputStream)
Stream.continually(zis.getNextEntry).takeWhile(_ != null).foreach { file =>
      if (!file.isDirectory && file.getName.endsWith(".csv")) {
        val buffer = new Array[Byte](file.getSize.toInt)
        zis.read(buffer)
        val fo = new FileOutputStream("c:\\temp\\input\\" + file.getName)
        fo.write(buffer)
 }

【问题讨论】:

    标签: scala zipinputstream


    【解决方案1】:

    您尚未closed/flushed 您尝试写入的文件。它应该是这样的(假设是 Scala 语法,或者这是 Kotlin/Ceylon?):

        val fo = new FileOutputStream("c:\\temp\\input\\" + file.getName)
        try {
          fo.write(buffer)
        } finally {
          fo.close
        }
    

    您还应该检查阅读次数并在必要时阅读更多内容,如下所示:

    var readBytes = 0
    while (readBytes < buffer.length) {
      val r = zis.read(buffer, readBytes, buffer.length - readBytes)
      r match {
        case -1 => throw new IllegalStateException("Read terminated before reading everything")
        case _ => readBytes += r
      }
    }
    

    PS:在您的示例中,关闭}s 似乎比要求的要少。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 2016-07-02
      • 2016-05-31
      • 1970-01-01
      相关资源
      最近更新 更多