0
我想在Scala中遞歸解壓縮文件我已經修改了現有的Java代碼到scala語法。斯卡拉字節數組類型不匹配錯誤
在我的代碼中的一個點,我聲明一個字節數組來讀取數據,我得到以下錯誤:類型不匹配;發現:數組[java.lang.Byte中]要求:數組[scala.Byte]
而且我inputstream.read功能給我的一個錯誤:替代品讀重載的方法值:(X $ 1:數組[斯卡拉Int()int(x $ 1:Array [scala.Byte])Int不能應用於(Array [java.lang.Byte],Int,Int)
我想這也是由於該數組的聲明。我該如何解決這個問題?有沒有辦法將java.lang.Byte轉換爲scala.Byte?
這是我的代碼:
import java.io._;
import org.apache.log4j._
import org.apache.spark.SparkContext
import java.io.IOException
import scala.collection.JavaConversions._
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.io.InputStream
import java.io.OutputStream
import java.io.File
import java.lang.Byte
object MultiLevelUnzip
{
val BUFFER = 2048
def main (args:Array[String])
{
Logger.getLogger("org").setLevel(Level.ERROR)
val sc = new SparkContext("local[*]","Unzip")
//val Files = sc.listFiles()
sc.stop()
}
def findFiles(d : File): Array[File] =
{
val (dirs, files) = d.listFiles.partition(_.isDirectory)
files ++ dirs.flatMap(findFiles)
}
def extractFolder(zipFile:String)=
{
System.out.println(zipFile);
val file = new File(zipFile);
val zip = new ZipFile(file);
val newPath = zipFile.substring(0, zipFile.length() - 4);
new File(newPath).mkdir();
var zipFileEntries = zip.entries()
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
val entry = zipFileEntries.nextElement()
val currentEntry = entry.getName()
val destFile = new File(newPath, currentEntry);
//destFile = new File(newPath, destFile.getName());
val destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory())
{
val is = new BufferedInputStream(zip.getInputStream(entry))
var currentByte = null
// establish buffer for writing file
// val buffer = Array.fill[Byte](BUFFER)(_)
// write the current file to disk
val fos = new FileOutputStream(destFile)
val dest = new BufferedOutputStream(fos,BUFFER)
val data = new Array[Byte](BUFFER)
while ((currentByte = is.read(data,0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
if (currentEntry.endsWith(".zip"))
{
// found a zip file, try to open
extractFolder(destFile.getAbsolutePath());
}
}
}
}
之大,工作,但在使用編譯型scala.Byte ((currentByte = is.read(data,0,BUFFER))!= -1) { dest.write(data,0,currentByte); } 我仍然在類型不匹配的讀取函數中出現錯誤,其中預計發現null並且發現Int。 關於如何解決這個問題的任何想法? –