javaでtar.gz書庫を展開する

ant.jarに依存しています。引数がjava7のnioになっていますが、適当に対処してください。

package;

import java.util.zip.GZIPInputStream;
import java.nio.file.*;
import java.io.*;
import org.apache.tools.tar.*;

public class Main {

    public static void main(String[] args) throws Exception {

        uncompress(Paths.get(args[0]));
        
    }

    private static void uncompress(Path path) throws IOException {

        if(!path.toString().endsWith(".tar.gz"))
            throw new Error("extension must be tar.gz.");

        TarInputStream tin = new TarInputStream(new GZIPInputStream(new FileInputStream(path.toFile())));

        for(TarEntry tarEnt = tin.getNextEntry(); tarEnt != null; tarEnt = tin.getNextEntry()) {
            if(tarEnt.isDirectory()){
                new File(tarEnt.getName()).mkdir();
            }
            else {
                FileOutputStream fos = new FileOutputStream(new File(tarEnt.getName()));
                tin.copyEntryContents(fos);
            }
        }

        tin.close();

    }

    
}