Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating zip file in memory out of byte[]. Zip file is allways corrupted

Tags:

java

zip

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.

This is my code:

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.

like image 999
F4k3d Avatar asked Sep 22 '14 12:09

F4k3d


People also ask

Why is my zip file corrupted?

ZIP files can get corrupted during the download process. If the download was interrupted, due to a power outage or an unexpected program closure even for a moment, unreadable data can end up becoming part of the downloaded ZIP file and make it difficult for the data to be extracted.

How do you check if a zip file is corrupted in Java?

Your code is basically OK, try to find out which file is responsible for the corrupted zip file. Check whether digitalFile. getFile() always returns a valid and accessible argument to FileInputStream. Just add a bit logging to your code and you will find out what's wrong.

How do I know if a zip file is corrupted Linux?

Ideally the best way to check if a zip is corrupted is to do a CRC check but this can take a long time especially if there is a lot of large zip files. I would be happy just to be able to do a quick file size or header check.


1 Answers

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
like image 158
Ian Roberts Avatar answered Feb 10 '23 02:02

Ian Roberts