Unzip Archive with Groovy

前端 未结 9 1224
深忆病人
深忆病人 2020-12-15 04:00

is there a built-in support in Groovy to handle Zip files (the groovy way)?

Or do i have to use Java\'s java.util.zip.ZipFile to process Zip files in Groovy ?

9条回答
  •  一个人的身影
    2020-12-15 04:40

    The below groovy methods will unzip into specific folder (C:\folder). Hope this helps.

    import org.apache.commons.io.FileUtils
    import java.nio.file.Files
    import java.nio.file.Paths
    import java.util.zip.ZipFile
    
    def unzipFile(File file) {
        cleanupFolder()
        def zipFile = new ZipFile(file)
        zipFile.entries().each { it ->
            def path = Paths.get('c:\\folder\\' + it.name)
            if(it.directory){
                Files.createDirectories(path)
            }
            else {
                def parentDir = path.getParent()
                if (!Files.exists(parentDir)) {
                    Files.createDirectories(parentDir)
                }
                Files.copy(zipFile.getInputStream(it), path)
            }
        }
    }
    
    private cleanupFolder() {
        FileUtils.deleteDirectory(new File('c:\\folder\\'))
    }
    

提交回复
热议问题