将字节保存到pdf文件并压缩

时间:2018-08-19 12:24:48

标签: java pdf zip

我正在尝试将bytes []数据保存为pdf并将其压缩。每次尝试时,我都会得到一个空白的pdf。下面是代码。谁能指导我我在做什么错?

BigIntegers.kt

这就是我正在关注的

要获取实际的PDF文档,您必须解码Base64编码的字符串,将其保存为扩展名为“ .zip”的二进制文件,然后从ZIP文件中提取PDF文件。

1 个答案:

答案 0 :(得分:0)

您实际上不需要创建一个zip文件。这些说明告诉您base64数据表示一个压缩的PDF,因此您可以将其解压缩为代码并编写PDF文件本身:

Path pdfFile = Files.createTempFile(null, ".pdf");

try (ZipInputStream zip = new ZipInputStream(
        new ByteArrayInputStream(
            Base64.getDecoder().decode(contents)))) {

    ZipEntry entry;
    while ((entry = zip.getNextEntry()) != null) {
        String name = entry.getName();
        if (name.endsWith(".pdf") || name.endsWith(".PDF")) {
            Files.copy(zip, pdfFile, StandardCopyOption.REPLACE_EXISTING);
            break;
        }
    }
}
相关问题