Skip to content

Commit

Permalink
Add Zip support
Browse files Browse the repository at this point in the history
Resolves com-lihaoyi#161. Add Zip
support.

`os.zip(path)` to read/write zip file. File will be created if not
exists.

`zipFile / subPath` to get a path. Should be able to support all the
file operations like copy, move, delete and so on.
  • Loading branch information
wb14123 committed Sep 17, 2024
1 parent 0ba9dfb commit 2dd39b9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
22 changes: 22 additions & 0 deletions os/src/ZipOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package os

import java.net.URI
import java.nio.file.{Path => _, _}
import collection.JavaConverters._


class zip(zipPath: Path) {

private val fs = FileSystems.newFileSystem(
URI.create("jar:file:" + zipPath.wrapped.toString),
Map("create" -> "true").asJava)

def path(path: Path): Path = new Path(fs.getPath(path.wrapped.toString))
def root(): Path = path(os.root)
def /(sub: PathChunk): Path = root() / sub
def close(): Unit = fs.close()
}

object zip extends Function1[Path, zip] {
def apply(path: Path): zip = new zip(path)
}
39 changes: 39 additions & 0 deletions os/test/src/ZipTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test.os

import test.os.TestUtil.prep
import utest._

import scala.collection.immutable.Seq

object ZipTests extends TestSuite {

// on unix it is 81 bytes, win adds 3 bytes (3 \r characters)
private val multilineSizes = Set[Long](81, 84)

def tests = Tests {

test("zipOps") {

test - prep { wd =>
val zipFile = os.zip(wd / "zip-test.zip")
os.copy(wd / "File.txt", zipFile / "File.txt")
os.copy(wd / "folder1", zipFile / "folder1")
os.copy(wd / "folder2", zipFile / "folder2")
zipFile.close()

val zipFile2 = os.zip(wd / "zip-test.zip")
os.list(zipFile2.root()).map(_.toString) ==>
Vector(os.root / "File.txt", os.root / "folder1", os.root / "folder2").map(_.toString)
os.remove.all(zipFile2 / "folder2")
os.remove(zipFile2 / "File.txt")
zipFile2.close()

val zipFile3 = os.zip(wd / "zip-test.zip")
os.list(zipFile3.root()).map(_.toString) ==> Vector(os.root / "folder1").map(_.toString)
zipFile3.close()

}
}
}

}

0 comments on commit 2dd39b9

Please sign in to comment.