多个文件存档的 ZIP 文件 servlet 响应已损坏

发布于 2024-09-25 00:12:00 字数 1717 浏览 5 评论 0原文

我正在尝试压缩一包文件并通过 servlet 返回 ZIP。如果我只有一个文件,则效果很好,但当我尝试添加更多文件时,它们似乎只是被附加到第一个 ZipEntry 中,因此 zip 存档会损坏。

private void writeZipResponse(HttpServletResponse response,
  List<File> bundle) {
    try {
       ByteArrayOutputStream bout=new ByteArrayOutputStream(); 
       ZipOutputStream zout = new ZipOutputStream(bout);
       ServletOutputStream out =response.getOutputStream();

        for (File file : bundle) {
            FileInputStream fi = new FileInputStream(file);
            byte bytes[] = new byte[(int)file.length()];

          // Read in the bytes
          int offset = 0; 
          int numRead = 0;
          while (offset < bytes.length && (numRead=fi.read(bytes, offset, bytes.length-offset)) >= 0) 
{ offset += numRead; } 

            // Ensure all the bytes have been read in 
          if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } 
          fi.close();

          ZipEntry zipEntry = new ZipEntry(file.getName());
          //zipEntry.setCompressedSize(file.length());
          zipEntry.setSize(offset);
          CRC32 crc = new CRC32();
          crc.update(bytes);
          zipEntry.setCrc(crc.getValue());
          zout.putNextEntry(zipEntry);
          zout.write(bytes, 0, offset);
          zout.closeEntry();
          zout.finish(); 
          fi.close();
      }

  zout.close();

  response.setContentType("application/zip");
  response.setHeader("Content-Disposition",
  "attachment; filename=hivseqdb.zip;");
  out.write(bout.toByteArray());
  out.flush();
  out.close();

  } catch (IOException e) {
   // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }

I'm trying ZIP a bundle of File's and return ZIP through a servlet. This works fine if I just have one file but when I try to add more it seems they are just being appended to the first ZipEntry and thus the zip archive gets corrupted.

private void writeZipResponse(HttpServletResponse response,
  List<File> bundle) {
    try {
       ByteArrayOutputStream bout=new ByteArrayOutputStream(); 
       ZipOutputStream zout = new ZipOutputStream(bout);
       ServletOutputStream out =response.getOutputStream();

        for (File file : bundle) {
            FileInputStream fi = new FileInputStream(file);
            byte bytes[] = new byte[(int)file.length()];

          // Read in the bytes
          int offset = 0; 
          int numRead = 0;
          while (offset < bytes.length && (numRead=fi.read(bytes, offset, bytes.length-offset)) >= 0) 
{ offset += numRead; } 

            // Ensure all the bytes have been read in 
          if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } 
          fi.close();

          ZipEntry zipEntry = new ZipEntry(file.getName());
          //zipEntry.setCompressedSize(file.length());
          zipEntry.setSize(offset);
          CRC32 crc = new CRC32();
          crc.update(bytes);
          zipEntry.setCrc(crc.getValue());
          zout.putNextEntry(zipEntry);
          zout.write(bytes, 0, offset);
          zout.closeEntry();
          zout.finish(); 
          fi.close();
      }

  zout.close();

  response.setContentType("application/zip");
  response.setHeader("Content-Disposition",
  "attachment; filename=hivseqdb.zip;");
  out.write(bout.toByteArray());
  out.flush();
  out.close();

  } catch (IOException e) {
   // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2024-10-02 00:12:00

您需要移动 zout.finish();循环外的线。

You need to move the zout.finish(); line outside of the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文