Apache Commons Compressを使用して展開してみる。
とりあえず以下のようなコードでできた。(Windows 7で確認した)
#!/bin/env groovy
@Grab('org.apache.commons:commons-compress:1.+')
import org.apache.commons.compress.compressors.gzip.*
import org.apache.commons.compress.archivers.tar.*
import org.apache.commons.compress.utils.*
new BufferedInputStream(new GzipCompressorInputStream(new FileInputStream('./archive1.tar.gz'))).with { gzipInputStream ->
new TarArchiveInputStream(gzipInputStream, 64 * 1024).with { tarInputStream -> // TODO: バッファサイズは適当
def tarEntry
while ((tarEntry = tarInputStream.nextTarEntry) != null) {
def entryFile = new File("./${tarEntry.name}")
if (tarEntry.isDirectory()) {
entryFile.mkdirs()
}
else {
if (!entryFile.parentFile.exists()) {
entryFile.parentFile.mkdirs()
}
entryFile.withOutputStream { outputStream ->
IOUtils.copy(tarInputStream, outputStream, 64 * 1024) // TODO: バッファサイズは適当
}
entryFile.with {
// TODO: 更新日時以外は反映していない
lastModified = tarEntry.modTime.time
}
}
}
}
}
tarの展開については、展開後に更新日時だけ反映している。更新日時以外の権限などは反映していない。
実際には、これに加えて、FTPダウンロード、MD5ハッシュ値判定、AsperaConnectダウンロード、SFTPアップロードなども一緒にやっているが、ここでは割愛した。
参考資料
http://groovyarekore.blogspot.jp/2010/09/groovyapache-commons-compresstar_11.html
http://groovyarekore.blogspot.jp/2010/09/groovyapache-commons-compressgzip_20.html