How do I archive multiple files into a .zip file using scala?

后端 未结 6 1159
孤独总比滥情好
孤独总比滥情好 2020-12-14 19:41

Could anyone post a simple snippet that does this?

Files are text files, so compression would be nice rather than just archive the files.

I have the filename

6条回答
  •  遥遥无期
    2020-12-14 19:58

    A bit modified (shorter) version using NIO2:

    private def zip(out: Path, files: Iterable[Path]) = {
      val zip = new ZipOutputStream(Files.newOutputStream(out))
    
      files.foreach { file =>
        zip.putNextEntry(new ZipEntry(file.toString))
        Files.copy(file, zip)
        zip.closeEntry()
      }
      zip.close()
    }
    

提交回复
热议问题