Skip to content

Commit

Permalink
Add comment and create methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchesc committed Jul 26, 2018
1 parent a76025c commit c7ee29d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/main/scala/com/yuchesc/sczip/Condition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class PathMatchCondition(pattern: String) extends Condition {
override def hit(file: Path): Boolean = matcher.matches(file)
}

/**
* Exclude pattern object.
* Note: may program Include some day...
*/
object Exclude {
def apply(pattern: String): Condition = new PathMatchCondition(pattern)
}
76 changes: 56 additions & 20 deletions src/main/scala/com/yuchesc/sczip/ScZip.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ import java.nio.file._
import java.util.zip.ZipOutputStream

/**
* Zip executor.
*
* @param targetPath Target zip root path.
* @param exclude Exclude files from target via glob match pattern.
* @param normalizeRootPath If true, eliminate root or relative path from starting point.
*/
case class ScZip(targetPath: Path,
exclude: Option[Condition] = None,
normalizeRootPath: Boolean = true) {
* Zip executor.
*
* @param targetPath Target zip root path.
* @param exclude Exclude files from target via glob match pattern.
* @param normalizeRootPath If true, eliminate root or relative path from starting point.
*/
class ScZip(targetPath: Path,
exclude: Option[Condition] = None,
normalizeRootPath: Boolean = true) {

/**
* Walk target path and collect each file path.
*
* @return Zip target file list.
*/
* Walk target path and collect each file path.
*
* @return Zip target file list.
*/
def dryRun(): Seq[String] = {
val visitor = new ListFileVisitor(exclude, normalizeRootPath)
Files.walkFileTree(targetPath, visitor)
visitor.getResult
}

/**
* Zip files into out stream.
*
* @param out stream to write zip data.
*/
* Zip files into out stream.
*
* @param out stream to write zip data.
*/
def zipToOutputStream(out: OutputStream): Unit = {
val zip = new ZipOutputStream(out)
Files.walkFileTree(targetPath, new ZipFileVisitor(zip, exclude, normalizeRootPath))
Expand All @@ -49,21 +49,57 @@ case class ScZip(targetPath: Path,
}
}

/**
* ScZip object creator.
*/
object ScZip {

/**
* The simplest way to create an object.
*
* @param targetPathName Target zip root path string.
* @return object
*/
def apply(targetPathName: String): ScZip = apply(Paths.get(targetPathName))

/**
* Make sczip.
*
* @param targetPathName Target zip root path string.
* @param excludePattern Exclude glob match pattern.
* @return object
*/
def apply(targetPathName: String, excludePattern: String): ScZip = apply(Paths.get(targetPathName), Exclude(excludePattern))

/**
* Make sczip.
*
* @param targetPath Target zip root path.
* @return object
*/
def apply(targetPath: Path): ScZip = new ScZip(targetPath, None)

/**
* Make sczip.
*
* @param targetPath Target zip root path.
* @param exclude Exclude files from target via glob match pattern.
*/
def apply(targetPath: Path, exclude: Condition): ScZip = new ScZip(targetPath, Option(exclude))


def main(args: Array[String]): Unit = {
val zip = ScZip(Paths.get("./project"), Option(Exclude("**/*.{class,cache}")))
val zip = ScZip("./project", "**/*.{class,cache}")

val bytes = zip.zipToBytes()
println(bytes.length)

zip.zipToFile(Paths.get("./out.zip"))

//zip.dryRun().foreach(println)
val zip2 = ScZip(targetPath = Paths.get("./src/test/resource"),
val zip2 = new ScZip(targetPath = Paths.get("./src/test/resource"),
exclude = Option(Exclude("**/{test.a,test1.b}")),
normalizeRootPath = true)
normalizeRootPath = false)
zip2.dryRun().foreach(println)
}
}

0 comments on commit c7ee29d

Please sign in to comment.