有没有一种方法可以使用Gradle从文件夹解压多个文件?

huangapple go评论67阅读模式
英文:

Is there a way to unzip multiple files from a folder using gradle?

问题

我正在尝试使用Gradle从一个文件夹解压多个`.zip`文件到另一个文件夹。我可以使用Gradle解压文件,但我正在努力找出如何在不必指定每个`.zip`文件名称的情况下解压目录中的所有`.zip`文件。我将提供我目前所拥有的示例。以下代码没有实现这一点,我不确定我需要做哪些更改,以便实现期望的结果,即从一个目录解压所有文件并将解压后的文件放入一个新目录。

任务解压(type: 复制) {
从 zipTree("$rootDir/destination_folder") {
包括 "**/*.zip"
到 "$new_folder"
}
}


<details>
<summary>英文:</summary>

I am trying to unzip multiple `.zip` files from a folder into another folder using Gradle. I am able to unzip files using Gradle, but I am trying to figure out how I can unzip all `.zip` files in a directory without having to specify the name of each `.zip` file. I will provide an example of what I currently have. The below code does not, and I am not sure what I need to change in order for me to achieve the desired result which is to unzip all the from one directory and place the unzipped files into a new directory.

task unzip(type: Copy) {
from zipTree("$rootDir/destination_folder") {
include "**/*.zip"
into "$new_folder"
}
}


</details>


# 答案1
**得分**: 1

方法 `zipTree` 用于解压单个 `.zip` 文件,并返回一个包含所有 `.zip` 文件内文件的 `FileTree`。在您当前的代码中,Gradle 将 `&quot;$rootDir/destination_folder&quot;` 解释为 `.zip` 文件的路径。它可能无法找到此 `.zip` 文件,即使能找到,它也会将所含文件限制为与 `&quot;**/*.zip&quot;` 匹配的文件,因此只会复制您的 `.zip` 文件内部的 `.zip` 文件。

您不应直接使用只能解析单个 `.zip` 文件的 `zipTree`,而应该从一个 `fileTree` 开始,以收集文件夹内的 `.zip` 文件。然后,您可以对每个这些文件使用 `zipTree`:

``` groovy
task unzip(type: Copy) {
    fileTree(dir: 'sourceDir', includes: ['**/*.zip']).each { zipFile ->
        from zipTree(zipFile)
    }
    into 'targetDir'
}
英文:

The method zipTree unzips a single .zip file and returns a FileTree with all the files inside the .zip. In your current code, Gradle interprets &quot;$rootDir/destination_folder&quot; as the path of the .zip file. It probably cannot find this .zip file but even if it could it would then limit the contained files to files that match &quot;**/*.zip&quot;, so only .zip files inside your .zip file would be copied.

Instead of directly using a zipTree that can only resolves a single .zip file you need to start with a fileTree to collect the .zip files inside your folder. You may then use zipTree on each of these files:

task unzip(type: Copy) {
    fileTree(dir: &#39;sourceDir&#39;, includes: [&#39;**/*.zip&#39;]).each { zipFile -&gt;
        from zipTree(zipFile)
    }
    into &#39;targetDir&#39;
}

huangapple
  • 本文由 发表于 2020年8月26日 04:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63586961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定