如何为"虚拟文件"列表创建ZIP文件并输出到httpservletresponse

Cor*_*rey 6 java zip spring servlets spring-mvc

我的目标是将多个java.io.File对象放入zip文件并打印到HttpServletResponse以供用户下载.

这些文件是由JAXB marshaller创建的.它是一个java.io.File对象,但它实际上不在文件系统上(它只在内存中),因此我无法创建FileInputStream.

我见过的所有资源都使用OutputStream来打印zip文件内容.但是,所有这些资源都使用FileInputStream(我无法使用).

谁知道我怎么能做到这一点?

Ral*_*lph 6

看看Apache Commons Compress库,它提供了您需要的功能.

当然,"erickson"对你的问题发表评论是正确的.您将需要文件内容而不是java.io.File对象.在我的示例中,我假设您有一个方法 byte[] getTheContentFormSomewhere(int fileNummer),它返回fileNummer-th文件的文件内容(在内存中). - 当然这个功能设计很差,但它仅用于说明.

它应该有点像这样:

void compress(final OutputStream out) {
  ZipOutputStream zipOutputStream = new ZipOutputStream(out);
  zipOutputStream.setLevel(ZipOutputStream.STORED);

  for(int i = 0; i < 10; i++) {
     //of course you need the file content of the i-th file
     byte[] oneFileContent = getTheContentFormSomewhere(i);
     addOneFileToZipArchive(zipOutputStream, "file"+i+"."txt", oneFileContent);
  }

  zipOutputStream.close();
}

void addOneFileToZipArchive(final ZipOutputStream zipStream,
          String fileName,
          byte[] content) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
    zipStream.putNextEntry(zipEntry);
    zipStream.write(pdfBytes);
    zipStream.closeEntry();
}
Run Code Online (Sandbox Code Playgroud)

你的http控制器的Snipets:

HttpServletResponse response
...
  response.setContentType("application/zip");
  response.addHeader("Content-Disposition", "attachment; filename=\"compress.zip\"");
  response.addHeader("Content-Transfer-Encoding", "binary");
  ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
  compress(outputBuffer);
  response.getOutputStream().write(outputBuffer.toByteArray());
  response.getOutputStream().flush();
  outputBuffer.close();
Run Code Online (Sandbox Code Playgroud)


Cor*_*rey 3

事实证明我是个白痴:)正在“创建”的文件保存到无效路径并吞下异常,所以我认为它正在“创建”正常。然而,当我尝试实例化一个新的 FileInputStream 时,它抱怨文件不存在(确实如此)。我脑子一热,假设 java.io.File 对象实际上在某个地方包含文件信息。但正如埃里克森指出的那样,这是错误的。

感谢拉尔夫提供的代码,我在解决无效路径问题后使用了它。

我的代码:

ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
byte[] buf = new byte[1024];

File file;
InputStream in;
// Loop through entities
for (TitleProductAccountApproval tpAccountApproval : tpAccountApprovals) {
    // Generate the file    
    file = xmlManager.getXML(
        tpAccountApproval.getTitleProduct().getTitleProductId(), 
        tpAccountApproval.getAccount().getAccountId(), 
        username);

    // Write to zip file
    in = new FileInputStream(file);
    out.putNextEntry(new ZipEntry(file.getName()));

    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    out.closeEntry();
    in.close();
}

out.close();
Run Code Online (Sandbox Code Playgroud)