javaでtar.gzファイルを作る

なんだこれ超めんどくせぇ

private void archive(File input, File output) {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    try {
        fOut = new FileOutputStream(output);
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        addFile(tOut, input.getAbsolutePath(), input.getName());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            tOut.finish();
            tOut.close();
            gzOut.close();
            bOut.close();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private void addFile(TarArchiveOutputStream tOut, String path,
        String base) throws IOException {
    String entryName = base;
    File f = new File(path);
    System.out.println(base);
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    tOut.putArchiveEntry(tarEntry);
    if (f.isDirectory()) {
        tOut.closeArchiveEntry();
        for (File child : f.listFiles()) {
            addFile(tOut, child.getAbsolutePath(),
                        entryName + "/" + child.getName());
        }
    } else {
        FileInputStream fis = new FileInputStream(f);
        IOUtils.copy(fis, tOut);
        tOut.closeArchiveEntry();
        fis.close();
    }
}

参考:

http://stackoverflow.com/questions/13461393/compress-directory-to-tar-gz-with-commons-compress