Commit 99e50066 authored by Jens Korinth's avatar Jens Korinth
Browse files

Add flatten option to unzip

* ZipUtils now has flatten option for unzip (set by default)
* if not set, will recreate directory structure in target dir
parent edf10ce3
...@@ -30,9 +30,11 @@ object ZipUtils { ...@@ -30,9 +30,11 @@ object ZipUtils {
/** Unpacks all files matching the given regular expressions into a temporary directory. /** Unpacks all files matching the given regular expressions into a temporary directory.
* @param zipFile Path to .zip file. * @param zipFile Path to .zip file.
* @param regexes List of regexes; all matching files will be extracted. * @param regexes List of regexes; all matching files will be extracted.
* @param exclude List of regexes; all matching files will be excluded.
* @param flatten If true, will extract all files in same directory.
* @return tuple (temporary directory, list of extracted files). * @return tuple (temporary directory, list of extracted files).
*/ */
def unzipFile(zipFile: Path, regexes: Seq[Regex], exclude: Seq[Regex] = Seq()) def unzipFile(zipFile: Path, regexes: Seq[Regex], exclude: Seq[Regex] = Seq(), flatten: Boolean = true)
(implicit logger: Logger): (Path, Seq[Path]) = { (implicit logger: Logger): (Path, Seq[Path]) = {
import java.util.zip._ import java.util.zip._
import java.io.{BufferedInputStream, BufferedOutputStream, FileInputStream, FileOutputStream} import java.io.{BufferedInputStream, BufferedOutputStream, FileInputStream, FileOutputStream}
...@@ -46,20 +48,28 @@ object ZipUtils { ...@@ -46,20 +48,28 @@ object ZipUtils {
var zipEntry = zis.getNextEntry() var zipEntry = zis.getNextEntry()
while (Option(zipEntry).nonEmpty) { while (Option(zipEntry).nonEmpty) {
logger.trace(zipFile + ": zipentry: " + zipEntry) logger.trace(zipFile + ": zipentry: " + zipEntry)
if ((regexes map (r => ! r.findFirstIn(zipEntry.toString()).isEmpty) reduce(_||_)) && if (! zipEntry.isDirectory()) {
(exclude map (r => r.findFirstIn(zipEntry.toString()).isEmpty) reduce (_&&_))) { if (((regexes map (r => ! r.findFirstIn(zipEntry.toString()).isEmpty) fold false) (_||_)) &&
logger.trace(zipFile + ": extracting " + zipEntry) ((exclude map (r => r.findFirstIn(zipEntry.toString()).isEmpty) fold true) (_&&_))) {
val buffer = new Array[Byte](bufsz) logger.trace(zipFile + ": extracting " + zipEntry)
val outname = tempdir.resolve(Paths.get(zipEntry.getName()).getFileName()).toString() val buffer = new Array[Byte](bufsz)
val dest = new BufferedOutputStream(new FileOutputStream(outname), bufsz) val outname = tempdir.resolve(if (flatten)
extracted = Paths.get(outname) :: extracted Paths.get(zipEntry.getName()).getFileName()
var count = 0 else
while ({count = zis.read(buffer, 0, bufsz); count != -1}) Paths.get(zipEntry.getName())
dest.write(buffer, 0, count); )
dest.flush() logger.trace("outname = {}}", outname)
dest.close() Option(outname.getParent) foreach { p => if (!p.toFile.exists()) Files.createDirectories(p) }
} else { val dest = new BufferedOutputStream(new FileOutputStream(outname.toString), bufsz)
logger.trace(zipFile + ": skipping " + zipEntry) extracted = outname :: extracted
var count = 0
while ({count = zis.read(buffer, 0, bufsz); count != -1})
dest.write(buffer, 0, count);
dest.flush()
dest.close()
} else {
logger.trace(zipFile + ": skipping " + zipEntry)
}
} }
zipEntry = zis.getNextEntry() zipEntry = zis.getNextEntry()
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment