java - Scala 字节数组类型不匹配错误

标签 java arrays scala unzip type-mismatch

我正在尝试在 Scala 中递归解压缩文件,我已将现有的 Java 代码修改为 scala 语法。

在我的代码中,当我声明一个字节数组来读取数据时,我收到以下错误:类型不匹配;找到:Array[java.lang.Byte] 必需:Array[scala.Byte]

我的 inputstream.read 函数还给我一个错误:用替代方法读取重载方法值: (x$1: Array[scala.Byte],x$2: Int,x$3: Int)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());
        }
    }
}
}

最佳答案

尝试删除该字符串

import java.lang.Byte

允许编译器在数组定义中使用 scala.Byte 类型

val data = new Array[Byte](BUFFER)

关于java - Scala 字节数组类型不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43319161/

相关文章:

java - GWT:从服务器上的 CSV 文件读取

java - 当我使用完 SVNKit 的工作副本后,如何删除它?

java - 使用 java 套接字从服务器到客户端的文件传输。服务器端错误并且传输到客户端的文件为空

java - Eclipse 生成的 equals 使用 1231 和 1237 的 boolean 值有什么特别的原因吗?

scala - 如何将 Scala Spark DataFrames 架构导出到 Json 文件?

java - scala akka 微内核中线程 "main"java.lang.InstantiationException 中的异常

javascript - 使用 JavaScript 将对象转换为数组

c - 在 ANSI C 中使用数组和指针

arrays - 如何在 Excel VBA 中对数组进行切片?

Scala 外部 => 语法