Example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream close

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream close

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this stream.

Usage

From source file:de.flapdoodle.embedmongo.extract.TgzExtractor.java

@Override
public void extract(RuntimeConfig runtime, File source, File destination, Pattern file) throws IOException {

    IProgressListener progressListener = runtime.getProgressListener();
    String progressLabel = "Extract " + source;
    progressListener.start(progressLabel);

    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
    try {/*from w  w w  . j a  v  a2s.c  o  m*/
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            if (file.matcher(entry.getName()).matches()) {
                //               System.out.println("File: " + entry.getName());
                if (tarIn.canReadEntryData(entry)) {
                    //                  System.out.println("Can Read: " + entry.getName());
                    long size = entry.getSize();
                    Files.write(tarIn, size, destination);
                    destination.setExecutable(true);
                    //                  System.out.println("DONE");
                    progressListener.done(progressLabel);
                }
                break;

            } else {
                //               System.out.println("SKIP File: " + entry.getName());
            }
        }

    } finally {
        tarIn.close();
        gzIn.close();
    }
}

From source file:eu.eubrazilcc.lvl.core.io.FileCompressor.java

/**
 * Uncompress the content of a tarball file to the specified directory.
 * @param srcFile - the tarball to be uncompressed
 * @param outDir - directory where the files (and directories) extracted from the tarball will be written
 * @return the list of files (and directories) extracted from the tarball
 * @throws IOException when an exception occurs on uncompressing the tarball
 *//*from  w  w w . j  a  v a  2  s.c o m*/
public static List<String> unGzipUnTar(final String srcFile, final String outDir) throws IOException {
    final List<String> uncompressedFiles = newArrayList();
    FileInputStream fileInputStream = null;
    BufferedInputStream bufferedInputStream = null;
    GzipCompressorInputStream gzipCompressorInputStream = null;
    TarArchiveInputStream tarArchiveInputStream = null;
    BufferedInputStream bufferedInputStream2 = null;

    try {
        forceMkdir(new File(outDir));
        fileInputStream = new FileInputStream(srcFile);
        bufferedInputStream = new BufferedInputStream(fileInputStream);
        gzipCompressorInputStream = new GzipCompressorInputStream(fileInputStream);
        tarArchiveInputStream = new TarArchiveInputStream(gzipCompressorInputStream);

        TarArchiveEntry entry = null;
        while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
            final File outputFile = new File(outDir, entry.getName());
            if (entry.isDirectory()) {
                // attempting to write output directory
                if (!outputFile.exists()) {
                    // attempting to create output directory
                    if (!outputFile.mkdirs()) {
                        throw new IOException("Cannot create directory: " + outputFile.getCanonicalPath());
                    }
                }
            } else {
                // attempting to create output file
                final byte[] content = new byte[(int) entry.getSize()];
                tarArchiveInputStream.read(content, 0, content.length);
                final FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                write(content, outputFileStream);
                outputFileStream.close();
                uncompressedFiles.add(outputFile.getCanonicalPath());
            }
        }
    } finally {
        if (bufferedInputStream2 != null) {
            bufferedInputStream2.close();
        }
        if (tarArchiveInputStream != null) {
            tarArchiveInputStream.close();
        }
        if (gzipCompressorInputStream != null) {
            gzipCompressorInputStream.close();
        }
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
    }
    return uncompressedFiles;
}

From source file:frameworks.Masken.java

public static void uncompressTarGZ(File tarFile, File dest) throws IOException {
    dest.mkdir();/*www  .  jav  a  2s.c  o  m*/
    TarArchiveInputStream tarIn = null;

    tarIn = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {// create a file with the same name as the tarEntry
        File destPath = new File(dest, tarEntry.getName());
        System.out.println("working: " + destPath.getCanonicalPath());
        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            if (!destPath.getParentFile().exists()) {
                destPath.getParentFile().mkdirs();
            }
            destPath.createNewFile();
            //byte [] btoRead = new byte[(int)tarEntry.getSize()];
            byte[] btoRead = new byte[1024];
            //FileInputStream fin 
            //  = new FileInputStream(destPath.getCanonicalPath());
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            int len = 0;

            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }

            bout.close();
            btoRead = null;

        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}

From source file:de.uzk.hki.da.pkg.ArchiveBuilder.java

/**
 * Unpacks the contents of the given archive into the given folder
 * /*  w w w  . j  a  va2s  .  com*/
 * @param srcFile The archive container file
 * @param destFolder The destination folder
 * @param uncompress Indicates if the archive file is compressed (tgz file) or not (tar file)
 * @throws Exception
 */
