我需要将文件添加到现有的zip存档中。我尝试使用Gradle Zip Task,但它覆盖了我的存档。
我现在已经解决了以下问题
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Paths
import java.util.function.Consumer
task AddToZip() {
doLast {
FileSystem fs = FileSystems.newFileSystem(Paths.get("$pathToZip"), null)
Files.walk(Paths.get("$rootDir/dir")).forEach(new Consumer<java.nio.file.Path>() {
@Override
void accept(java.nio.file.Path path) {
java.nio.file.Path dest = fs.getPath(path.toString().substring("$rootDir".length()))
if (path.toFile().isDirectory()) {
Files.createDirectory(dest)
return
}
Files.copy(path, dest)
}
})
fs.close()
}
}
还有其他解决方法吗?
最佳答案
您可以使用Project.zipTree(...)
从原始邮政编码创建FileTree
。然后可以将其传递给Zip.from(...)
task addToZip(type:Zip) {
from zipTree('path/to/original.zip')
from 'path/to/additional/file.txt'
archiveFileName = "updated.zip"
destinationDirectory = file("$buildDir/zips")
}
关于java - Gradle附加zip存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63776713/