diff --git a/os/src/ZipOps.scala b/os/src/ZipOps.scala index a62d7d49..f46bc83c 100644 --- a/os/src/ZipOps.scala +++ b/os/src/ZipOps.scala @@ -102,6 +102,7 @@ object zip { makeZipEntry0(path, source.dest.getOrElse(os.sub) / path.subRelativeTo(source.src)) } } + makeZipEntry0(source.src, source.dest.getOrElse(os.sub / source.src.last)) } else if (shouldInclude(source.src.last, excludePatterns, includePatterns)) { makeZipEntry0(source.src, source.dest.getOrElse(os.sub / source.src.last)) } @@ -153,6 +154,7 @@ object zip { val mtimeOpt = if (preserveMtimes) Some(os.mtime(file)) else None val fis = if (os.isFile(file)) Some(os.read.inputStream(file)) else None + try makeZipEntry0(sub, fis, mtimeOpt, zipOut) finally fis.foreach(_.close()) } @@ -163,7 +165,14 @@ object zip { preserveMtimes: Option[Long], zipOut: ZipOutputStream ) = { - val zipEntry = new ZipEntry(sub.toString) + val path = is match { + // for folder + case None => sub.toString + "/" + // for file + case Some(_) => sub.toString + } + + val zipEntry = new ZipEntry(path) preserveMtimes match { case Some(mtime) => zipEntry.setTime(mtime) diff --git a/os/test/resources/test/emptyFolder/.gitkeep b/os/test/resources/test/emptyFolder/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/os/test/src-jvm/ZipOpJvmTests.scala b/os/test/src-jvm/ZipOpJvmTests.scala index dfe4bad2..c7dabc7f 100644 --- a/os/test/src-jvm/ZipOpJvmTests.scala +++ b/os/test/src-jvm/ZipOpJvmTests.scala @@ -46,9 +46,10 @@ object ZipOpJvmTests extends TestSuite { wd / "unzipped folder/nestedB", wd / "unzipped folder/one.txt", wd / "unzipped folder/nestedA/a.txt", - wd / "unzipped folder/nestedB/b.txt" + wd / "unzipped folder/nestedB/b.txt", + wd / "unzipped folder" ) - assert(paths.sorted == expected) + assert(paths.sorted == expected.sorted) } test("zipAndUnzipPreserveMtimes") - prep { wd => diff --git a/os/test/src/CheckerTests.scala b/os/test/src/CheckerTests.scala index e992d3bf..c6ad256c 100644 --- a/os/test/src/CheckerTests.scala +++ b/os/test/src/CheckerTests.scala @@ -453,8 +453,9 @@ object CheckerTests extends TestSuite { val unzipDir = os.unzip(zipFile, wd / "unzipped") os.walk(unzipDir).sorted ==> Seq( unzipDir / "File.txt", - unzipDir / "one.txt" - ) + unzipDir / "one.txt", + unzipDir / "folder1" + ).sorted } test("unzip") - prepChecker { wd => val zipFileName = "zipped.zip" @@ -478,7 +479,7 @@ object CheckerTests extends TestSuite { source = zipFile, dest = wd / "unzipped" ) - os.walk(unzipDir).length ==> 2 + os.walk(unzipDir).length ==> 3 } } } diff --git a/os/test/src/ZipOpTests.scala b/os/test/src/ZipOpTests.scala index cf6c6d90..393f84ca 100644 --- a/os/test/src/ZipOpTests.scala +++ b/os/test/src/ZipOpTests.scala @@ -56,7 +56,7 @@ object ZipOpTests extends TestSuite { wd / "unzipped folder/renamed-folder", wd / "unzipped folder/renamed-folder/one.txt" ) - assert(paths.sorted == expected) + assert(paths.sorted == expected.sorted) } test("excludePatterns") - prep { wd => @@ -80,8 +80,9 @@ object ZipOpTests extends TestSuite { zipFile1, dest = wd / "zipByExcludingCertainFiles" ) - val paths = os.walk(outputZipFilePath).sorted - val expected = Seq(wd / "zipByExcludingCertainFiles/File.amx") + val paths = os.walk(outputZipFilePath).toSeq.sorted + val expected = + Seq(wd / "zipByExcludingCertainFiles/File.amx", wd / "zipByExcludingCertainFiles").sorted assert(paths == expected) } @@ -104,9 +105,9 @@ object ZipOpTests extends TestSuite { // Unzip file to check for contents val outputZipFilePath = os.unzip(zipFile1, dest = wd / "zipByIncludingCertainFiles") - val paths = os.walk(outputZipFilePath) + val paths = os.walk(outputZipFilePath).toSeq val expected = Seq(wd / "zipByIncludingCertainFiles" / amxFile) - assert(paths == expected) + assert(paths.sorted == expected.sorted) } test("zipStream") - prep { wd => @@ -124,8 +125,8 @@ object ZipOpTests extends TestSuite { dest = wd / "zipStreamFunction" ) - val paths = os.walk(unzippedFolder) - assert(paths == Seq(unzippedFolder / "File.txt")) + val paths = os.walk(unzippedFolder).toSeq + assert(paths.sorted == Seq(unzippedFolder / "File.txt").sorted) } test("list") - prep { wd => @@ -142,8 +143,8 @@ object ZipOpTests extends TestSuite { // Unzip file to a destination folder val listedContents = os.unzip.list(source = wd / zipFileName).toSeq - val expected = Seq(os.sub / "File.txt", os.sub / "one.txt") - assert(listedContents == expected) + val expected = Seq(os.sub / "File.txt", os.sub / "one.txt", os.sub / "folder1") + assert(listedContents.sorted == expected.sorted) } test("unzipExcludePatterns") - prep { wd => @@ -167,13 +168,14 @@ object ZipOpTests extends TestSuite { excludePatterns = Seq(amxFile.r) ) - val paths = os.walk(unzippedFolder) + val paths = os.walk(unzippedFolder).toSeq val expected = Seq( wd / "unzipAllExceptExcludingCertainFiles/File.txt", - wd / "unzipAllExceptExcludingCertainFiles/one.txt" + wd / "unzipAllExceptExcludingCertainFiles/one.txt", + wd / "folder1" ) - assert(paths == expected) + assert(paths.sorted == expected.sorted) } test("unzipStream") - prep { wd => @@ -222,5 +224,23 @@ object ZipOpTests extends TestSuite { assert(file2Content == "Content of file2") } + test("emptyFolder") - prep { wd => + val zipFileName = "zipCheckEmptyDirectory.zip" + val zipFile = os.zip( + dest = wd / zipFileName, + sources = Seq( + wd / "emptyFolder", + wd / "File.txt" + ) + ) + + val unzippedFolder = os.unzip( + source = wd / zipFileName, + dest = wd / "unzipped-empty-directory" + ) + + os.walk(unzippedFolder).foreach(println) + assert(os.exists(unzippedFolder / "emptyFolder")) + } } }