Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Return whether or not this entry represents a directory.

Usage

From source file:org.killbill.billing.beatrix.integration.osgi.util.SetupBundleWithAssertion.java

private static void unTar(final File inputFile, final File outputDir) throws IOException, ArchiveException {
    InputStream is = null;//  ww w .  j a v a  2s  . c  o m
    TarArchiveInputStream archiveInputStream = null;
    TarArchiveEntry entry;

    try {
        is = new FileInputStream(inputFile);
        archiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar",
                is);
        while ((entry = (TarArchiveEntry) archiveInputStream.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);
                ByteStreams.copy(archiveInputStream, outputFileStream);
                outputFileStream.close();
            }
        }
    } finally {
        if (archiveInputStream != null) {
            archiveInputStream.close();
        }
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.kitesdk.cli.commands.TarImportCommand.java

@Override
public int run() throws IOException {
    Preconditions.checkArgument(targets != null && targets.size() == 2,
            "Tar path and target dataset URI are required.");

    Preconditions.checkArgument(SUPPORTED_TAR_COMPRESSION_TYPES.contains(compressionType),
            "Compression type " + compressionType + " is not supported");

    String source = targets.get(0);
    String datasetUri = targets.get(1);

    long blockSize = getConf().getLong("dfs.blocksize", DEFAULT_BLOCK_SIZE);

    int success = 0;

    View<TarFileEntry> targetDataset;
    if (Datasets.exists(datasetUri)) {
        console.debug("Using existing dataset: {}", datasetUri);
        targetDataset = Datasets.load(datasetUri, TarFileEntry.class);
    } else {//from   ww  w  .j a v  a 2s .c o  m
        console.info("Creating new dataset: {}", datasetUri);
        DatasetDescriptor.Builder descriptorBuilder = new DatasetDescriptor.Builder();
        descriptorBuilder.format(Formats.AVRO);
        descriptorBuilder.schema(TarFileEntry.class);
        targetDataset = Datasets.create(datasetUri, descriptorBuilder.build(), TarFileEntry.class);
    }

    DatasetWriter<TarFileEntry> writer = targetDataset.newWriter();

    // Create a Tar input stream wrapped in appropriate decompressor
    // TODO: Enhancement would be to use native compression libs
    TarArchiveInputStream tis;
    CompressionType tarCompressionType = CompressionType.NONE;

    if (compressionType.isEmpty()) {
        if (source.endsWith(".tar")) {
            tarCompressionType = CompressionType.NONE;
        } else if (source.endsWith(".tar.gz")) {
            tarCompressionType = CompressionType.GZIP;
        } else if (source.endsWith(".tar.bz2")) {
            tarCompressionType = CompressionType.BZIP2;
        }
    } else if (compressionType.equals("gzip")) {
        tarCompressionType = CompressionType.GZIP;
    } else if (compressionType.equals("bzip2")) {
        tarCompressionType = CompressionType.BZIP2;
    } else {
        tarCompressionType = CompressionType.NONE;
    }

    console.info("Using {} compression", tarCompressionType);

    switch (tarCompressionType) {
    case GZIP:
        tis = new TarArchiveInputStream(new GzipCompressorInputStream(open(source)));
        break;
    case BZIP2:
        tis = new TarArchiveInputStream(new BZip2CompressorInputStream(open(source)));
        break;
    case NONE:
    default:
        tis = new TarArchiveInputStream(open(source));
    }

    TarArchiveEntry entry;

    try {
        int count = 0;
        while ((entry = tis.getNextTarEntry()) != null) {
            if (!entry.isDirectory()) {
                long size = entry.getSize();
                if (size >= blockSize) {
                    console.warn(
                            "Entry \"{}\" (size {}) is larger than the "
                                    + "HDFS block size of {}. This may result in remote block reads",
                            new Object[] { entry.getName(), size, blockSize });
                }

                byte[] buf = new byte[(int) size];
                try {
                    IOUtils.readFully(tis, buf, 0, (int) size);
                } catch (IOException e) {
                    console.error("Did not read entry {} successfully (entry size {})", entry.getName(), size);
                    success = 1;
                    throw e;
                }
                writer.write(TarFileEntry.newBuilder().setFilename(entry.getName())
                        .setFilecontent(ByteBuffer.wrap(buf)).build());
                count++;
            }
        }
        console.info("Added {} records to \"{}\"", count, targetDataset.getDataset().getName());
    } finally {
        IOUtils.closeStream(writer);
        IOUtils.closeStream(tis);
    }

    return success;
}

From source file:org.lobid.lodmill.TarReader.java

@Override
public void process(final Reader reader) {
    TarArchiveInputStream tarInputStream = null;
    try {//from  ww w  .j  a  v a2  s. c o  m
        tarInputStream = new TarArchiveInputStream(new ReaderInputStream(reader));
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                byte[] buffer = new byte[(int) entry.getSize()];
                while ((tarInputStream.read(buffer)) > 0) {
                    getReceiver().process(new StringReader(new String(buffer)));
                }
            }
        }
        tarInputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(tarInputStream);
    }
}

