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:com.google.cloud.public_datasets.nexrad2.GcsUntar.java

private static File[] unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {
    // from//from   w  w w. jav  a2  s. c o  m
    // http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java/7556307#7556307
    log.info("tar xf " + inputFile + " to " + outputDir);
    final List<File> untaredFiles = new ArrayList<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()) {
            if (!outputFile.exists()) {
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles.toArray(new File[0]);
}

From source file:cgs_lda_multicore.DataModel.DataPreparation.java

public static LDADataset readDatasetCORE(String fileName) throws Exception {
    try {//from  w w w . jav a2 s.c o  m
        // Read document file.
        BufferedReader reader = null;

        if (fileName.endsWith(".tar.gz")) {
            // This case read from .tar.gz file.
            TarArchiveInputStream tAIS = new TarArchiveInputStream(
                    new GZIPInputStream(new FileInputStream(fileName)));
            TarArchiveEntry tarArchiveEntry;

            while ((tarArchiveEntry = tAIS.getNextTarEntry()) != null) {
                if (tarArchiveEntry.isFile()) {
                    reader = new BufferedReader(
                            new InputStreamReader(new FileInputStream(tarArchiveEntry.getFile()), "UTF-8"));
                    String line;

                    while ((line = reader.readLine()) != null) {
                        // Process line, each line is a json of a document.
                    }
                    reader.close();
                }
            }
            tAIS.close();
        }
        return null;
    } catch (Exception e) {
        System.out.println("Read Dataset Error: " + e.getMessage());
        e.printStackTrace();
        return null;
    }
}

From source file:com.audiveris.installer.Expander.java

/**
 * Untar an input file into an output directory.
 *
 * @param inFile the input .tar file//  w ww. j ava2  s.  com
 * @param outDir the output directory
 * @throws IOException
 * @throws FileNotFoundException
 * @throws ArchiveException
 *
 * @return The list of files with the untared content.
 */
public static List<File> unTar(final File inFile, final File outDir)
        throws FileNotFoundException, IOException, ArchiveException {
    logger.debug("Untaring {} to dir {}", inFile, outDir);
    assert inFile.getName().endsWith(TAR_EXT);

    final List<File> untaredFiles = new ArrayList<File>();
    final InputStream is = new FileInputStream(inFile);
    final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    ArchiveEntry entry;

    while ((entry = tis.getNextEntry()) != null) {
        final File outFile = new File(outDir, entry.getName());

        if (entry.isDirectory()) {
            logger.debug("Attempting to write output dir {}", outFile);

            if (!outFile.exists()) {
                logger.debug("Attempting to create output dir {}", outFile);

                if (!outFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s", outFile.getAbsolutePath()));
                }
            }
        } else {
            logger.debug("Creating output file {}", outFile);
            streamToFile(tis, outFile);
        }

        untaredFiles.add(outFile);
    }

    tis.close();

    return untaredFiles;
}

From source file:com.cip.crane.agent.utils.FileExtractUtils.java

/** Untar an input file into an output file.
        /*from  ww w. j a  v a  2  s.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 FileNotFoundException, IOException, ArchiveException {
    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()) {
            if (!outputFile.exists()) {
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();
    return untaredFiles;
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void decompress(InputStream tarGzInputStream, Path outDir) throws IOException {
    GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream);
    TarArchiveEntry entry;/*from  www  .j  ava2s.co  m*/
    int bufferSize = 1024;
    while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) {
        String entryName = entry.getName();
        // strip out the leading directory like the --strip tar argument
        String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1);
        if (entryNameWithoutLeadingDir.isEmpty()) {
            continue;
        }
        Path outFile = outDir.resolve(entryNameWithoutLeadingDir);
        if (entry.isDirectory()) {
            outFile.toFile().mkdirs();
            continue;
        }
        int count;
        byte data[] = new byte[bufferSize];
        BufferedOutputStream fios = new BufferedOutputStream(new FileOutputStream(outFile.toFile()),
                bufferSize);
        while ((count = tarInput.read(data, 0, bufferSize)) != -1) {
            fios.write(data, 0, count);
        }
        fios.close();
    }
    tarInput.close();
    gzipStream.close();
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void decompressFromStream2(InputStream inputStream) throws IOException {
    GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(inputStream);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream);
    TarArchiveEntry entry;//w  ww .  j  a v  a 2  s  .c o m
    int bufferSize = 1024;
    while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) {
        String entryName = entry.getName();
        // strip out the leading directory like the --strip tar argument
        String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1);
        if (entryNameWithoutLeadingDir.isEmpty()) {
            continue;
        }
        if (entry.isDirectory()) {
            System.out.println("found dir " + entry.getName());
        } else {
            int count;
            byte data[] = new byte[bufferSize];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((count = tarInput.read(data, 0, bufferSize)) != -1) {
                baos.write(data, 0, count);
            }
            JarOutputStream jarOutputStream = new JarOutputStream(baos);
            System.out.println(new String(baos.toByteArray()));
        }
    }
    tarInput.close();
    gzipStream.close();
}

