com.sap.cloud.security.ams.dcl.archivetools.ArchiveExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of archive-tools Show documentation
Show all versions of archive-tools Show documentation
Tools for handling AMS bundles.
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
}
}
}