user4659009 user4659009 -4 years ago 257
Scala Question

How to unzip a zip file using scala?

Basically I need to unzip a .zip file which contains a folder called modeled which in turn contains a number of excel files.

I have had some luck in finding code that was already written (ZipArchive) which is meant to unzip the zip file, but I cannot figure out why it throws an error message when I use it. The code for ZipArchive and the error message are listed below:

import{OutputStream, InputStream, File, FileOutputStream}
import{ZipEntry, ZipFile}
import scala.collection.JavaConversions._

object ZipArchive {

val BUFSIZE = 4096
val buffer = new Array[Byte](BUFSIZE)

def unZip(source: String, targetFolder: String) = {
val zipFile = new ZipFile(source)

unzipAllFile(zipFile.entries.toList, getZipEntryInputStream(zipFile)_, new File(targetFolder))

def getZipEntryInputStream(zipFile: ZipFile)(entry: ZipEntry) = zipFile.getInputStream(entry)

def unzipAllFile(entryList: List[ZipEntry], inputGetter: (ZipEntry) => InputStream, targetFolder: File): Boolean = {

entryList match {
case entry :: entries =>

if (entry.isDirectory)
new File(targetFolder, entry.getName).mkdirs
saveFile(inputGetter(entry), new FileOutputStream(new File(targetFolder, entry.getName)))

unzipAllFile(entries, inputGetter, targetFolder)
case _ =>

def saveFile(fis: InputStream, fos: OutputStream) = {
writeToFile(bufferReader(fis)_, fos)

def bufferReader(fis: InputStream)(buffer: Array[Byte]) = (, buffer)

def writeToFile(reader: (Array[Byte]) => Tuple2[Int, Array[Byte]], fos: OutputStream): Boolean = {
val (length, data) = reader(buffer)
if (length >= 0) {
fos.write(data, 0, length)
writeToFile(reader, fos)
} else

Error Message: src/test/resources/oepTemp/modeled/EQ_US_2_NULL_('CA')_ALL_ELT_IL_EQ_US.xlsx (No such file or directory), took 6.406 sec
[error] at Method)
[error] at<init>(
[error] at<init>(
[error] at com.contract.testing.ZipArchive$.unzipAllFile(ZipArchive.scala:28)
[error] at com.contract.testing.ZipArchive$.unZip(ZipArchive.scala:15)
[error] at com.contract.testing.OepStepDefinitions$$anonfun$1.apply$mcZ$sp(OepStepDefinitions.scala:175)
[error] at com.contract.testing.OepStepDefinitions$$anonfun$1.apply(OepStepDefinitions.scala:150)
[error] at com.contract.testing.OepStepDefinitions$$anonfun$1.apply(OepStepDefinitions.scala:150)
[error] at cucumber.api.scala.ScalaDsl$StepBody$$anonfun$apply$1.applyOrElse(ScalaDsl.scala:61)
[error] at cucumber.api.scala.ScalaDsl$StepBody$$anonfun$apply$1.applyOrElse(ScalaDsl.scala:61)
[error] at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
[error] at cucumber.runtime.scala.ScalaStepDefinition.execute(ScalaStepDefinition.scala:71)
[error] at cucumber.runtime.StepDefinitionMatch.runStep(
[error] at cucumber.runtime.Runtime.runStep(
[error] at cucumber.runtime.model.StepContainer.runStep(
[error] at cucumber.runtime.model.StepContainer.runSteps(
[error] at
[error] at
[error] at cucumber.runtime.junit.FeatureRunner.runChild(
[error] at cucumber.runtime.junit.FeatureRunner.runChild(
[error] ...

So based on the error message it looks like it's trying to find the exported excel file? This part completely throws me off. Any help would be greatly appreciated.
I've added below how I'm calling the method, perhaps I'm doing something silly. Also I'm up for using a different way to extract my zip file if you can recommend one.

val tempDirectoryDir = "src/test/resources/oepTemp/"
ZipArchive.unZip(tempDirectoryDir + "Sub Region Input -", tempDirectoryDir)

Answer Source

Well since were are using some utilities from java, here is a version basen on this, translated to scala, maybe this should be more functional, but it is useful

package zip

import{ IOException, FileOutputStream, FileInputStream, File }
import{ ZipEntry, ZipInputStream }

 * Created by anquegi on 04/06/15.
object Unzip extends App {

  val INPUT_ZIP_FILE: String = "src/main/resources/";
  val OUTPUT_FOLDER: String = "src/main/resources/my-zip";

  def unZipIt(zipFile: String, outputFolder: String): Unit = {

    val buffer = new Array[Byte](1024)

    try {

      //output directory
      val folder = new File(OUTPUT_FOLDER);
      if (!folder.exists()) {

      //zip file content
      val zis: ZipInputStream = new ZipInputStream(new FileInputStream(zipFile));
      //get the zipped file list entry
      var ze: ZipEntry = zis.getNextEntry();

      while (ze != null) {

        val fileName = ze.getName();
        val newFile = new File(outputFolder + File.separator + fileName);

        System.out.println("file unzip : " + newFile.getAbsoluteFile());

        //create folders
        new File(newFile.getParent()).mkdirs();

        val fos = new FileOutputStream(newFile);

        var len: Int =;

        while (len > 0) {

          fos.write(buffer, 0, len)
          len =

        ze = zis.getNextEntry()


    } catch {
      case e: IOException => println("exception caught: " + e.getMessage)



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download