Newer
Older
osmCoverage / test / tools / Compless.java
package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;

public class Compless {
    
    /**
     * `.tar.gz`形式のファイルを指定のディレクトリにuncompless
     * 
     * @param outDir
     * @param gzipFile
     * @throws FileNotFoundException
     * @throws IOException 
     */
    public static void uncomplessTarGz(File outDir, File  gzipFile) throws FileNotFoundException, IOException {
        try (FileInputStream fis = new FileInputStream(gzipFile)) {
            try (GZIPInputStream gis = new GZIPInputStream(fis)) {
                try (TarArchiveInputStream tis = new TarArchiveInputStream(gis)) {
                    for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis.getNextTarEntry()) {
                        if (entry.isDirectory()) {
                            File newDir = new File(outDir, entry.getName());
                            try {
                                newDir.mkdirs();
                            }
                            catch(Exception e) {
                                throw new IOException("directory '" + newDir.getAbsolutePath() + "' cannot create.");
                            }
                        }
                        else {
                            File newfile = new File(outDir, entry.getName());
                            File parentDir = newfile.getParentFile();
                            try {
                                parentDir.mkdirs();
                            }
                            catch(Exception e) {
                                throw new IOException("directory '" + parentDir.getAbsolutePath() + "' cannot create.");
                            }
                            try (FileOutputStream fos = new FileOutputStream(newfile)) {
                                IOUtils.copy(tis, fos);  // Apache common-io
                            }
                        }
                    }
                }
            }
        }
    }
    
    
}