Unzip Archive with Groovy

前端 未结 9 1184
深忆病人
深忆病人 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:35

    The Groovy common extension project provides this functionality for Groovy 2.0 and above: https://github.com/timyates/groovy-common-extensions

    0 讨论(0)
  • 2020-12-15 04:37

    Although taking the question a bit into another direction, I started off using Groovy for a DSL that I was building, but ended up using Gradle as a starting point to better handle a lot of the file-based tasks that I wanted to do (eg., unzip and untar files, execute other programs, etc). Gradle builds on what groovy can do, and can be extended further via plugins.

    // build.gradle
    task doUnTar << {
        copy {
            // tarTree uses file ext to guess compression, or may be specific
            from tarTree(resources.gzip('foo.tar.gz'))
            into getBuildDir()
        }
    }
    
    task doUnZip << {
        copy {
            from zipTree('bar.zip')
            into getBuildDir()
        }
    }
    

    Then, for example (this extracts the bar.zip and foo.tgz into the directory build):

    $ gradle doUnZip
    $ gradle doUnTar
    
    0 讨论(0)
  • 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\\'))
    }
    
    0 讨论(0)
  • 2020-12-15 04:41

    In my experience, the best way to do this is to use the Antbuilder:

    def ant = new AntBuilder()   // create an antbuilder
    
    ant.unzip(  src:"your-src.zip",
                dest:"your-dest-directory",
                overwrite:"false" )
    

    This way you aren't responsible for doing all the complicated stuff - ant takes care of it for you. Obviously if you need something more granular then this isn't going to work, but for most 'just unzip this file' scenarios this is really effective.

    To use antbuilder, just include ant.jar and ant-launcher.jar in your classpath.

    0 讨论(0)
  • 2020-12-15 04:41
    def zip(String s){
        def targetStream = new ByteArrayOutputStream()
        def zipStream = new GZIPOutputStream(targetStream)
        zipStream.write(s.getBytes())
        zipStream.close()
        def zipped = targetStream.toByteArray()
        targetStream.close()
        return zipped.encodeBase64()
    }
    
    0 讨论(0)
  • 2020-12-15 04:43

    Maybe Groovy doesn't have 'native' support for zip files, but it is still pretty trivial to work with them.

    I'm working with zip files and the following is some of the logic I'm using:

    def zipFile = new java.util.zip.ZipFile(new File('some.zip'))
    
    zipFile.entries().each {
       println zipFile.getInputStream(it).text
    }
    

    You can add additional logic using a findAll method:

    def zipFile = new java.util.zip.ZipFile(new File('some.zip'))
    
    zipFile.entries().findAll { !it.directory }.each {
       println zipFile.getInputStream(it).text
    }
    
    0 讨论(0)
提交回复
热议问题