【问题标题】:Extract the zip and convert to base 64 of each file提取 zip 并转换为每个文件的 base 64
【发布时间】:2020-08-19 00:13:36
【问题描述】:

我正在从系统中读取附件列表,女巫将 base 64 编码字符串中的附加文档作为 zip 返回,我的目标是获取每个附加文档的 base 64 编码字符串。 注意:- 我正在尝试下面的代码,我正在解压缩 zip 并在我的本地文件系统中写入。 .但实际上我想获得每个文件的 base 64 格式,而不是在本地文件系统中写入文件。

public class UnzipUtility {
      private static final int BUFFER_SIZE = 4096;

         private static void extractFile(ZipInputStream zipIn, ZipEntry entry) throws IOException {


            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/Project/"+File.separator+entry.getName()));
            byte[] bytesIn = new byte[BUFFER_SIZE];
            System.out.println("File Name  "+entry.getName());
            int read = 0;
            while ((read = zipIn.read(bytesIn)) != -1) {
                //Hear I dont not want to write the output stream insted I want to get the base64 data for each file.
              bos.write(bytesIn);
            }
            bos.close();
        }
     public static void main(String[] args) throws IOException {
     String attachmentVariable="zip base 64 data"
          byte[] bytedata = attachmentVariable.getBytes("UTF-8");
         byte[] valueDecoded = Base64.decodeBase64(bytedata);
         ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(valueDecoded));
         ZipEntry entry = zipIn.getNextEntry();

            // iterates over entries in the zip file
             while (entry != null) {                    extractFile(zipIn,entry);
                    zipIn.closeEntry();
                    entry = zipIn.getNextEntry();


          }       

        }
}

【问题讨论】:

    标签: java zipinputstream


    【解决方案1】:

    因此,您有一个带有 zip 文件的 Base64 编码字符串,并且您需要一个 Map<String, String>,其中键是 zip 条目名称,值是 Base64 编码内容。

    在 Java 9+ 中,这很容易做到:

    String base64ZipFile = "zip base 64 data";
    Map<String, String> base64Entries = new LinkedHashMap<>();
    try (ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(base64ZipFile)))) {
        Encoder encoder = Base64.getEncoder();
        for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) {
            base64Entries.put(entry.getName(), encoder.encodeToString(zipIn.readAllBytes()));
        }
    }
    

    【讨论】:

    • 谢谢安德烈亚斯,这就是我一直在寻找的 :-),有没有可能在 java 8 中实现这一点?由于我们的客户没有 java 8+ 环境。感谢您的帮助和快速响应。
    • @UpendraSingh 是的,你只需要做你自己的循环来做readAllBytes() 所做的事情。或者使用有方法的第三方库。
    • 谢谢安德烈亚斯。
    【解决方案2】:

    要在写入OutputStream 时对数据进行Base64 编码,请使用Encoder.wrap(OutputStream os) 方法。

    默认情况下,BufferedOutputStream 将使用 8192 字节的缓冲区,因此如果将 BUFFER_SIZE 增加到 8192,则不需要 BufferedOutputStream

    您应该使用 try-with-resources 和更新的 NIO.2 API。

    这意味着您的代码应该是:

    private static final int BUFFER_SIZE = 8192;
    
    private static void extractFile(ZipInputStream zipIn, ZipEntry entry) throws IOException {
        try ( OutputStream fos = Files.newOutputStream(Paths.get("D:/Project", entry.getName()));
              OutputStream b64os = Base64.getEncoder().wrap(fos); ) {
            System.out.println("File Name  " + entry.getName());
            byte[] buf = new byte[BUFFER_SIZE];
            for (int len = 0; (len = zipIn.read(buf)) != -1; ) {
                b64os.write(buf, 0, len);
            }
        }
    }
    

    【讨论】:

    • 感谢您的快速回复 Andreas,但我不确定我是否正确解释了我的要求。正如我上面提到的,我正在读取 base 64 zip 数据,最后我试图获取输出,比如 map (key=name of file value=base 64 string),我不想在任何地方写入文件。谢谢你的帮助。
    • 从您的代码中,我正在尝试类似下面的内容。 for (int len = 0; (len = zipIn.read(buf)) != -1; ) { b64os.write(buf, 0, len); } byte[] 编码 = java.util.Base64.getEncoder().encode(buf); System.out.println(new String(encoded));
    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 2011-12-27
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2014-02-26
    相关资源
    最近更新 更多