excel - How to unzip a zip file using scala? -
basically need unzip .zip file contains folder called modeled in turn contains number of excel files.
i have had luck in finding code written (ziparchive) meant unzip zip file, cannot figure out why throws error message when use it. code ziparchive , error message listed below:
import java.io.{outputstream, inputstream, file, fileoutputstream} import java.util.zip.{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 else savefile(inputgetter(entry), new fileoutputstream(new file(targetfolder, entry.getname))) unzipallfile(entries, inputgetter, targetfolder) case _ => true } } def savefile(fis: inputstream, fos: outputstream) = { writetofile(bufferreader(fis)_, fos) fis.close fos.close } def bufferreader(fis: inputstream)(buffer: array[byte]) = (fis.read(buffer), 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 true } }
error message:
java.io.filenotfoundexception: 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] @ java.io.fileoutputstream.open(native method) [error] @ java.io.fileoutputstream.<init>(fileoutputstream.java:221) [error] @ java.io.fileoutputstream.<init>(fileoutputstream.java:171) [error] @ com.contract.testing.ziparchive$.unzipallfile(ziparchive.scala:28) [error] @ com.contract.testing.ziparchive$.unzip(ziparchive.scala:15) [error] @ com.contract.testing.oepstepdefinitions$$anonfun$1.apply$mcz$sp(oepstepdefinitions.scala:175) [error] @ com.contract.testing.oepstepdefinitions$$anonfun$1.apply(oepstepdefinitions.scala:150) [error] @ com.contract.testing.oepstepdefinitions$$anonfun$1.apply(oepstepdefinitions.scala:150) [error] @ cucumber.api.scala.scaladsl$stepbody$$anonfun$apply$1.applyorelse(scaladsl.scala:61) [error] @ cucumber.api.scala.scaladsl$stepbody$$anonfun$apply$1.applyorelse(scaladsl.scala:61) [error] @ scala.runtime.abstractpartialfunction.apply(abstractpartialfunction.scala:36) [error] @ cucumber.runtime.scala.scalastepdefinition.execute(scalastepdefinition.scala:71) [error] @ cucumber.runtime.stepdefinitionmatch.runstep(stepdefinitionmatch.java:37) [error] @ cucumber.runtime.runtime.runstep(runtime.java:298) [error] @ cucumber.runtime.model.stepcontainer.runstep(stepcontainer.java:44) [error] @ cucumber.runtime.model.stepcontainer.runsteps(stepcontainer.java:39) [error] @ cucumber.runtime.model.cucumberscenario.run(cucumberscenario.java:48) [error] @ cucumber.runtime.junit.executionunitrunner.run(executionunitrunner.java:91) [error] @ cucumber.runtime.junit.featurerunner.runchild(featurerunner.java:63) [error] @ cucumber.runtime.junit.featurerunner.runchild(featurerunner.java:18) [error] ...
so based on error message looks it's trying find exported excel file? part throws me off. appreciated. i've added below how i'm calling method, perhaps i'm doing silly. i'm using different way extract zip file if can recommend one.
val tempdirectorydir = "src/test/resources/oeptemp/" ziparchive.unzip(tempdirectorydir + "sub region input - output.zip", tempdirectorydir)
well since using utilities java, here version basen on this, translated scala, maybe should more functional, useful
package zip import java.io.{ ioexception, fileoutputstream, fileinputstream, file } import java.util.zip.{ zipentry, zipinputstream } /** * created anquegi on 04/06/15. */ object unzip extends app { val input_zip_file: string = "src/main/resources/my-zip.zip"; 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()) { folder.mkdir(); } //zip file content val zis: zipinputstream = new zipinputstream(new fileinputstream(zipfile)); //get 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 = zis.read(buffer); while (len > 0) { fos.write(buffer, 0, len) len = zis.read(buffer) } fos.close() ze = zis.getnextentry() } zis.closeentry() zis.close() } catch { case e: ioexception => println("exception caught: " + e.getmessage) } } unzip.unzipit(input_zip_file, output_folder) }
Comments
Post a Comment