Skip to content

Commit

Permalink
Focused only exclude files.
Browse files Browse the repository at this point in the history
  • Loading branch information
hossshy committed Jul 20, 2018
1 parent 06b07ff commit 4e590cb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# sczip
Zip archive for Scala.
# ScZip

Simple zip archive for Scala.


4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ name := "sczip"

version := "0.1"

scalaVersion := "2.12.6"
scalaVersion := "2.12.6"

libraryDependencies += "net.lingala.zip4j" % "zip4j" % "1.3.2"
9 changes: 2 additions & 7 deletions src/main/scala/com/yuchesc/sczip/Condition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class PathMatchCondition(pattern: String) extends Condition {
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))
object Exclude {
def apply(pattern: String): Condition = new PathMatchCondition(pattern)
}
28 changes: 18 additions & 10 deletions src/main/scala/com/yuchesc/sczip/ScZip.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@ import java.io._
import java.nio.file._
import java.util.zip.ZipOutputStream

object ScZip {
class ScZip(targetPath: Path, exclude: Option[Condition]) {

def zipTreeToOutputStream(out: OutputStream, root: Path, condition: Condition): Unit = {
def zipToOutputStream(out: OutputStream): Unit = {
val zip = new ZipOutputStream(out)
Files.walkFileTree(root, new ZipFileVisitor(zip, condition))
Files.walkFileTree(targetPath, new ZipFileVisitor(zip, exclude))
zip.close()
}

def zipTreeToFile(root: Path, outPath: Path, condition: Condition): Unit = {
zipTreeToOutputStream(new FileOutputStream(outPath.toFile), root, condition)
def zipToFile(outPath: Path): Unit = {
zipToOutputStream(new FileOutputStream(outPath.toFile))
}

def zipTreeToFileToBytes(root: Path, condition: Condition): Array[Byte] = {
def zipToFileToBytes(): Array[Byte] = {
val out = new ByteArrayOutputStream()
zipTreeToOutputStream(out, root, condition)
zipToOutputStream(out)
out.toByteArray
}
}

object ScZip {

def apply(targetPath: Path): ScZip = new ScZip(targetPath, None)

def apply(targetPath: Path, exclude: Condition): ScZip = new ScZip(targetPath, Option(exclude))

def main(args: Array[String]): Unit = {
val cond = Condition.pathMatch("**/*.{class,properties}")
zipTreeToFile(Paths.get("./project"), Paths.get("./hoge/out.zip"), cond)
val zip = ScZip(Paths.get("./project"), Exclude("**/*.{class,cache}"))

val bytes = zipTreeToFileToBytes(Paths.get("./src"), cond)
val bytes = zip.zipToFileToBytes()
println(bytes.length)

zip.zipToFile(Paths.get("./out.zip"))
}
}
4 changes: 2 additions & 2 deletions src/main/scala/com/yuchesc/sczip/ZipFileVisitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ 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] {
protected class ZipFileVisitor(zip: ZipOutputStream, exclude: Option[Condition]) extends SimpleFileVisitor[Path] {
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
if (condition.hit(file)) {
if (exclude.isEmpty || !exclude.get.hit(file)) {
val entry = new ZipEntry(file.toString)
zip.putNextEntry(entry)
val buffer = Array.ofDim[Byte](255)
Expand Down

0 comments on commit 4e590cb

Please sign in to comment.