com-lihaoyi / os-lib

OS-Lib is a simple, flexible, high-performance Scala interface to common OS filesystem and subprocess APIs
Other
691 stars 72 forks source link

`os.zip` ignore empty directory. #328

Open counter2015 opened 2 weeks ago

counter2015 commented 2 weeks ago

version: 0.11.3

I have such files structure.

/dir
  /emptyDir
  1.txt
  /someDir
    2.txt

I try to write this code.

val source = os.Path("/dir")
val target = os.Path("/dir2/dir.zip")
os.zip(target, List(source))

the unzipped file lost emptyDir.

/dir
  1.txt
  /someDir
    2.txt

UPD: here is a example


@main def zipTest(): Unit =
  val wd = os.pwd

  // mkdir for such tree
  /**
    * dir/
    *   emptyDir/
    *   1.txt
    *   someDir/
    *     2.txt
    */

  os.makeDir.all(wd / "dir" / "emptyDir")
  os.write.over(wd / "dir" / "1.txt", "1")
  os.makeDir.all(wd / "dir" / "someDir")
  os.write.over(wd / "dir" / "someDir" / "2.txt", "2")

  os.remove.all(wd / "dir.zip")
  val zipFile = wd / "dir"
  os.zip(wd / "dir.zip", List(zipFile))

  os.unzip(wd / "dir.zip", wd / "unzipped")

  // does my empty dir exist? false
  println(s"does my empty dir exist? ${os.exists(wd / "unzipped" / "dir" / "emptyDir")}")
counter2015 commented 2 weeks ago

In order to keep empty directory inside zip, we should check path is a dir or file, for dir, we should create a zipEntry with one more / character. see: https://stackoverflow.com/questions/740375/directories-in-a-zip-file-when-using-java-util-zip-zipoutputstream