Osy
Osy

Reputation: 115

Scala byte array type mismatch error

I'm trying to unzip files recursively in Scala I've modified an existing Java code to scala syntax.

At one point in my code where I declare a byte array to read data I get the following error : type mismatch; found : Array[java.lang.Byte] required: Array[scala.Byte]

Also my inputstream.read function gives me an error of : overloaded method value read with alternatives: (x$1: Array[scala.Byte],x$2: Int,x$3: Int)Int ()Int (x$1: Array[scala.Byte])Int cannot be applied to (Array[java.lang.Byte], Int, Int)

I assume this too is due to the declaration of that array. How do I resolve this? Is there a way to convert java.lang.Byte to scala.Byte?

This is my code :

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());
        }
    }
}
}

Upvotes: 0

Views: 1891

Answers (1)

Alexander Prokofyev
Alexander Prokofyev

Reputation: 34515

Try to remove this string

import java.lang.Byte

to allow compiler use scala.Byte type in array definition

val data = new Array[Byte](BUFFER)

Upvotes: 3

Related Questions