基本上,我需要解压一个包含名为modeled的文件夹的.zip文件,该文件夹又包含多个Excel文件。
我已经找到了一个已经编写好的代码(ZipArchive)来解压缩zip文件,但是当我使用它时无法弄清楚为什么会抛出错误消息。以下是ZipArchive的代码和错误消息:
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
}
}
错误信息:
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] at java.io.FileOutputStream.open(Native Method)
[error] at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
[error] at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
[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(StepDefinitionMatch.java:37)
[error] at cucumber.runtime.Runtime.runStep(Runtime.java:298)
[error] at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
[error] at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
[error] at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:48)
[error] at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
[error] at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
[error] at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
[error] ...
根据错误信息看起来它是在试图查找导出的Excel文件? 这部分完全让我困惑。任何帮助将不胜感激。 我已经添加了我如何调用这个方法,也许我做了一些傻事。如果您能推荐一种不同的方式来提取我的zip文件,我也可以尝试。
val tempDirectoryDir = "src/test/resources/oepTemp/"
ZipArchive.unZip(tempDirectoryDir + "Sub Region Input - Output.zip", tempDirectoryDir)