scala/ScalaBook/chapter-09/src/main/scala/scalabook/file/ZipFile.scala

package scalabook.file

import java.util.zip.{ZipEntry => JavaZipEntry}
import scala.collection.mutable
import path.Path

class ZipFile(zipFS: ZipFS, val path: Path, entry: JavaZipEntry) extends VFile[ZipFile] {
  private[this] var childrenCache = List[ZipFile]()

  def exists = true

  def inputStream = Some(zipFS.nativeZip.getInputStream(entry))
  def outputStream = None

  def isFile = !isFolder
  def isFolder = entry.isDirectory

  def /(that: Path) = zipFS.newFile(path / that)

  def /(that: String) = zipFS.newFile(path / that)

  def children =
    if(!childrenCache.isEmpty)
      childrenCache
    else {
      val childrenBuffer = new mutable.ListBuffer[ZipFile]
      zipFS.path2file.foreach {
        case (tpath, tfile) =>
          if(tpath.isChildOf(path))
            childrenBuffer += tfile
      }

      childrenCache = childrenBuffer.toList
      childrenCache
    }

  def childrenNames = children.map(_.name)
}

class SyntheticZipFolder(zipFS: ZipFS, path: Path) extends ZipFile(zipFS, path, null) {
  override def isFolder = true
}