All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.security.ams.dcl.archivetools.ArchiveExtractor Maven / Gradle / Ivy

The newest version!
/************************************************************************
 * © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 ************************************************************************/
package com.sap.cloud.security.ams.dcl.archivetools;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.function.Predicate;

import org.apache.commons.compress.archivers.ArchiveEntry;

public class ArchiveExtractor extends ArchiveExtractorBase {
    public ArchiveExtractor(File baseFolder, Predicate filter) throws IOException {
        super(baseFolder, filter);
    }

    public ArchiveExtractor(File baseFolder) throws IOException {
        this(baseFolder, null);
    }

    private static void createDirectories(File file) throws IOException {
        if (!file.mkdirs() && !file.isDirectory()) {
            throw new IOException("Could not create directory '" + file.getName() + "'.");
        }
    }

    @Override
    protected void doExtractEntry(ArchiveEntry entry, InputStream is, File file) throws IOException {
        if (entry.isDirectory()) {
            createDirectories(file);
        } else {
            createDirectories(file.getParentFile());
            try (OutputStream out = Files.newOutputStream(file.toPath())) {
                copyStream(is, out);
            }
        }

        long time = entry.getLastModifiedDate().getTime();
        if (time >= 0) {
            file.setLastModified(time); // NOSONAR NOPMD
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy