-
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
9 changed files
with
190 additions
and
164 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 |
---|---|---|
|
@@ -12,4 +12,5 @@ project/plugins/project/ | |
.cache | ||
.lib/ | ||
.idea/ | ||
.DS_Store | ||
.DS_Store | ||
out.zip |
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name := "sczip" | ||
|
||
version := "0.9.1" | ||
version := "0.9.5" | ||
|
||
organization := "com.yuchesc" | ||
|
||
|
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
This file was deleted.
Oops, something went wrong.
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,80 @@ | ||
package com.yuchesc.sczip | ||
|
||
import java.io.{BufferedInputStream, OutputStream} | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import java.nio.file.{FileVisitResult, Files, Path, SimpleFileVisitor} | ||
import java.util.zip.{ZipEntry, ZipOutputStream} | ||
|
||
import com.yuchesc.sczip.lib.Condition | ||
|
||
import scala.collection.mutable.ListBuffer | ||
|
||
/** | ||
* Zip executor. | ||
* | ||
* @param out the output stream | ||
* @param normalizeRootPath if true, eliminate root or relative path from starting point. (Default: true) | ||
* @param capacity file reading buffer size. (Default: 4096) | ||
*/ | ||
class Zipper(out: OutputStream, | ||
val normalizeRootPath: Boolean = true, | ||
val capacity: Int = 4096) { | ||
|
||
private val zip: ZipOutputStream = new ZipOutputStream(out) | ||
private val buffer: Array[Byte] = Array.ofDim[Byte](capacity) | ||
|
||
protected def getEntryName(file: Path): String = if (normalizeRootPath) { | ||
file.toString dropWhile ("./\\" contains _) | ||
} else { | ||
file.toString | ||
} | ||
|
||
/** | ||
* Add a file to zip. | ||
* | ||
* @param path Target file | ||
* @return added entry name | ||
*/ | ||
def add(path: Path): String = { | ||
val entryName = getEntryName(path) | ||
val entry = new ZipEntry(entryName) | ||
entry.setTime(Files.getLastModifiedTime(path).toMillis) | ||
zip.putNextEntry(entry) | ||
var in = None: Option[BufferedInputStream] | ||
try { | ||
in = Option(new BufferedInputStream(Files.newInputStream(path))) | ||
Stream.continually(in.get.read(buffer)) | ||
.takeWhile(_ != -1) | ||
.foreach(zip.write(buffer, 0, _)) | ||
zip.closeEntry() | ||
entryName | ||
} finally { | ||
in.foreach(_.close()) | ||
} | ||
} | ||
|
||
/** | ||
* Add a file to zip. | ||
* | ||
* @param root Target file path | ||
* @param exclude Exclude files from target via glob match pattern. | ||
* @return added entry name list | ||
*/ | ||
def addTree(root: Path, exclude: Option[Condition] = None): Seq[String] = { | ||
val list = ListBuffer.empty[String] | ||
Files.walkFileTree(root, new SimpleFileVisitor[Path] { | ||
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = { | ||
if (exclude.isEmpty || !exclude.get.hit(file)) { | ||
list.append(add(file)) | ||
} | ||
FileVisitResult.CONTINUE | ||
} | ||
}) | ||
list | ||
} | ||
|
||
/** | ||
* Close zip stream. | ||
*/ | ||
def close(): Unit = zip.close() | ||
} |
Oops, something went wrong.