मूल रूप से मुझे एक .zip फ़ाइल को अनजिप करने की आवश्यकता होती है जिसमें मॉडलिंग नामक फ़ोल्डर होता है जिसमें बदले में कई एक्सेल फ़ाइलें होती हैं।स्कैला का उपयोग कर ज़िप फ़ाइल को अनजिप कैसे करें?
मुझे कोड ढूंढने में कुछ भाग्य मिला है जो पहले से लिखा गया था (ज़िप आर्चिव) जो ज़िप फ़ाइल को अनजिप करने के लिए है, लेकिन मुझे पता नहीं चल रहा है कि जब मैं इसका उपयोग करता हूं तो यह एक त्रुटि संदेश क्यों फेंकता है।
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
}
}
त्रुटि संदेश:: ZipArchive और त्रुटि संदेश के लिए कोड के नीचे सूचीबद्ध हैं
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 फ़ाइल खोजने की कोशिश कर रहा है के आधार पर? यह हिस्सा पूरी तरह से मुझे फेंकता है। किसी भी तरह की सहायता का स्वागत किया जाएगा। मैंने नीचे जोड़ा है कि मैं विधि कैसे कॉल कर रहा हूं, शायद मैं कुछ मूर्खतापूर्ण कर रहा हूं। अगर आप एक की सिफारिश कर सकते हैं तो मैं अपनी ज़िप फ़ाइल निकालने के लिए एक अलग तरीका का उपयोग करने के लिए भी तैयार हूं।
val tempDirectoryDir = "src/test/resources/oepTemp/"
ZipArchive.unZip(tempDirectoryDir + "Sub Region Input - Output.zip", tempDirectoryDir)
बस उत्सुक है, क्या यह फ़ाइलऑटपुटस्ट्रीम ऑब्जेक्ट को बंद नहीं करना ठीक है? – costa
कुछ सालों बाद मुझे लगता है कि आप सही हैं। यह किसी आउटपुट स्ट्रीम को बंद करने के लिए हमेशा अधिक सुरक्षित रहेगा। :) –