【发布时间】: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;
}
【问题讨论】:
-
到目前为止你尝试过什么?