com.sap.cloud.security.ams.dcl.archivetools.ArchiveVisitor 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 static java.nio.file.FileVisitResult.CONTINUE;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.Predicate;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
public class ArchiveVisitor extends SimpleFileVisitor {
private final Path source;
private final ArchiveOutputStream out;
private byte[] buffer;
private final ArchiveEntryCreator entryGenerator;
private final Predicate pred;
private byte[] getBuffer() {
if (buffer == null) {
buffer = new byte[8 * 1024];
}
return buffer;
}
public ArchiveVisitor(Path source, ArchiveOutputStream out, ArchiveEntryCreator entryGenerator,
Predicate pred) {
this.source = source;
this.out = out;
this.entryGenerator = entryGenerator;
this.pred = pred;
}
public ArchiveVisitor(Path source, ArchiveOutputStream out, ArchiveEntryCreator entryGenerator) {
this(source, out, entryGenerator, null);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (pred == null || pred.test(file)) {
String name = source.relativize(file).toString();
entryGenerator.add(out, name, file.toFile(), getBuffer());
}
return CONTINUE;
}
public Path getSource() {
return source;
}
}