Commit 6e748efd by Amelin Konstantin

Add convertion java stream -> scala stream

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