From source file:org.moe.cli.utils.ArchiveUtils.java

public static void untarArchive(ArchiveInputStream archStrim, File destination) throws IOException {
    TarArchiveEntry entry;

    while ((entry = (TarArchiveEntry) archStrim.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            String dest = entry.getName();
            File destFolder = new File(destination, dest);
            if (!destFolder.exists()) {
                destFolder.mkdirs();// ww  w.  j  av  a2  s .com
            }
        } else {
            int count;
            byte[] data = new byte[2048];
            File d = new File(destination, entry.getName());

            if (!d.getParentFile().exists()) {
                d.getParentFile().mkdirs();
            }

            if (entry.isSymbolicLink()) {
                String link = entry.getLinkName();

                String entryName = entry.getName();
                int parentIdx = entryName.lastIndexOf("/");

                String newLink = entryName.substring(0, parentIdx) + "/" + link;
                File destFile = new File(destination, newLink);
                File linkFile = new File(destination, entryName);

                Files.createSymbolicLink(Paths.get(linkFile.getPath()), Paths.get(destFile.getPath()));

            } else {
                FileOutputStream fos = new FileOutputStream(d);
                BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
                while ((count = archStrim.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }
    }
}

From source file:org.mulima.internal.freedb.FreeDbTarDaoImpl.java

/**
 * {@inheritDoc}//from   w  w  w.  j  a  v a 2 s.com
 */
@Override
public List<Disc> getAllDiscsFromOffset(int startNum, int numToRead) {
    FileInputStream fin = null;
    BufferedInputStream bfin = null;
    TarArchiveInputStream tin = null;
    List<Disc> discs = new ArrayList<Disc>();
    try {
        fin = new FileInputStream(tarArchive);
        bfin = new BufferedInputStream(fin);
        tin = new TarArchiveInputStream(bfin);

        int currentNum = 0;
        TarArchiveEntry entry = tin.getNextTarEntry();
        ProgressBar progress = new SLF4JProgressBar("TAR getDiscs", numToRead);
        while (entry != null && (numToRead < 0 || currentNum < startNum + numToRead)) {
            if (!entry.isDirectory() && currentNum >= startNum) {
                logger.debug("Loading: " + entry.getName());
                int offset = 0;
                byte[] content = new byte[(int) entry.getSize()];
                while (offset < content.length) {
                    offset += tin.read(content, offset, content.length - offset);
                }
                Disc disc = bytesToDisc(content);
                if (disc == null) {
                    logger.warn("Invalid file: " + entry.getName());
                } else {
                    logger.debug(disc.toString());
                    discs.add(disc);
                }
            }

            entry = tin.getNextTarEntry();
            currentNum++;
            progress.next();
        }

        if (entry == null) {
            progress.done();
        }
    } catch (IOException e) {
        logger.error("Problem reading tar archive.", e);
        throw new UncheckedIOException("Problem reading tar archive.", e);
    } finally {
        try {
            if (tin != null) {
                tin.close();
            } else if (bfin != null) {
                bfin.close();
            } else if (fin != null) {
                fin.close();
            }
        } catch (IOException e) {
            logger.error("Problem closing streams.", e);
        }
    }
    return discs;
}

From source file:org.mycore.common.MCRUtils.java

/**
 * Extracts files in a tar archive. Currently works only on uncompressed tar files.
 * //from w w  w .  jav a2 s  .co  m
 * @param source
 *            the uncompressed tar to extract
 * @param expandToDirectory
 *            the directory to extract the tar file to
 * @throws IOException
 *             if the source file does not exists
 */
public static void untar(Path source, Path expandToDirectory) throws IOException {
    try (TarArchiveInputStream tain = new TarArchiveInputStream(Files.newInputStream(source))) {
        TarArchiveEntry tarEntry;
        FileSystem targetFS = expandToDirectory.getFileSystem();
        HashMap<Path, FileTime> directoryTimes = new HashMap<>();
        while ((tarEntry = tain.getNextTarEntry()) != null) {
            Path target = MCRPathUtils.getPath(targetFS, tarEntry.getName());
            Path absoluteTarget = expandToDirectory.resolve(target).normalize().toAbsolutePath();
            if (tarEntry.isDirectory()) {
                Files.createDirectories(expandToDirectory.resolve(absoluteTarget));
                directoryTimes.put(absoluteTarget,
                        FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime()));
            } else {
                if (Files.notExists(absoluteTarget.getParent())) {
                    Files.createDirectories(absoluteTarget.getParent());
                }
                Files.copy(tain, absoluteTarget, StandardCopyOption.REPLACE_EXISTING);
                Files.setLastModifiedTime(absoluteTarget,
                        FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime()));
            }
        }
        //restore directory dates
        Files.walkFileTree(expandToDirectory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Path absolutePath = dir.normalize().toAbsolutePath();
                Files.setLastModifiedTime(absolutePath, directoryTimes.get(absolutePath));
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}

From source file:org.nd4j.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 *
 * @param file the file to extract to/*from   w  ww  .  j  a  v  a 2s. c  om*/
 * @param dest the destination directory
 * @throws IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip") || file.endsWith(".jar")) {
        try (ZipInputStream zis = new ZipInputStream(fin)) {
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(dest + File.separator + fileName);

                if (ze.isDirectory()) {
                    newFile.mkdirs();
                    zis.closeEntry();
                    ze = zis.getNextEntry();
                    continue;
                }

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(data)) > 0) {
                    fos.write(data, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
                log.debug("File extracted: " + newFile.getAbsoluteFile());
            }

            zis.closeEntry();
        }
    } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry;
        /* Read the tar entries using the getNextEntry method **/
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            log.info("Extracting: " + entry.getName());
            /* If the entry is a directory, create the directory. */

            if (entry.isDirectory()) {
                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /*
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             */
            else {
                int count;
                try (FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                        BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);) {
                    while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                        destStream.write(data, 0, count);
                    }

                    destStream.flush();
                    IOUtils.closeQuietly(destStream);
                }
            }
        }

        // Close the input stream
        tarIn.close();
    } else if (file.endsWith(".gz")) {
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        try (GZIPInputStream is2 = new GZIPInputStream(fin);
                OutputStream fos = FileUtils.openOutputStream(extracted)) {
            IOUtils.copyLarge(is2, fos);
            fos.flush();
        }
    } else {
        throw new IllegalStateException(
                "Unable to infer file type (compression format) from source file name: " + file);
    }
    target.delete();
}

