-
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.
- Loading branch information
Showing
5 changed files
with
87 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,5 @@ | ||
name := "sczip" | ||
|
||
version := "0.1" | ||
|
||
scalaVersion := "2.12.6" |
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 @@ | ||
sbt.version = 1.1.6 |
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 com.yuchesc.sczip | ||
|
||
import java.nio.file._ | ||
|
||
trait Condition { | ||
def hit(file: Path): Boolean | ||
} | ||
|
||
class PathMatchCondition(pattern: String) extends Condition { | ||
val matcher: PathMatcher = FileSystems.getDefault.getPathMatcher(s"glob:$pattern") | ||
|
||
override def hit(file: Path): Boolean = matcher.matches(file) | ||
} | ||
|
||
class Not(condition: Condition) extends Condition { | ||
override def hit(file: Path): Boolean = !condition.hit(file) | ||
} | ||
|
||
object Condition { | ||
def pathMatch(pattern: String) = new PathMatchCondition(pattern) | ||
def notPathMatch(pattern: String) = new Not(pathMatch(pattern)) | ||
} |
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,32 @@ | ||
package com.yuchesc.sczip | ||
|
||
import java.io._ | ||
import java.nio.file._ | ||
import java.util.zip.ZipOutputStream | ||
|
||
object ScZip { | ||
|
||
def zipTreeToOutputStream(out: OutputStream, root: Path, condition: Condition): Unit = { | ||
val zip = new ZipOutputStream(out) | ||
Files.walkFileTree(root, new ZipFileVisitor(zip, condition)) | ||
zip.close() | ||
} | ||
|
||
def zipTreeToFile(root: Path, outPath: Path, condition: Condition): Unit = { | ||
zipTreeToOutputStream(new FileOutputStream(outPath.toFile), root, condition) | ||
} | ||
|
||
def zipTreeToFileToBytes(root: Path, condition: Condition): Array[Byte] = { | ||
val out = new ByteArrayOutputStream() | ||
zipTreeToOutputStream(out, root, condition) | ||
out.toByteArray | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
val cond = Condition.pathMatch("**/*.{class,properties}") | ||
zipTreeToFile(Paths.get("./project"), Paths.get("./hoge/out.zip"), cond) | ||
|
||
val bytes = zipTreeToFileToBytes(Paths.get("./src"), cond) | ||
println(bytes.length) | ||
} | ||
} |
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,27 @@ | ||
package com.yuchesc.sczip | ||
|
||
import java.io._ | ||
import java.nio.file._ | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import java.util.zip.{ZipEntry, ZipOutputStream} | ||
|
||
protected class ZipFileVisitor(zip: ZipOutputStream, condition: Condition) extends SimpleFileVisitor[Path] { | ||
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = { | ||
if (condition.hit(file)) { | ||
val entry = new ZipEntry(file.toString) | ||
zip.putNextEntry(entry) | ||
val buffer = Array.ofDim[Byte](255) | ||
var in = None: Option[BufferedInputStream] | ||
try { | ||
in = Option(new BufferedInputStream(Files.newInputStream(file))) | ||
Stream.continually(in.get.read(buffer)) | ||
.takeWhile(_ != -1) | ||
.foreach(zip.write(buffer, 0, _)) | ||
zip.closeEntry() | ||
} finally { | ||
in.foreach(_.close()) | ||
} | ||
} | ||
FileVisitResult.CONTINUE | ||
} | ||
} |