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

com.neko233.toolchain.common.file.visitor.ZipTreeFileVisitor Maven / Gradle / Ivy

package com.neko233.toolchain.common.file.visitor;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Zip 树形结构
 */
public class ZipTreeFileVisitor extends SimpleFileVisitor {

    private static ZipOutputStream zos;

    private Path sourceDir;

    public ZipTreeFileVisitor(Path sourceDir) {
        this.sourceDir = sourceDir;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {

        try {
            Path targetFile = sourceDir.relativize(file);

            zos.putNextEntry(new ZipEntry(targetFile.toString()));

            byte[] bytes = Files.readAllBytes(file);
            zos.write(bytes, 0, bytes.length);
            zos.closeEntry();

        } catch (IOException ex) {
            System.err.println(ex);
        }

        return FileVisitResult.CONTINUE;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy