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

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

Introduction

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

Prototype

public Date getLastModifiedDate() 

Source Link

Usage

From source file:com.amaze.filemanager.asynchronous.asynctasks.compress.TarHelperTask.java

@Override
void addElements(ArrayList<CompressedObjectParcelable> elements) {
    TarArchiveInputStream tarInputStream = null;
    try {/*from ww w  . ja  v a2 s.  co  m*/
        tarInputStream = new TarArchiveInputStream(new FileInputStream(filePath));

        TarArchiveEntry entry;
        while ((entry = tarInputStream.getNextTarEntry()) != null) {
            String name = entry.getName();
            if (!CompressedHelper.isEntryPathValid(name)) {
                continue;
            }
            if (name.endsWith(SEPARATOR))
                name = name.substring(0, name.length() - 1);

            boolean isInBaseDir = relativePath.equals("") && !name.contains(SEPARATOR);
            boolean isInRelativeDir = name.contains(SEPARATOR)
                    && name.substring(0, name.lastIndexOf(SEPARATOR)).equals(relativePath);

            if (isInBaseDir || isInRelativeDir) {
                elements.add(new CompressedObjectParcelable(entry.getName(),
                        entry.getLastModifiedDate().getTime(), entry.getSize(), entry.isDirectory()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.amaze.filemanager.asynchronous.asynctasks.compress.GzipHelperTask.java

@Override
void addElements(ArrayList<CompressedObjectParcelable> elements) {
    TarArchiveInputStream tarInputStream = null;
    try {//from   w w w  . j a v  a2s .  c  o m
        tarInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(new FileInputStream(filePath)));

        TarArchiveEntry entry;
        while ((entry = tarInputStream.getNextTarEntry()) != null) {
            String name = entry.getName();
            if (!CompressedHelper.isEntryPathValid(name)) {
                AppConfig.toast(context.get(),
                        context.get().getString(R.string.multiple_invalid_archive_entries));
                continue;
            }
            if (name.endsWith(SEPARATOR))
                name = name.substring(0, name.length() - 1);

            boolean isInBaseDir = relativePath.equals("") && !name.contains(SEPARATOR);
            boolean isInRelativeDir = name.contains(SEPARATOR)
                    && name.substring(0, name.lastIndexOf(SEPARATOR)).equals(relativePath);

            if (isInBaseDir || isInRelativeDir) {
                elements.add(new CompressedObjectParcelable(entry.getName(),
                        entry.getLastModifiedDate().getTime(), entry.getSize(), entry.isDirectory()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.google.devtools.build.lib.bazel.repository.CompressedTarFunction.java

@Override
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
    Optional<String> prefix = descriptor.prefix();
    boolean foundPrefix = false;

    try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
        TarArchiveInputStream tarStream = new TarArchiveInputStream(decompressorStream);
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
            foundPrefix = foundPrefix || entryPath.foundPrefix();
            if (entryPath.skip()) {
                continue;
            }/*from  w ww.  ja  va  2 s. c o m*/

            Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment());
            FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory());
            if (entry.isDirectory()) {
                FileSystemUtils.createDirectoryAndParents(filename);
            } else {
                if (entry.isSymbolicLink() || entry.isLink()) {
                    PathFragment linkName = new PathFragment(entry.getLinkName());
                    boolean wasAbsolute = linkName.isAbsolute();
                    // Strip the prefix from the link path if set.
                    linkName = StripPrefixedPath.maybeDeprefix(linkName.getPathString(), prefix)
                            .getPathFragment();
                    if (wasAbsolute) {
                        // Recover the path to an absolute path as maybeDeprefix() relativize the path
                        // even if the prefix is not set
                        linkName = descriptor.repositoryPath().getRelative(linkName).asFragment();
                    }
                    if (entry.isSymbolicLink()) {
                        FileSystemUtils.ensureSymbolicLink(filename, linkName);
                    } else {
                        FileSystemUtils.createHardLink(filename,
                                descriptor.repositoryPath().getRelative(linkName));
                    }
                } else {
                    Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
                    filename.chmod(entry.getMode());

                    // This can only be done on real files, not links, or it will skip the reader to
                    // the next "real" file to try to find the mod time info.
                    Date lastModified = entry.getLastModifiedDate();
                    filename.setLastModifiedTime(lastModified.getTime());
                }
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }

    if (prefix.isPresent() && !foundPrefix) {
        throw new RepositoryFunctionException(
                new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"),
                Transience.PERSISTENT);
    }

    return descriptor.repositoryPath();
}

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

/**
 * Extracts files in a tar archive. Currently works only on uncompressed tar files.
 * /*from  ww w  .jav  a  2s. c om*/
 * @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);
            }
        });
    }
}