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

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

Go to download

A Java library to convert .pdf files into .txt, .jpg, .png, .epub and .zip formats.

There is a newer version: 1.0.2
Show newest version
package pdf.converter.zip;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import pdf.converter.img.ImageFileExtension;
import pdf.converter.img.ImgCreator;

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);
                        }
                    });
        }
    }

    public static void main(String[] args) {
        new ImgCreator().process(new File("/home/joze/Downloads/ASEFlogo_popravljen.pdf"), new File("/home/joze/Downloads"), 300, 800, 800, ImageFileExtension.PNG);
    }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy