【发布时间】: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