【发布时间】:2020-11-20 04:07:33
【问题描述】:
我正在尝试用 Java 压缩多个文件以在我的 jar 中使用。 2 个文件是图像,1 个是 HTML 临时文件。压缩这些文件后,当我尝试查看 zip 文件的内容时,所有 3 个文件都变空了。由于文件在 zip 中,但由于某种原因它们是空的,因此不会引发错误。我需要将我的 zip 文件保存在内存中。
这是我的邮政编码。
public static File zipPdf(File data, File cover) throws IOException {
ArrayList<ByteArrayOutputStream> zips = new ArrayList<>();
ClassLoader loader = RunningPDF.class.getClassLoader();
File image = new File(Objects.requireNonNull(loader.getResource("chekklogo.png")).getFile());
File man = new File(Objects.requireNonNull(loader.getResource("manlogo.jpg")).getFile());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ZipOutputStream zos = new ZipOutputStream(baos)) {
ZipEntry entry = new ZipEntry(data.getName());
zos.putNextEntry(entry);
ZipEntry entry2 = new ZipEntry(image.getName());
zos.putNextEntry(entry2);
ZipEntry entry3 = new ZipEntry(man.getName());
zos.putNextEntry(entry3);
} catch(IOException ioe) {
ioe.printStackTrace();
}
【问题讨论】: