Skip to content

Commit

Permalink
Added sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
hossshy committed Jul 19, 2018
1 parent 3193cdf commit 06b07ff
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name := "sczip"

version := "0.1"

scalaVersion := "2.12.6"
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version = 1.1.6
22 changes: 22 additions & 0 deletions src/main/scala/com/yuchesc/sczip/Condition.scala
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))
}
32 changes: 32 additions & 0 deletions src/main/scala/com/yuchesc/sczip/ScZip.scala
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)
}
}
27 changes: 27 additions & 0 deletions src/main/scala/com/yuchesc/sczip/ZipFileVisitor.scala
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
}
}

0 comments on commit 06b07ff

Please sign in to comment.