How can I avoid mutable variables in Scala when using ZipInputStreams and ZipOutpuStreams?
- by pr1001
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
}
}