summaryrefslogtreecommitdiff
path: root/main/api/src/mill/api/IO.scala
blob: 135a26b08147bc01652bd6686a3b225793510aed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package mill.api

import java.io.{InputStream, OutputStream}

/**
 * Misc IO utilities, eventually probably should be pushed upstream into
 * ammonite-ops
 */
object IO extends StreamSupport {

  /**
   * Unpacks the given `src` path into the context specific destination directory.
   * @param src The ZIP file
   * @param dest The relative ouput folder under the context specifix destination directory.
   * @param ctx The target context
   * @return The [[PathRef]] to the unpacked folder.
   */
  def unpackZip(src: os.Path, dest: os.RelPath = "unpacked")(implicit ctx: Ctx.Dest): PathRef = {

    val byteStream = os.read.inputStream(src)
    val zipStream = new java.util.zip.ZipInputStream(byteStream)
    while ({
      zipStream.getNextEntry match {
        case null => false
        case entry =>
          if (!entry.isDirectory) {
            val entryDest = ctx.dest / dest / os.RelPath(entry.getName)
            os.makeDir.all(entryDest / os.up)
            val fileOut = new java.io.FileOutputStream(entryDest.toString)
            IO.stream(zipStream, fileOut)
            fileOut.close()
          }
          zipStream.closeEntry()
          true
      }
    }) ()
    PathRef(ctx.dest / dest)
  }
}