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:com.boxupp.utilities.PuppetUtilities.java

private void extrectFile(String saveFilePath, String moduleDirPath, String moduleName) {
    try {//from  w  ww .  j a v  a 2  s .co  m
        File file = new File(saveFilePath);
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
        TarArchiveEntry entry = null;
        int length = 0;
        File unzipFile = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                if (length == 0) {
                    File f = new File(moduleDirPath + entry.getName());
                    f.mkdirs();
                    unzipFile = f;
                    length++;
                } else {
                    File f = new File(moduleDirPath + entry.getName());
                    f.mkdirs();
                }
            } else {
                int count;
                byte data[] = new byte[4096];
                FileOutputStream fos;
                fos = new FileOutputStream(moduleDirPath + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, 4096);
                while ((count = tarIn.read(data, 0, 4096)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }

        }
        File renamedFile = new File(moduleDirPath + "/" + moduleName);
        if (unzipFile.isDirectory()) {
            unzipFile.renameTo(renamedFile);
        }
        tarIn.close();

    } catch (IOException e) {
        logger.error("Error in unzip the module file :" + e.getMessage());
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * extract tarfile to constituent parts processing gzips along the way
 * yyyyMMdd.tar->/yyyyMMdd/INode-CH_RNC01/A2010...gz
 *///from  ww w .j a  va  2s  .c  om
protected void untar(File tf) throws FileNotFoundException {

    try {
        TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(tf));
        TarArchiveEntry t1 = null;
        while ((t1 = tais.getNextTarEntry()) != null) {
            if (t1.isDirectory()) {
                if (t1.getName().contains("account"))
                    identifier = ".vcc";
                else
                    identifier = "";
            } else {
                String fn = t1.getName().substring(t1.getName().lastIndexOf("/"));
                File f = new File(getCalTempPath() + fn);
                FileOutputStream fos = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

                int n = 0;
                byte[] content = new byte[BUFFER];
                while (-1 != (n = tais.read(content))) {
                    fos.write(content, 0, n);
                }

                bos.flush();
                bos.close();
                fos.close();

                File unz = null;
                if (f.getName().endsWith("zip"))
                    unz = unzip3(f);
                else
                    unz = ungzip(f);

                if (unz != null)
                    allfiles.add(unz);
                f.delete();
            }
        }
        tais.close();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

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;
    do {// ww  w.  ja v a 2 s .  c om
        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:gdt.data.entity.ArchiveHandler.java

/**
 * Extract tar archive stream into the target directory
 * @param targetDirectory$ the path of the target directory
 * @param tis the tar archive input stream.
 *//* w  ww  .j ava2 s  .c  o m*/
public static void extractEntitiesFromTar(String targetDirectory$, TarArchiveInputStream tis) {
    try {

        TarArchiveEntry entry = null;
        File outputFile;
        File outputDir;
        FileOutputStream outputStream;
        while ((entry = tis.getNextTarEntry()) != null) {

            outputFile = new File(targetDirectory$ + "/" + entry.getName());
            outputDir = outputFile.getParentFile();
            if (!outputDir.exists())
                outputDir.mkdirs();
            if (entry.isDirectory()) {
                outputFile.mkdir();

            } else {
                outputStream = new FileOutputStream(outputFile);
                IOUtils.copy(tis, outputStream);
                outputStream.close();
            }
        }
        tis.close();
    } catch (Exception e) {
        Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString());
    }
}

From source file:hudson.gridmaven.gridlayer.HadoopInstance.java

/**
 * This method decompress filesystem structure from HDFS archive
 *//*from w  w w . ja va2  s. c  o m*/
public void getAndUntar(String src, String targetPath) throws FileNotFoundException, IOException {
    BufferedOutputStream dest = null;
    InputStream tarArchiveStream = new FSDataInputStream(fs.open(new Path(src)));
    TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream));
    TarArchiveEntry entry = null;
    try {
        while ((entry = tis.getNextTarEntry()) != null) {
            int count;
            File outputFile = new File(targetPath, entry.getName());

            if (entry.isDirectory()) { // entry is a directory
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else { // entry is a file
                byte[] data = new byte[BUFFER_MAX];
                FileOutputStream fos = new FileOutputStream(outputFile);
                dest = new BufferedOutputStream(fos, BUFFER_MAX);
                while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dest != null) {
            dest.flush();
            dest.close();
        }
        tis.close();
    }
}

From source file:at.ac.tuwien.big.testsuite.impl.task.UnzipTaskImpl.java

private void untar(File tarFile, File baseDirectory, Collection<Exception> exceptions, boolean gzipped) {
    try (FileInputStream fis = new FileInputStream(tarFile);
            TarArchiveInputStream tis = new TarArchiveInputStream(
                    gzipped ? new GzipCompressorInputStream(fis) : fis)) {
        TarArchiveEntry entry;

        while ((entry = tis.getNextTarEntry()) != null) {
            String escapedEntryName = escapeFileName(entry.getName());
            File entryFile = new File(baseDirectory, escapedEntryName);

            if (entry.isDirectory()) {
                if (!contains(entryFile.getAbsolutePath(), ignoredDirectories)) {
                    entryFile.mkdir();/*  w ww. ja v a2 s.  com*/
                }
            } else if (!endsWith(entryFile.getName(), ignoredExtensions)
                    && !contains(entryFile.getAbsolutePath(), ignoredDirectories)) {
                if (entryFile.getAbsolutePath().indexOf(File.separatorChar,
                        baseDirectory.getAbsolutePath().length()) > -1) {
                    // Only create dirs if file is not in the root dir
                    entryFile.getParentFile().mkdirs();
                }

                try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(entryFile))) {
                    copyInputStream(tis, outputStream);
                }
            }

            addTotalWork(1);
            addDoneWork(1);
        }
    } catch (Exception ex) {
        exceptions.add(ex);
    }
}

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

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

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

    TarArchiveInputStream tIn = new TarArchiveInputStream(in);

    HashMap<String, Long> modDateMap = new HashMap<String, Long>();
    TarArchiveEntry entry;
    do {/*from   w ww.j  ava2s .  c o m*/
        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();
    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.puppetlabs.geppetto.forge.util.TarUtils.java

/**
 * Unpack the content read from <i>source</i> into <i>targetFolder</i>. If the
 * <i>skipTopFolder</i> is set, then don't assume that the archive contains one
 * single folder and unpack the content of that folder, not including the folder
 * itself./*from  w w  w  .  jav a2 s.c  o  m*/
 * 
 * @param source
 *            The input source. Must be in <i>TAR</i> format.
 * @param targetFolder
 *            The destination folder for the unpack. Not used when a <tt>fileCatcher</tt> is provided
 * @param skipTopFolder
 *            Set to <code>true</code> to unpack beneath the top folder
 *            of the archive. The archive must consist of one single folder and nothing else
 *            in order for this to work.
 * @param fileCatcher
 *            Used when specific files should be picked from the archive without writing them to disk. Can be
 *            <tt>null</tt>.
 * @throws IOException
 */
public static void unpack(InputStream source, File targetFolder, boolean skipTopFolder, FileCatcher fileCatcher)
        throws IOException {
    String topFolderName = null;
    Map<File, Map<Integer, List<String>>> chmodMap = new HashMap<File, Map<Integer, List<String>>>();
    TarArchiveInputStream in = new TarArchiveInputStream(source);
    try {
        TarArchiveEntry te = in.getNextTarEntry();
        if (te == null) {
            throw new IOException("No entry in the tar file");
        }
        do {
            if (te.isGlobalPaxHeader())
                continue;

            String name = te.getName();
            if (skipTopFolder) {
                int firstSlash = name.indexOf('/');
                if (firstSlash < 0)
                    throw new IOException("Archive doesn't contain one single folder");

                String tfName = name.substring(0, firstSlash);
                if (topFolderName == null)
                    topFolderName = tfName;
                else if (!tfName.equals(topFolderName))
                    throw new IOException("Archive doesn't contain one single folder");
                name = name.substring(firstSlash + 1);
            }
            if (name.length() == 0)
                continue;

            String linkName = te.getLinkName();
            if (linkName != null) {
                if (linkName.trim().equals(""))
                    linkName = null;
            }

            if (fileCatcher != null) {
                if (linkName == null && !te.isDirectory() && fileCatcher.accept(name)) {
                    if (fileCatcher.catchData(name, in))
                        // We're done here
                        return;
                }
                continue;
            }

            File outFile = new File(targetFolder, name);
            if (linkName != null) {
                if (!OsUtil.link(targetFolder, name, te.getLinkName()))
                    throw new IOException("Archive contains links but they are not supported on this platform");
            } else {
                if (te.isDirectory()) {
                    outFile.mkdirs();
                } else {
                    outFile.getParentFile().mkdirs();
                    OutputStream target = new FileOutputStream(outFile);
                    StreamUtil.copy(in, target);
                    target.close();
                    outFile.setLastModified(te.getModTime().getTime());
                }
                registerChmodFile(chmodMap, targetFolder, Integer.valueOf(te.getMode()), name);
            }
        } while ((te = in.getNextTarEntry()) != null);
    } finally {
        StreamUtil.close(in);
    }
    chmod(chmodMap);
}

From source file:algorithm.TarPackaging.java

@Override
protected List<RestoredFile> restore(File tarFile) throws IOException {
    List<RestoredFile> extractedFiles = new ArrayList<RestoredFile>();
    FileInputStream inputStream = new FileInputStream(tarFile);
    if (GzipUtils.isCompressedFilename(tarFile.getName())) {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(inputStream)));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }//w w w  .ja  va2  s  . c o  m
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    } else if (BZip2Utils.isCompressedFilename(tarFile.getName())) {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new BufferedInputStream(inputStream)));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    } else {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new BufferedInputStream(inputStream));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    }
    for (RestoredFile file : extractedFiles) {
        file.algorithm = this;
        file.checksumValid = true;
        file.restorationNote = "The algorithm doesn't alter these files, so they are brought to its original state. No checksum validation executed.";
        // All files in a .tar file are payload files:
        file.wasPayload = true;
        for (RestoredFile relatedFile : extractedFiles) {
            if (file != relatedFile) {
                file.relatedFiles.add(relatedFile);
            }
        }
    }
    return extractedFiles;
}

