skip to content

What does ArchiveOperations provide, and how does it differ from FileSystemOperations when working with zip/tar contents in a task?

level: juniorimportance: nice to knowfreq 18%

answer

  1. zipTree / tarTree → FileTree
  2. read view, not an action
  3. compose with FileSystemOperations.copy to extract
  4. replaces project.zipTree
  5. creating archives = Zip/Tar tasks

basics

~10 s

ArchiveOperations gives you zipTree() and tarTree() to read the contents of an archive as a lazy file tree. FileSystemOperations does copy/sync/delete. You inject ArchiveOperations to replace project.zipTree/project.tarTree in a config-cache-safe task.

solid answer

~40 s

`ArchiveOperations` is the injected service for **viewing archive contents** as a `FileTree`: `archiveOps.zipTree(file)` and `archiveOps.tarTree(file)`. It's the config-cache-safe replacement for `project.zipTree(...)` / `project.tarTree(...)`. It does not copy anything by itself — it produces a lazy tree you typically feed into a `FileSystemOperations.copy { from(archiveOps.zipTree(...)) }` to actually extract, or into other file-collection consumers. So the two services compose: `ArchiveOperations` reads the archive, `FileSystemOperations` performs the copy/sync/delete. Both are injected via `@get:Inject abstract val ...`. Because `zipTree`/`tarTree` are lazy, the archive isn't opened until the tree is actually iterated (e.g. during the copy), which keeps configuration cheap. Creating archives (zip/tar) is a separate concern handled by the `Zip`/`Tar` task types, not by ArchiveOperations.

code

kotlin · 12 lines
kotlin
abstract class Extract : DefaultTask() {
    @get:Inject abstract val archives: ArchiveOperations
    @get:Inject abstract val fs: FileSystemOperations
    @get:InputFile abstract val src: RegularFileProperty
    @get:OutputDirectory abstract val dest: DirectoryProperty

    @TaskAction
    fun go() = fs.copy {
        from(archives.zipTree(src))
        into(dest)
    }.let { Unit }
}

go deeper

for a junior

Know zipTree/tarTree give you the contents of an archive and you inject ArchiveOperations to use them safely.

for a middle

Show the compose pattern: archives.zipTree feeding fs.copy to extract, and that it replaces project.zipTree.

for a senior

Explain laziness/cache impact and the read-view vs side-effect split between the two services.

for a principal

Ensure plugins standardize on injected file/archive services so no project.* archive calls survive a config-cache audit.

## What ArchiveOperations is for `ArchiveOperations` is a small injected service with two main methods: - `zipTree(Any zipFile): FileTree` - `tarTree(Any tarFile): FileTree` A `FileTree` is a lazily-evaluated, hierarchical view of files. `zipTree`/`tarTree` give you the *contents* of an archive as such a tree — without eagerly unpacking. This replaces the legacy `project.zipTree(...)` / `project.tarTree(...)`, which the configuration cache flags inside task actions because they go through `Project`. ## How it differs from FileSystemOperations | | ArchiveOperations | FileSystemOperations | |---|---|---| | Purpose | view archive contents as a tree | copy / sync / delete files | | Methods | `zipTree`, `tarTree` | `copy`, `sync`, `delete` | | Produces | a `FileTree` (read view) | performs an action (side effect) | They compose — the classic extract pattern uses both: ```kotlin abstract class Unpack : DefaultTask() { @get:Inject abstract val archives: ArchiveOperations @get:Inject abstract val fs: FileSystemOperations @get:InputFile abstract val zip: RegularFileProperty @get:OutputDirectory abstract val into: DirectoryProperty @TaskAction fun run() { fs.copy { from(archives.zipTree(zip)) into(into) } } } ``` Here `archives.zipTree(zip)` is the *source view*, and `fs.copy` is the *action* that extracts it. ## Lazy and cache-friendly Because the tree is lazy, the archive isn't read at configuration time; it's opened only when iterated during the copy. That keeps the configuration phase fast and the configuration cache valid. ## What it is NOT ArchiveOperations does **not create** archives. To produce a zip/tar you use the `Zip` or `Tar` task types (which extend `AbstractArchiveTask`). ArchiveOperations is strictly a read-side, contents-as-tree helper.

  • Does ArchiveOperations create zip files?
    No. It only views existing archive contents via zipTree/tarTree. Creating archives is done with the Zip and Tar task types (AbstractArchiveTask), not ArchiveOperations.
  • Why is zipTree returning a lazy FileTree beneficial?
    The archive isn't opened until the tree is iterated (e.g. during a copy), so configuration stays cheap and you don't pay extraction cost unless the consuming action runs.

saying these in an interview costs you the question

  • Thinking ArchiveOperations extracts files by itself — it only provides a tree; an action like fs.copy does the work.
  • Using ArchiveOperations to create archives instead of the Zip/Tar tasks.
  • Calling project.zipTree inside a @TaskAction under the configuration cache.

context