[GRADLE-2171] Add an option to avoid duplicate entries when creating a zip file Created: 14/Mar/12  Updated: 28/Jun/13  Resolved: 28/Jun/13

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: 1.7-rc-1

Type: Bug
Reporter: Gradle Forums Assignee: Unassigned
Resolution: Fixed Votes: 18

Issue Links:
Duplicates GRADLE-1050 Jar task should merge entries, not cr... Resolved


Hi everyone,

While working on another issue, I stumbled across this issue: when you create a Zip file with the Zip task, you might end up with multiple identical entries in the Zip, e.g. the exact same file more than once in the same directory within the Zip. This is the case when multiple "from"'s are given which end up creating the same Filename at the same location within the resulting zip.

As the Zip spec doesn't forbid this case (I think), these Zip files are, in theory, okay. However, depending on how and where you try to extract them, you'll end up with a whole slue or problems and errors. This is why gradle should propose an option to ensure file uniqueness in Zip files.

imho, zip entry uniqueness should even be the default behavior for a couple of reasons: First, it would make the zip task behave the same as the copy task (when it comes to filesystem copies), which also only keeps the last file copied in case of a conflict. Lastly, having multiple identical zip entries is clearly the corner case that 99,99% of users and use cases do NOT want, so in accordance with gradle's general philosophy, those who want the "special" behavior should be the ones having to customize the task.

For the history, please see:

best regards,
[1] http://forums.gradle.org/gradle/topics/solution_for_copying_a_subdirectory_wihtin_a_zip_into_another_zip

Comment by Michael Brand [ 26/Mar/12 ]

Seems like Gradle Zip should provide the same options that Ant zip provides:

duplicate: behavior when a duplicate file is found. Valid values are "add", "preserve", and "fail". The default value is "add".

Comment by Peter Niederwieser [ 14/Jul/12 ]

I have a tested fix that filters duplicates for Zips/Jars/Wars/Ears/Tars. Do we need to support other behaviors (e.g. fail fast)? Can we change the default? Seems like adding duplicate entries is almost never useful. Conversely, people regularly run into the problem that the same file gets added multiple times. For comparison, the JDK ZipOutputStream class fails hard when an attempt is made to add another entry with the same path.

Comment by Jason D [ 27/Nov/12 ]

The solution to this may also apply to GRADLE-1050 . (Consider voting for that one too )

Comment by Kyle Mahan [ 06/May/13 ]

I agree that filtering duplicates would be a much friendlier (default?) behavior. Configuring with Ant's add/preserve/fail would also be satisfactory to me but feels a little un-Gradle-like.

task zip(type: Zip) {
  from ...
  onDuplicate 'fail'

as opposed to something like

task zip(type: Zip) {
  from ...
  onDuplicate { throw new RuntimeException("Duplicate: $it") }

I would be interested in helping to test Peter's patch or contributing code to this issue if it would help!

Generated at Wed Jun 30 12:14:38 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.