From source file:com.datos.vfs.provider.tar.TarFileSystem.java

@Override
public void init() throws FileSystemException {
    super.init();

    // Build the index
    try {// w  ww  . j  a  va 2  s . co  m
        final List<TarFileObject> strongRef = new ArrayList<>(DEFAULT_INDEX_SIZE);
        TarArchiveEntry entry;
        while ((entry = getTarFile().getNextTarEntry()) != null) {
            final AbstractFileName name = (AbstractFileName) getFileSystemManager().resolveName(getRootName(),
                    UriParser.encode(entry.getName()));

            // Create the file
            TarFileObject fileObj;
            if (entry.isDirectory() && getFileFromCache(name) != null) {
                fileObj = (TarFileObject) getFileFromCache(name);
                fileObj.setTarEntry(entry);
                continue;
            }

            fileObj = createTarFileObject(name, entry);
            putFileToCache(fileObj);
            strongRef.add(fileObj);
            fileObj.holdObject(strongRef);

            // Make sure all ancestors exist
            // TODO - create these on demand
            TarFileObject parent = null;
            for (AbstractFileName parentName = (AbstractFileName) name
                    .getParent(); parentName != null; fileObj = parent, parentName = (AbstractFileName) parentName
                            .getParent()) {
                // Locate the parent
                parent = (TarFileObject) getFileFromCache(parentName);
                if (parent == null) {
                    parent = createTarFileObject(parentName, null);
                    putFileToCache(parent);
                    strongRef.add(parent);
                    parent.holdObject(strongRef);
                }

                // Attach child to parent
                parent.attachChild(fileObj.getName());
            }
        }
    } catch (final IOException e) {
        throw new FileSystemException(e);
    } finally {
        closeCommunicationLink();
    }
}