From source file:at.illecker.sentistorm.commons.util.io.IOUtils.java

public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) {
    try {// w w  w.j  a v a  2s.  c  om
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(inputTarGzStream);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        // read Tar entries
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            if (logging) {
                LOG.info("Extracting: " + outDir + File.separator + entry.getName());
            }
            if (entry.isDirectory()) { // create directory
                File f = new File(outDir + File.separator + entry.getName());
                f.mkdirs();
            } else { // decompress file
                int count;
                byte data[] = new byte[EXTRACT_BUFFER_SIZE];

                FileOutputStream fos = new FileOutputStream(outDir + File.separator + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, EXTRACT_BUFFER_SIZE);
                while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }

        // close input stream
        tarIn.close();

    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    }
}

From source file:gdt.data.entity.ArchiveHandler.java

private static boolean hasPropertyIndexInTarStream(TarArchiveInputStream tis) {
    try {//from   w  w  w .j a v  a2 s.c om
        TarArchiveEntry entry = null;
        String entryName$;
        while ((entry = tis.getNextTarEntry()) != null) {
            entryName$ = entry.getName();
            if (entryName$.equals(Entigrator.PROPERTY_INDEX)) {
                tis.close();
                return true;
            }
        }
        tis.close();
        return false;
    } catch (Exception e) {
        Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString());
        return false;
    }
}

From source file:deodex.tools.TarGzUtils.java

/** Untar an input file into an output file.
        /*  ww w  . j a 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 FileNotFoundException, IOException, ArchiveException {

    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()) {
            Logger.appendLog("Attempting to write output directory . " + outputFile.getAbsolutePath());
            if (!outputFile.exists()) {
                Logger.appendLog("Attempting to create output directory ." + outputFile.getAbsolutePath());
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            outputFile.getParentFile().mkdirs();
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}

From source file:adams.core.io.TarUtils.java

/**
 * Lists the files stored in the tar file.
 *
 * @param input   the tar file to obtain the file list from
 * @param listDirs   whether to include directories in the list
 * @return      the stored files/*from   ww w  . ja  v a 2 s.  c o m*/
 */
public static List<File> listFiles(File input, boolean listDirs) {
    List<File> result;
    FileInputStream fis;
    TarArchiveInputStream archive;
    TarArchiveEntry entry;

    result = new ArrayList<>();
    archive = null;
    fis = null;
    try {
        fis = new FileInputStream(input.getAbsolutePath());
        archive = openArchiveForReading(input, fis);
        while ((entry = archive.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                if (listDirs)
                    result.add(new File(entry.getName()));
            } else {
                result.add(new File(entry.getName()));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        FileUtils.closeQuietly(fis);
        if (archive != null) {
            try {
                archive.close();
            } catch (Exception e) {
                // ignored
            }
        }
    }

    return result;
}