LoginSignup
0
0

More than 5 years have passed since last update.

Groovyで*.tar.gzファイルを展開する方法

Last updated at Posted at 2015-06-03

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0