无法读取ZipEntry中的内容

时间:2017-03-07 20:20:06

标签: scala unzip finagle

我无法阅读ZipEntry中的内容。下面的代码遍历ZipEntry中的每个文件。每个文件的file.getSize-1,但该文件包含文本内容。

我无法在outputStream中撰写内容。

error - IndexOutOfBoundsException in java.io.ByteArrayOutputStream.write

代码

val zipStreamm = new ZipInputStream(S3Object.getObjectContent)
val stream = Stream.continually(zipStreamm.getNextEntry)
val fiels = stream.takeWhile(x => x != null).foreach{ file =>
    val outputStream = new ByteArrayOutputStream()
    val buffer = new Array[Byte](102400)
    val size = file.getSize
    val bytesRead = Stream.continually(zipStreamm.read(buffer))
    bytesRead.takeWhile(x => x != -1).foreach(outputStream.write(buffer, 0, _))
}

错误

Mar 07, 2017 2:07:42 PM com.twitter.finagle.Init$ $anonfun$once$1
INFO: Finagle version 6.42.0 (rev=f48520b6809792d8cb87c5d81a13075fd01c051d) built at 20170203-170145
Mar 07, 2017 2:07:45 PM com.twitter.finagle.util.DefaultMonitor logWithRemoteInfo
WARNING: Exception propagated to the default monitor (upstream address: /127.0.0.1:53261, downstream address: n/a, label: ).
java.lang.IndexOutOfBoundsException
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:151)
    at com.gogoair.udp.logparsers.LogFileParsingApp$$anon$1.$anonfun$apply$5(LogFileParsingApp.scala:151)
    at scala.runtime.java8.JFunction1$mcVI$sp.apply(JFunction1$mcVI$sp.java:12)
    at scala.collection.immutable.Stream.foreach(Stream.scala:530)

1 个答案:

答案 0 :(得分:1)

我不清楚你到底要做什么。如果您尝试读取zip流中每个文件的内容,则应执行此操作:

val entryStream: Stream[ZipEntry] =
  Stream.continually(zipStreamm.getNextEntry).takeWhile(_ != null)
// Files are read into strings lazily.
val files: Stream[String] = entryStream.map { _ =>
  scala.io.Source.fromInputStream(zipStreamm).getLines.mkString("\n")
}