首页 > 解决方案 > 解压时如何在 Scala 中压缩多个文件而不创建文件夹

问题描述

所以我有一个文件位置列表。我已经将文件下载到这些文件位置,所以从技术上讲,它们已经有内容了。这些只是 .txt 文件。

我的问题是,当我有多个由于此代码而被压缩的文件时,当我在文件管理器(mac 中的 Finder)中解压缩它时,解压缩的文件包含一个包含文本文件的文件夹。解压缩时我不想看到任何文件夹。我怎样才能解决这个问题?这是我的代码顺便说一句。

  def zipFiles(fileLocations: List[String], zipOutputFilename: String): Unit = {
    val a =
      for {
        fos <- managed(new FileOutputStream(zipOutputFilename))
        zos <- managed(new ZipOutputStream(fos))
      } yield {
        for {
          fileLoc <- fileLocations
        } {
          val file = new File(fileLoc)
          zos.putNextEntry(new ZipEntry(fileLoc))
          val in = new BufferedInputStream(new FileInputStream(file))
          var b = in.read()
          while (b > -1) {
            zos.write(b)
            b = in.read()
          }
          in.close()
          zos.closeEntry()
        }
        zos.close()
      }
    a.map(identity).tried
    ()
  }

标签: scalazip

解决方案


使用new ZipEntry(file.getName)而不是new ZipEntry(fileLoc).


推荐阅读