Commit 6e748efd by Amelin Konstantin

Add convertion java stream -> scala stream

parent 8c0b17a3
import com.typesafe.config.{Config, ConfigFactory} import com.typesafe.config.{Config, ConfigFactory}
import scala.xml._ import scala.xml._
import java.nio.file.{Files, Path, Paths, StandardOpenOption} import java.nio.file.{Files, Paths, StandardOpenOption}
import java.io.InputStream import java.io.InputStream
import java.nio.file.attribute.FileTime import java.nio.file.attribute.FileTime
import java.util.zip.{ZipEntry, ZipFile} import java.util.zip.ZipFile
import java.time.LocalDateTime import java.time.LocalDateTime
import scala.util.{Failure, Success, Try} import scala.util.{Failure, Success, Try}
import scala.collection.JavaConverters._
import xmlvalid._ import xmlvalid._
import templates._ import templates._
...@@ -35,10 +36,9 @@ object Main { ...@@ -35,10 +36,9 @@ object Main {
.filter { .filter {
p => !Files.isDirectory(p) p => !Files.isDirectory(p)
} }
.toArray .iterator
.map { .asScala
_.asInstanceOf[Path] .toStream
}
val start = LocalDateTime.now.toString.replace(":", ".") val start = LocalDateTime.now.toString.replace(":", ".")
...@@ -68,66 +68,64 @@ object Main { ...@@ -68,66 +68,64 @@ object Main {
createDirRes match { createDirRes match {
case Success(_) => case Success(_) =>
val sourceArray = { val sourceStream = paths
for (p <- paths) yield { .flatMap {
val name = p.getFileName.toString p =>
val ext = name.split('.').last.toLowerCase val name = p.getFileName.toString
val ext = name.split('.').last.toLowerCase
if (ext == "zip")
Try { if (ext == "zip")
val zip = new ZipFile(p.toString) Try {
val zip = new ZipFile(p.toString)
zip
.stream zip
.toArray .stream
.map { .iterator
z => z.asInstanceOf[ZipEntry] .asScala
} .toStream
.map { .map {
e => e =>
FileEntry(
s"${name}_${e.getName}",
//e.getName,
e.getLastModifiedTime,
Some(zip.getInputStream(e)),
zip.getInputStream(e),
Some(zip),
None)
}
} match {
case Success(v) =>
v
case Failure(ex) =>
Stream(
FileEntry( FileEntry(
s"${name}_${e.getName}", name,
//e.getName, Files.getLastModifiedTime(p),
e.getLastModifiedTime, None,
Some(zip.getInputStream(e)), Files.newInputStream(p),
zip.getInputStream(e), None,
Some(zip), Some(ex.toString))
None) )
} }
} match { else
case Success(v) => Stream(
v FileEntry(
case Failure(ex) => name,
Array( Files.getLastModifiedTime(p),
FileEntry( Some(Files.newInputStream(p)),
name, Files.newInputStream(p),
Files.getLastModifiedTime(p), None,
None, None)
Files.newInputStream(p), )
None,
Some(ex.toString))
)
}
else
Array(
FileEntry(
name,
Files.getLastModifiedTime(p),
Some(Files.newInputStream(p)),
Files.newInputStream(p),
None,
None)
)
} }
}
.flatten
.sortWith { (a, b) => .sortWith { (a, b) =>
(a.modified.compareTo(b.modified) == 0 && a.name <= b.name) || (a.modified.compareTo(b.modified) < 0) (a.modified.compareTo(b.modified) == 0 && a.name <= b.name) || (a.modified.compareTo(b.modified) < 0)
} }
Try { Try {
for (f <- sourceArray) { for (f <- sourceStream) {
val now = LocalDateTime.now.toString val now = LocalDateTime.now.toString
f.msg match { f.msg match {
...@@ -170,13 +168,13 @@ object Main { ...@@ -170,13 +168,13 @@ object Main {
} }
} }
sourceArray sourceStream
.filter { f => f.zip.nonEmpty } .filter { f => f.zip.nonEmpty }
.groupBy { f => f.zip.get } .groupBy { f => f.zip.get }
.keys .keys
.foreach { _.close() } .foreach { _.close() }
sourceArray sourceStream
.filter { f => f.zip.isEmpty } .filter { f => f.zip.isEmpty }
.foreach { .foreach {
f => f =>
......
Markdown is supported
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