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

com.sap.cloud.security.ams.dcl.archivetools.ArchiveVisitor 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 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy