jar ファイルから plugin.xml を抜き出す Groovy スクリプト

久しぶりに Groovy を使ってみたり。targetDir 下の *.jar ファイルにある plugin.xml ファイルを、jar ファイル名 + "-plugin.xml" というファイル名で outputDir へ抜き出すスクリプト。参考にするため。

import java.util.jar.JarFile

def outputDir = "C:/output/" // "/"で終わること
def targetDir = "C:/eclipse/plugins"

def count = 0
def buf = new byte[1024]

new File(targetDir).eachFileMatch(~".*.jar") {
    def jar = new JarFile(it);
    jar.entries().each({ entry ->
        if (entry.name.equals("plugin.xml")) {
            def outfile = new File(outputDir + new File(jar.name).name + "-" + entry.name)
            outfile.withOutputStream { os ->
                jar.getInputStream(entry).withStream { is ->
                    while ((count = is.read(buf, 0, buf.length)) != -1) {
                        os.write(buf, 0, count)
                    }
                }
            }
        }
    });
}