From source file:org.ngrinder.common.util.CompressionUtil.java

/**
 * Untar an input file into an output file.
 * // w  ww . ja  v  a2s .  com
 * The output file is created in the output folder, having the same name as the input file,
 * minus the '.tar' extension.
 * 
 * @param inFile
 *            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 inFile, final File outputDir) {
    final List<File> untaredFiles = new LinkedList<File>();
    try {
        final InputStream is = new FileInputStream(inFile);
        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 {
                File parentFile = outputFile.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                final OutputStream outputFileStream = new FileOutputStream(outputFile);

                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
                if (FilenameUtils.isExtension(outputFile.getName(), EXECUTABLE_EXTENSION)) {
                    outputFile.setExecutable(true, true);
                }
                outputFile.setReadable(true);
                outputFile.setWritable(true, true);
            }
            untaredFiles.add(outputFile);
        }
        debInputStream.close();
    } catch (Exception e) {
        LOGGER.error("Error while untar {} file by {}", inFile, e.getMessage());
        LOGGER.debug("Trace is : ", e);
        throw new NGrinderRuntimeException("Error while untar file", e);
    }
    return untaredFiles;
}

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Untar an input file into an output file.
 * The output file is created in the output folder, having the same name as
 * the input file, minus the '.tar' extension.
 *
 * @param inFile    the input .tar file//  w  w  w.jav  a  2 s  .c o  m
 * @param outputDir the output directory file.
 * @return The {@link List} of {@link File}s with the untared content.
 */
@SuppressWarnings("resource")
public static List<File> untar(final File inFile, final File outputDir) {
    final List<File> untaredFiles = new LinkedList<File>();
    InputStream is = null;
    TarArchiveInputStream debInputStream = null;
    try {
        is = new FileInputStream(inFile);
        debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry;
        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 {
                File parentFile = outputFile.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                try {
                    IOUtils.copy(debInputStream, outputFileStream);
                } finally {
                    IOUtils.closeQuietly(outputFileStream);
                }

                if (FilenameUtils.isExtension(outputFile.getName(), EXECUTABLE_EXTENSION)) {
                    outputFile.setExecutable(true, true);
                }
                outputFile.setReadable(true);
                outputFile.setWritable(true, true);
            }
            untaredFiles.add(outputFile);
        }
    } catch (Exception e) {
        throw processException("Error while untar file", e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(debInputStream);
    }
    return untaredFiles;
}

From source file:org.onebusaway.util.FileUtility.java

/**
 * Untar an input file into an output file.
 * //from w  ww  .  j a va2  s . co m
 * 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 List<File> unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {

    _log.info(
            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.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
            if (!outputFile.exists()) {
                _log.info(String.format("Attempting to create output directory %s.",
                        outputFile.getAbsolutePath()));
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("CHUNouldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            _log.info(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;
}