How can I avoid mutable variables in Scala when using ZipInputStreams and ZipOutpuStreams?

Posted by pr1001 on Stack Overflow See other posts from Stack Overflow or by pr1001
Published on 2010-05-17T13:17:14Z Indexed on 2010/05/17 13:20 UTC
Read the original article Hit count: 207

I'm trying to read a zip file, check that it has some required files, and then write all valid files out to another zip file. The basic introduction to java.util.zip has a lot of Java-isms and I'd love to make my code more Scala-native. Specifically, I'd like to avoid the use of vars. Here's what I have:

val fos = new FileOutputStream("new.zip");
val zipOut = new ZipOutputStream(new BufferedOutputStream(fos));

while (zipIn.available == 1) {
  val entry = zipIn.getNextEntry
  if (entryIsValid(entry)) {
    val fos = new FileOutputStream("subdir/" + entry.getName());
    val dest = new BufferedOutputStream(fos);
    // read data into the data Array
    var data: Array[Byte] = null
    var count = zip.read(data)
    while (count != -1) {
      dest.write(data, 0, count)
      count = zip.read(data)
    }
    dest.flush
    dest.close
  }
}

© Stack Overflow or respective owner

Related posts about scala

Related posts about immutability