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

pdf.converter.zip.ZipCreator Maven / Gradle / Ivy

package pdf.converter.zip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCreator {
    private static final Logger log = LoggerFactory.getLogger(ZipCreator.class);

    public void create(File imgsDir, File output) throws IOException {
        pack(imgsDir.getAbsolutePath(), output.getAbsolutePath());
    }

    private static void pack(String sourceDirPath, String zipFilePath) throws IOException {
        Path p = Files.createFile(Paths.get(zipFilePath));
        try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(p))) {
            Path pp = Paths.get(sourceDirPath);
            Files.walk(pp)
                    .filter(path -> !Files.isDirectory(path))
                    .forEach(path -> {
                        ZipEntry zipEntry = new ZipEntry(pp.relativize(path).toString());
                        try {
                            zs.putNextEntry(zipEntry);
                            zs.write(Files.readAllBytes(path));
                            zs.closeEntry();
                        } catch (Exception e) {
                            log.warn("Failed to create zip file", e);
                        }
                    });
        }
    }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy