英文:
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 将 `"$rootDir/destination_folder"` 解释为 `.zip` 文件的路径。它可能无法找到此 `.zip` 文件,即使能找到,它也会将所含文件限制为与 `"**/*.zip"` 匹配的文件,因此只会复制您的 `.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 "$rootDir/destination_folder"
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 "**/*.zip"
, 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: 'sourceDir', includes: ['**/*.zip']).each { zipFile ->
from zipTree(zipFile)
}
into 'targetDir'
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论