Java 解压缩zip文件并将其转换为每个文件的base 64

Java 解压缩zip文件并将其转换为每个文件的base 64,java,zipinputstream,Java,Zipinputstream,我正在从一个系统读取附件列表,该系统将以Base64编码字符串作为zip返回附加文档,我的目标是为每个附加文档获取Base64编码字符串。 注意:-我在下面的代码中尝试解压zip并在本地文件系统中编写。 . 但实际上,我希望为每个文件获取base64格式,而不必在本地文件系统中写入文件 public class UnzipUtility { private static final int BUFFER_SIZE = 4096; private static vo

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

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


          }       

        }
}

要在将数据写入
输出流时对其进行Base64编码,请使用以下方法

默认情况下,将使用8192字节的缓冲区,因此如果将
buffer\u SIZE
增加到8192,则不需要
bufferedoutstream

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

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

private static final int BUFFER_SIZE=8192;
私有静态void提取器文件(ZipInputStream zipIn、ZipEntry条目)引发IOException{
try(OutputStream fos=Files.newOutputStream(path.get(“D:/Project”,entry.getName()));
OutputStream b64os=Base64.getEncoder().wrap(fos);){
System.out.println(“文件名”+entry.getName());
字节[]buf=新字节[缓冲区大小];
对于(int len=0;(len=zipIn.read(buf))!=-1;){
b64os.write(buf,0,len);
}
}
}

要在将数据写入
输出流时对其进行Base64编码,请使用以下方法

默认情况下,将使用8192字节的缓冲区,因此如果将
buffer\u SIZE
增加到8192,则不需要
bufferedoutstream

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

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

private static final int BUFFER_SIZE=8192;
私有静态void提取器文件(ZipInputStream zipIn、ZipEntry条目)引发IOException{
try(OutputStream fos=Files.newOutputStream(path.get(“D:/Project”,entry.getName()));
OutputStream b64os=Base64.getEncoder().wrap(fos);){
System.out.println(“文件名”+entry.getName());
字节[]buf=新字节[缓冲区大小];
对于(int len=0;(len=zipIn.read(buf))!=-1;){
b64os.write(buf,0,len);
}
}
}

因此,您有一个带zip文件的Base64编码字符串,您需要一个
映射,其中key是zip条目名称,value是Base64编码的内容

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

String base64ZipFile=“zip base 64 data”;
Map base64Entries=新建LinkedHashMap();
try(ZipInputStream zipIn=new-ZipInputStream(new-ByteArrayInputStream(Base64.getDecoder().decode(base64ZipFile))){
编码器编码器=Base64.getEncoder();
for(ZipEntry entry;(entry=zipIn.getNextEntry())!=null;){
base64Entries.put(entry.getName(),encoder.encodeToString(zipIn.readAllBytes());
}
}

因此,您有一个带zip文件的Base64编码字符串,您需要一个
映射,其中key是zip条目名称,value是Base64编码的内容

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

String base64ZipFile=“zip base 64 data”;
Map base64Entries=新建LinkedHashMap();
try(ZipInputStream zipIn=new-ZipInputStream(new-ByteArrayInputStream(Base64.getDecoder().decode(base64ZipFile))){
编码器编码器=Base64.getEncoder();
for(ZipEntry entry;(entry=zipIn.getNextEntry())!=null;){
base64Entries.put(entry.getName(),encoder.encodeToString(zipIn.readAllBytes());
}
}

谢谢您的快速回复,Andreas,但我不确定我是否正确解释了我的要求。正如我上面提到的,我正在读取base64压缩数据,最后我试图获得输出,比如map(key=name of file value=base64 string),我不想在任何地方写入文件。谢谢你的帮助。从你的代码中,我正在尝试以下内容。对于(int len=0;(len=zipIn.read(buf))!=-1;{b64os.write(buf,0,len);}byte[]encoded=java.util.Base64.getEncoder().encode(buf);System.out.println(新字符串(编码));感谢您的快速回复,Andreas,但我不确定是否正确解释了我的要求。正如我上面提到的,我正在读取base64压缩数据,最后我试图获得输出,比如map(key=name of file value=base64 string),我不想在任何地方写入文件。谢谢你的帮助。从你的代码中,我正在尝试以下内容。对于(int len=0;(len=zipIn.read(buf))!=-1;{b64os.write(buf,0,len);}byte[]encoded=java.util.Base64.getEncoder().encode(buf);System.out.println(新字符串(编码));谢谢Andreas,这就是我想要的:-),有没有可能在Java8中实现这一点?因为我们的客户机没有Java8+环境。感谢您的帮助和快速响应。@UpendraSingh是的,您只需执行自己的循环即可完成
readAllBytes()
的功能。或者使用有方法的第三方库。谢谢Andreas。谢谢Andreas,这就是我想要的:-),有没有可能在Java8中实现这一点?因为我们的客户机没有Java8+环境。感谢您的帮助和快速响应。@UpendraSingh是的,您只需执行自己的循环即可完成
readAllBytes()
的功能。或者使用一个第三方库,它有一个方法来做这件事。谢谢Andreas。