public void unarchiveFolder(File srcFile, File destFolder, boolean uncompress) throws Exception {

    FileInputStream fIn = new FileInputStream(srcFile);
    BufferedInputStream in = new BufferedInputStream(fIn);

    TarArchiveInputStream tIn = null;
    GzipCompressorInputStream gzIn = null;

    if (uncompress) {
        gzIn = new GzipCompressorInputStream(in);
        tIn = new TarArchiveInputStream(gzIn);
    } else
        tIn = new TarArchiveInputStream(fIn);

    TarArchiveEntry entry;
    do {
        entry = tIn.getNextTarEntry();

        if (entry == null)
            break;

        File entryFile = new File(destFolder.getAbsolutePath() + "/" + entry.getName());

        if (entry.isDirectory())
            entryFile.mkdirs();
        else {
            new File(entryFile.getAbsolutePath().substring(0, entryFile.getAbsolutePath().lastIndexOf('/')))
                    .mkdirs();

            FileOutputStream out = new FileOutputStream(entryFile);
            IOUtils.copy(tIn, out);
            out.close();
        }
    } while (true);

    tIn.close();
    if (gzIn != null)
        gzIn.close();
    in.close();
    fIn.close();
}

From source file:io.wcm.maven.plugins.nodejs.installation.TarUnArchiver.java

/**
 * Unarchives the arvive into the pBaseDir
 * @param baseDir/*  ww  w .java  2  s  . co m*/
 * @throws MojoExecutionException
 */
public void unarchive(String baseDir) throws MojoExecutionException {
    try {
        FileInputStream fis = new FileInputStream(archive);

        // TarArchiveInputStream can be constructed with a normal FileInputStream if
        // we ever need to extract regular '.tar' files.
        final TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis));

        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
        while (tarEntry != null) {
            // Create a file for this tarEntry
            final File destPath = new File(baseDir + File.separator + tarEntry.getName());
            if (tarEntry.isDirectory()) {
                destPath.mkdirs();
            } else {
                destPath.createNewFile();
                destPath.setExecutable(true);
                //byte [] btoRead = new byte[(int)tarEntry.getSize()];
                byte[] btoRead = new byte[8024];
                final BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
                int len = 0;

                while ((len = tarIn.read(btoRead)) != -1) {
                    bout.write(btoRead, 0, len);
                }

                bout.close();
            }
            tarEntry = tarIn.getNextTarEntry();
        }
        tarIn.close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not extract archive: '" + archive.getAbsolutePath() + "'", e);
    }

}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

/** Untar an input file into an output file.
        /*from   w w  w . ja v  a2s  . c  om*/
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.tar' extension. 
 * 
 * @param inputFile     the input .tar file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException 
 */
public static List<File> unTar(final File inputFile, final File outputDir) throws Exception {

    log.debug(
            String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            log.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
            if (!outputFile.exists()) {
                log.debug(String.format("Attempting to create output directory %s.",
                        outputFile.getAbsolutePath()));
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            log.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}

From source file:de.uzk.hki.da.pkg.TarGZArchiveBuilder.java

public void unarchiveFolder(File srcTar, File destFolder) throws Exception {

    FileInputStream fin = new FileInputStream(srcTar);
    BufferedInputStream in = new BufferedInputStream(fin);

    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tIn = new TarArchiveInputStream(gzIn);

    HashMap<String, Long> modDateMap = new HashMap<String, Long>();
    TarArchiveEntry entry;/*from w w w  .j  av  a  2s  .  com*/
    do {
        entry = tIn.getNextTarEntry();
        if (entry == null)
            break;
        logger.debug(entry.getName());

        String dstName = destFolder.getAbsolutePath() + "/" + entry.getName();
        File entryFile = new File(dstName);
        if (entry.isDirectory()) {
            entryFile.mkdirs();
            modDateMap.put(dstName, new Long(entry.getModTime().getTime()));
        } else {
            new File(entryFile.getAbsolutePath().substring(0, entryFile.getAbsolutePath().lastIndexOf('/')))
                    .mkdirs();

            FileOutputStream out = new FileOutputStream(entryFile);
            IOUtils.copy(tIn, out);
            out.close();
            entryFile.setLastModified(entry.getModTime().getTime());
        }
    } while (true);

    tIn.close();
    gzIn.close();
    in.close();
    fin.close();

    for (Map.Entry<String, Long> moddi : modDateMap.entrySet()) {
        String key = moddi.getKey();
        Long value = moddi.getValue();
        (new File(key)).setLastModified(value);
    }
}

From source file:com.espringtran.compressor4j.processor.TarProcessor.java

/**
 * Read from compressed file//from  w  w  w . j  av a2  s.  c o m
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    TarArchiveInputStream ais = new TarArchiveInputStream(bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        TarArchiveEntry entry = ais.getNextTarEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextTarEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:com.espringtran.compressor4j.processor.TarBz2Processor.java

/**
 * Read from compressed file//w ww . ja  va  2s  . c o m
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    BZip2CompressorInputStream cis = new BZip2CompressorInputStream(bais);
    TarArchiveInputStream ais = new TarArchiveInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        TarArchiveEntry entry = ais.getNextTarEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextTarEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:com.espringtran.compressor4j.processor.TarGzProcessor.java

/**
 * Read from compressed file//from  www .  j a v a  2 s.  co m
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    GzipCompressorInputStream cis = new GzipCompressorInputStream(bais);
    TarArchiveInputStream ais = new TarArchiveInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        TarArchiveEntry entry = ais.getNextTarEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextTarEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}