skip to content

How does the Tar task differ from the Zip task, and how do you produce a gzip-compressed tarball (.tar.gz)?

level: middleimportance: should knowfreq 35%

answer

  1. Tar & Zip both AbstractArchiveTask
  2. compression = Compression.GZIP / BZIP2 / NONE
  3. compression doesn't rename the file
  4. archiveExtension = tar.gz
  5. zip compression is per-entry, no top-level knob

basics

~10 s

Tar is the same kind of archive task as Zip but produces a .tar. Set its compression property to Compression.GZIP and give it a .tar.gz extension to get a compressed tarball.

solid answer

~30 s

Both `Tar` and `Zip` extend `AbstractArchiveTask`, so you configure inputs identically with `from`/`into`/`include`. The difference is the output format. By default `Tar` produces an uncompressed `.tar`. To compress it you set the `compression` property to `Compression.GZIP` or `Compression.BZIP2` (from `org.gradle.api.tasks.bundling.Compression`). Compression does not change the file name automatically, so you also set `archiveExtension.set("tar.gz")` (or use `archiveFileName`) so the name matches the format. Zip has no `compression` property in the same sense — zip compression is per-entry and handled internally. A common gotcha: setting `compression = GZIP` but leaving the extension `.tar` produces a gzipped file misleadingly named `.tar`.

code

kotlin · 9 lines
kotlin
import org.gradle.api.tasks.bundling.Compression

tasks.register<Tar>("packageTar") {
    from("src/main/dist")
    compression = Compression.GZIP
    archiveBaseName.set("myapp")
    archiveExtension.set("tar.gz")
    destinationDirectory.set(layout.buildDirectory.dir("dist"))
}

go deeper

for a junior

Know that Tar makes a .tar and you can compress it with the compression property.

for a middle

Explain Compression.GZIP/BZIP2/NONE, that compression doesn't auto-rename, and that you must align archiveExtension; contrast with Zip's per-entry compression.

for a senior

Discuss when to ship tar.gz vs zip, distTar/distZip from the distribution plugin, and reproducibility flags affecting archive bytes.

for a principal

Standardize distribution formats across an org, considering platform consumers, size/throughput trade-offs, and reproducible-build guarantees in the release pipeline.

## Tar vs Zip Structurally, `Tar` and `Zip` are siblings: both extend `AbstractArchiveTask` and therefore both are `CopySpec`-based copy tasks. You populate them the same way — `from(...)`, `into(...)`, `include`/`exclude`, `rename`. The only meaningful difference is the **on-disk container format** and how compression is expressed. ## Compression model - **Zip**: compression is *per-entry* and built into the zip format. There is no top-level `compression` knob the way Tar has; the archive is always a `.zip`. - **Tar**: a tar is, by default, an *uncompressed* concatenation of files. To compress the whole stream you set the `compression` property: ```kotlin import org.gradle.api.tasks.bundling.Compression tasks.register<Tar>("packageTar") { from("src/main/dist") compression = Compression.GZIP // or BZIP2, or NONE (default) archiveBaseName.set("myapp") archiveExtension.set("tar.gz") // make the name reflect the format destinationDirectory.set(layout.buildDirectory.dir("dist")) } ``` `Compression` is an enum with `NONE`, `GZIP`, and `BZIP2`. ## The naming gotcha Setting `compression` does **not** rename the file. If you leave the default extension you get a file ending in `.tar` whose bytes are actually gzip-compressed — confusing for consumers and tooling. Always align `archiveExtension` (or `archiveFileName`) with the chosen compression: `tar.gz` for GZIP, `tar.bz2` for BZIP2. ## When to choose which - `.zip` is universally consumable on Windows and is the default for `distZip` from the `application`/`distribution` plugins. - `.tar.gz` is conventional on Unix and often smaller for text-heavy payloads; `distTar` from the distribution plugin produces it. Both participate in incremental builds the same way: their inputs (the CopySpec contents) and naming properties are tracked, so re-running is up-to-date when nothing changed.

  • If you set compression = GZIP but forget to change the extension, what happens?
    Gradle still gzip-compresses the stream, but the output file is named with the default .tar extension — a gzipped file mislabeled as plain tar, which can confuse consumers and tools that key off the extension.
  • Why doesn't Zip have a compression enum like Tar?
    The zip format compresses each entry internally as part of the format itself, so there is no whole-stream compression toggle to expose; a Tar is inherently uncompressed and needs an external compression wrapper (gzip/bzip2).

saying these in an interview costs you the question

  • Claiming Zip has a compression property analogous to Tar's.
  • Setting Compression.GZIP and assuming Gradle automatically renames the archive to .tar.gz.

context