解压Zip包

解压Zip包:

def unZipFile(zipFile:String , descDir:String ):Boolean ={
    var flag = true
    try {
        val pathFile:File = new File(descDir)
        if (!pathFile.exists) {
            pathFile.mkdirs
        }
        val zip = new ZipFile(new File(zipFile), Charset.forName("UTF-8"))
//        val entries = zip.entries.asInstanceOf[Enumeration[ZipEntry]]
        val entries = zip.entries.asInstanceOf[util.Enumeration[ZipEntry]]
        while (entries.hasMoreElements){
            // val entry = entries.nextElement.asInstanceOf[ZipFile]
            val entry = entries.nextElement
            val zipEntryName = entry.getName
            val in = zip.getInputStream(entry)
            val outPath = (descDir + zipEntryName).replaceAll("\*", "/")
            //判断路径是否存在,不存在则创建文件路径
            val file = new File(outPath.substring(0, outPath.lastIndexOf('/')))
            if (!file.exists) {
                file.mkdirs
            }
            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if (!new File(outPath).isDirectory()) {
                //输出文件路径信息
                println(outPath)
                val out = new FileOutputStream(outPath)
                //val writer = new PrintWriter(new File(outPath))
                val buf1 = Array[Byte]()
                while((in.read(buf1))>0){
                    in.read
                    out.write(buf1)
                }
                // out.close
            }
            in.close
        }
    }catch{
        case e: ZipException =>
            flag = false
        case e: FileNotFoundException =>
            flag = false
        case e: IOException =>
            flag = false
    }
    println("******************解压完毕********************")
    flag
}


    def main(args: Array[String]): Unit = {
        unZipFile("E:/test/zhangsan.zip", "E:/zip")
    }

  

原文地址:https://www.cnblogs.com/wangxiaowang/p/7818915.html