首页 > 解决方案 > 设置仅流式传输的 zip 文件的文件名

问题描述

我目前正在创建一个 zip 文件并用各种 json 文件和图像填充它。所有这些都应该只在内存中运行,而不是在硬盘上。因此,到目前为止,我有以下构造:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zip = null;
String FILE_NAME = "file.zip";

try {
   zip = new ZipOutputStream(baos);
   //now the critical part where the name of the file should be set
   ZipEntry entry = new ZipEntry(FILE_NAME);
   zip.putNextEntry(entry);
   byte[] data = FILE_NAME.getBytes();
   zip.write(data, 0, data.length);
   zip.closeEntry();
   //end of critical part and filling the rest of the zip
   //...
   //
}finally{
   IOUtils.closeQuietly(zip);
   byte[] byteFile = baos.toByteArray();
   IOUtils.closeQuietly(baos);}

问题是 zip 文件称为 file.zip,但也包含 file.zip 本身。如何从 ZipOutputStream 中命名我的 Zip 文件,而不用打包到同名的这个文件中?不幸的是,我只在这里找到了这个解决方案。

标签: javastreamzipfilenames

解决方案


推荐阅读