Zip文件总是被破坏了

Zip文件总是被破坏了

本文介绍了在byte []中创建内存中的zip文件。 Zip文件总是被破坏了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的创建的zip文件有问题。我正在使用Java 7.我尝试用字节数组创建一个zip文件,其中包含两个或更多Excel文件。应用程序完成,无任何例外。所以,我认为一切都很好。在我尝试打开zip文件后,Windows 7出现了一条错误消息,即zip文件可能已损坏。我无法打开它,我不知道为什么......!
我用Google搜索了这个问题,但我发现的代码片段看起来与我的实现完全相同。

I have a problem with my created zip file. I am using Java 7. I tried to create a zip file out of a byte array, which contains two or more Excel files. The application finishes allways without any exceptions. So, I thought everything is alright. After I tried to open the zip file, there was an error message from Windows 7, that the zip file is maybe corrupted. I couldn't open it and I have no idea why...!I googled for this problem but the code snippets I found, looks exactly the same than in my implementation.

这是我的代码:

if (repsList.size() > 1)
{
  String today = DateUtilities.convertDateToString(new Date(), "dd_MM_yyyy");
  String prefix = "recs_" + today;
  String suffix = ".zip";
  ByteArrayOutputStream baos = null;
  ZipOutputStream zos = null;
  try
  {
    baos = new ByteArrayOutputStream();
    zos = new ZipOutputStream(baos);

    for (RepBean rep : repsList)
    {
      String filename = rep.getFilename();
      ZipEntry entry = new ZipEntry(filename);
      entry.setSize(rep.getContent().length);
      zos.putNextEntry(entry);
      zos.write(rep.getContent());
      zos.closeEntry();
    }
    // this is the zip file as byte[]
    reportContent = baos.toByteArray();

  }
  catch (UnsupportedEncodingException e)
  {
    ...
  }
  catch (ZipException e) {
    ...
  }
  catch (IOException e)
  {
    ...
  }
  finally
  {
    try
    {
      if (zos != null)
      {
        zos.close();
      }

      if (baos != null)
      {
        baos.close();
      }
    }
    catch (IOException e)
    {
      // Nothing to do ...
      e.printStackTrace();
    }
  }
}
try
{
  response.setContentLength(reportContent.length);
  response.getOutputStream().write(reportContent);
}
catch (IOException e)
{
  ...
}
finally
{
  try
  {
    response.getOutputStream().flush();
    response.getOutputStream().close();
  }
  catch (IOException e)
  {
    ...
  }
}

这一定是一个非常简单的失败,但我找不到它。如果你可以帮我解决我的问题会很好。
非常感谢提前。

It must be a very simple failure but I cannot find it. Would be nice if you can help me with my problem.Thanks a lot in advance.

推荐答案

您正在转换 ByteArrayOutputStream ZipOutputStream 之前,$ c>到 byte [] 。您必须确保在 baos.toByteArray()之前关闭 zos ,这是确保尝试此操作的最简单方法-with-resources构造:

You are converting the ByteArrayOutputStream to a byte[] before you have closed the ZipOutputStream. You must ensure zos is closed before you do baos.toByteArray(), the easiest way to ensure this is a try-with-resources construct:

  try
  {
    try (baos = new ByteArrayOutputStream();
         zos = new ZipOutputStream(baos))
    {
      for (RepBean rep : repsList)
      {
        String filename = rep.getFilename();
        ZipEntry entry = new ZipEntry(filename);
        entry.setSize(rep.getContent().length);
        zos.putNextEntry(entry);
        zos.write(rep.getContent());
        zos.closeEntry();
      }
    }
    // this is the zip file as byte[]
    reportContent = baos.toByteArray();
  }
  // catch blocks as before, finally is no longer required as the try-with-resources
  // will ensure the streams are closed

这篇关于在byte []中创建内存中的zip文件。 Zip文件总是被破坏了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 07:36