Newer
Older
osmCoverage / src / tools / Compless.java
@yuu yuu on 24 Dec 2018 3 KB function: UnMapped.
package tools;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

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
                            }
                        }
                    }
                }
            }
        }
    }
    
    /**
     * 
     * @param sourceFile
     * @param zipFile
     * @throws FileNotFoundException 
     * @throws org.apache.commons.compress.archivers.ArchiveException 
     */
    public static void toZip(File sourceFile, File zipFile) throws FileNotFoundException, IOException, ArchiveException {
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(zipFile));
        try (ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out)) {
            os.putArchiveEntry(new ZipArchiveEntry(sourceFile.getName()));
            IOUtils.copy(new FileInputStream(sourceFile), os);
            os.closeArchiveEntry();
        }
    }
}