Prateek Mane
Prateek Mane

Reputation: 111

Unable to read contents in a ZipEntry

I am unable to read the contents in ZipEntry. The code below iterates though each file in a ZipEntry. file.getSize for each file is -1, though the file has text contents.

I am unable to write contents in outputStream.

error - IndexOutOfBoundsException in java.io.ByteArrayOutputStream.write

Code

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, _))
}

ERROR

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)

Upvotes: 2

Views: 603

Answers (1)

jkinkead
jkinkead

Reputation: 4411

It's not clear to me exactly what you're trying to do. If you're trying to read the contents of each file in a zip stream, this should do it:

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")
}

Upvotes: 1

Related Questions