【问题标题】:encode zip file to Base64 without write zip on filesystem将 zip 文件编码为 Base64,而不在文件系统上写入 zip
【发布时间】:2018-08-09 09:56:06
【问题描述】:

出于我的目的,我想压缩 zip 文件夹中的一些文件。然后必须对 zip 文件进行 Base64 编码。如果不在文件系统上写入 zip 文件,我该如何做到这一点?

private static String zipB64(List<File> files) throws FileNotFoundException, IOException {

    String encodedBase64 = null;
    String zipFile = C_ARCHIVE_ZIP;
    byte[] buffer = new byte[1024];

    try (FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos);) {

        for (File f : files) {

            FileInputStream fis = new FileInputStream(f);
            zos.putNextEntry(new ZipEntry(f.getName()));
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();
        }
    }
    File originalFile = new File(C_ARCHIVE_ZIP);
    byte[] bytes = new byte[(int)originalFile.length()];
    encodedBase64 = Base64.getEncoder().encodeToString(bytes);
    return encodedBase64;
}

【问题讨论】:

  • 到目前为止你尝试过什么?

标签: java base64 zip


【解决方案1】:

这是您可以使用ByteArrayOutputStream 将文件替换为内存缓冲区的方法。

注意此代码未经测试/未编译。把它当作一个“草图”而不是一个完成的产品来复制和粘贴。

private static String zipB64(List<File> files) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        for (File f : files) {
            try (FileInputStream fis = new FileInputStream(f)) {
                zos.putNextEntry(new ZipEntry(f.getName()));
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
            }
        }
    }
    byte[] bytes = baos.toByteArray();
    encodedBase64 = new String(Base64.getEncoder().encodeToString(bytes));
    return encodedBase64;
}

(您的原始版本存在错误。除了在文件系统中留下临时文件外,您的代码还泄露了您添加到 ZIP 中的文件的文件描述符。)

【讨论】:

  • 好的 :)),谢谢你的修改。老实说,我还没有测试这个功能。我现在就试试。
  • 致那些一直想修改这个答案的人,说代码已经过测试/工作/您可以复制和粘贴它等等。不要!这与作者的意图相冲突。 (我会知道它们是什么!)您的编辑暗示 >>I>I
【解决方案2】:

Stephen C 答案的测试和编译版本:

private static String zipB64(List<File> files) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        for (File f : files) {
            try (FileInputStream fis = new FileInputStream(f)) {
                zos.putNextEntry(new ZipEntry(f.getName()));
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
            }
        }
    }
    byte[] bytes = baos.toByteArray();
    return Base64.getEncoder().encodeToString(bytes);
}

你可以复制粘贴它。

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多