forked from com-lihaoyi/os-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} | ||
} | ||
} | ||
|
||
} |