[GRADLE-3025] How can I expand only a particular subdirectory of a zip(Tree)? Created: 14/Feb/14  Updated: 05/Jan/17  Resolved: 05/Jan/17

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: None

Type: Task
Reporter: Gradle Forums Assignee: Unassigned
Resolution: Duplicate Votes: 26


 Description   

How can I expand contents from a zipTree in a way that only the contents of a given sub directory gets extracted



 Comments   
Comment by Gradle Forums [ 14/Feb/14 ]

Use an include filter:

task unzip(type: Copy) {
from zipTree(zipFile)
into "unzipped"
include "dir/to/unzip/**"
}

Comment by Gradle Forums [ 14/Feb/14 ]

Hi Peter thank you this has but this does unzip's but it extracts the all directory tree and not the contents from that given subdirectory.
Say if the contents of the zip where:
/A---+

B---C

D----E

-----F
If the include is "A/D", I was expecting to have only
E
F
In the output
Comment by Gradle Forums [ 14/Feb/14 ]

> If the include is "A/D", I was expecting to have only E F In the output

That's not how include works. Ideally, you could achieve your goal with rename. However, rename currently operates on file names rather than file paths. What you can do is to add another copy task/action that copies everything below A/D to a new place. Or, code a solution based on _zipTree(zipFile).visit

{ ... }

_.

Comment by Gradle Forums [ 14/Feb/14 ]

Thank you, Peter

Comment by Gradle Forums [ 14/Feb/14 ]

Vote++ for a "rename" variant that can operate on full file paths

Comment by Gradle Forums [ 14/Feb/14 ]

You can use the `eachFile {}` hook to do this.

task unzip(type: Copy) {
from zipTree(zipFile)
into "unzipped"
eachFile { FileCopyDetails fcp ->
if (fcp.relativePath.pathString.startsWith("dir/to/unzip/")) {
// remap the file to the root
fcp.relativePath = new RelativePath(fcp.file, fcp.relativePath.segments[3..-1])
} else {
fcp.exclude()
}
}
}

That's untested, but it should be pretty close.

docs:

[1]http://www.gradle.org/docs/current/ja...
[2]http://gradle.org/docs/current/dsl/or...
----------------------------------------------------------------------------------------
[1] http://www.gradle.org/docs/current/javadoc/org/gradle/api/file/RelativePath.html
[2] http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:eachFile(groovy.lang.Closure)

Comment by Gradle Forums [ 14/Feb/14 ]

@Luke
it is necessary to coerce the segments back into a String Array after removing elements. The code below works but has the unintended side effect of also copying the original directory structure with no files in it. Any thoughts?
eachFile { FileCopyDetails fcp ->
if (fcp.relativePath.pathString.startsWith(appName)) {
// remap the file to the root
def segments = fcp.relativePath.segments
def pathsegments =segments[1..-1] as String[]
fcp.relativePath = new RelativePath(!fcp.file.isDirectory(), pathsegments)
}
else {
fcp.exclude()
}
}

Comment by Gradle Forums [ 14/Feb/14 ]

You need to specify to ignore empty dirs:

task unzip(type: Copy) {
...
includeEmptyDirs = false
}

Comment by Gradle Forums [ 14/Feb/14 ]

We actually do have directories in the structure that are empty that we would like to unzip. Any Ideas?

Comment by Gradle Forums [ 14/Feb/14 ]

This is a limitation of the API. You could keep track of what all the directories will be in the eachFile, and use a `doLast

{ project.delete "remapped dir" }

`.

Comment by Gradle Forums [ 14/Feb/14 ]

I'm hitting this same issue. Utility could be improved if the zipTree method took argument for a directory within the archive from which to root the tree. For example:

task unzip(type: Copy) {
from zipTree(zipFile, root: 'directory/in/the/zip/to/consider/as/root')
into destDir
include 'paths/relative/to/root'
}

Comment by Ned Twigg [ 26/Jan/16 ]

I encounter this issue almost every time I try to pull a new tool into gradle land. Coming up on 2 years old. Here's how to solve this using Gulp: http://stackoverflow.com/questions/25651264/copy-only-files-inside-a-folder-in-gulp

Comment by Benjamin Muschko [ 15/Nov/16 ]

As announced on the Gradle blog we are planning to completely migrate issues from JIRA to GitHub.

We intend to prioritize issues that are actionable and impactful while working more closely with the community. Many of our JIRA issues are inactionable or irrelevant. We would like to request your help to ensure we can appropriately prioritize JIRA issues you’ve contributed to.

Please confirm that you still advocate for your JIRA issue before December 10th, 2016 by:

  • Checking that your issues contain requisite context, impact, behaviors, and examples as described in our published guidelines.
  • Leave a comment on the JIRA issue or open a new GitHub issue confirming that the above is complete.

We look forward to collaborating with you more closely on GitHub. Thank you for your contribution to Gradle!

Comment by David Meibusch [ 16/Nov/16 ]

+1 I still believe this is a valuable use case.

Comment by Stalle [ 16/Nov/16 ]

+1, I do too. I don't understand why it should not be possible except for the extra effort to support it. Currently I typically extract everything and use only parts, wasting a lot of disk space.

Comment by Kenzie Togami [ 05/Jan/17 ]

For all tracking this, I've made a new GitHub issue at https://github.com/gradle/gradle/issues/1108.

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