×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Scala
Posted by: osocron coder
Added: Sep 12, 2016 10:21 PM
Views: 7
Tags: file zip scala
How to zip in Scala
  1. def createZip() = {
  2.     val fos = new FileOutputStream("src/myzip.zip")
  3.     val zos = new ZipOutputStream(fos)
  4.  
  5.     val fileName = "src/myText.txt"
  6.  
  7.     addToZipFile(fileName, zos)
  8.   }
  9.  
  10.   def addToZipFile(fileName: String, zos: ZipOutputStream) = {
  11.     val file = new File(fileName)
  12.     println(file.exists())
  13.     val fis = new FileInputStream(file)
  14.     val zipEntry = new ZipEntry(fileName)
  15.     zos.putNextEntry(zipEntry)
  16.  
  17.     val bytes = new Array[Byte](1024)
  18.     var length = fis.read(bytes)
  19.     while(length >= 0) {
  20.       zos.write(bytes, 0, length)
  21.       length = fis.read(bytes)
  22.     }
  23.     zos.closeEntry()
  24.     fis.close()
  25.   }
  26.  
  27.   createZip()