0

Here is the code that I'm using to create a zip file:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(baos);
try {
    for(int i=0; i<docId.length; i++){
        BoxDotComDocumentManager docman = new BoxDotComDocumentManager();
        Document d = docman.get(docId[i]);
        ZipEntry entry = new ZipEntry(d.getFileName());
        entry.setSize(d.getFileBytes().length);
        out.putNextEntry(entry);
        out.write(d.getFileBytes());
        resp.setContentType("application/zip");
        resp.setHeader("Content-Disposition", "attachment; filename="+ "zipdemo.zip"); 
        out.closeEntry();
 }
 } catch (Exception e) {
      System.out.println("E = " + e);
 }
 try {
       resp.getOutputStream().write(baos.toByteArray());
   resp.flushBuffer();
} catch (IOException e) {
   e.printStackTrace();
    }
 finally {
baos.close();
out.close();
 }

the zip file is being returned to the browser to be downloaded but when I attempt to download the file I'm receiving an error stating that the file cannot be downloaded because the zip file is invalid.

Document is an object which contains all of the information about a file including the actual file.

Any ideas as to what I'm doing wrong? I've tried a lot of permutations of this and none of them seem to work. Thank you in advance.

Keith

keith h
  • 83
  • 3
  • 10
  • For one thing: when you encounter a fatal error, *don't* just print the stack trace. Make the request fail. Also, don't catch bare Exceptino - it's almost always the wrong approach. Oh, and why are you setting the headers on each iteration of the loop? – Jon Skeet Mar 06 '12 at 23:00
  • Jon - Just trying to make a happy trail work and then I'll handle all of the exceptions properly, same thing with the headers; having them in the loop or outside doesn't change the error. They will be moved out of the loop. – keith h Mar 06 '12 at 23:06
  • 1
    But it's making it very easy for you to *miss* exceptions. IMO, it's worth getting the exception handling right *first* so that you can tell when you're really going through the happy path... – Jon Skeet Mar 06 '12 at 23:08
  • I appreciate your feedback; any idea why this isn't working? I know that the exception handling isn't correct but that wouldn't effect the proper creating and downloading of this file. – keith h Mar 06 '12 at 23:11

1 Answers1

2

Try using the out.write method with 3 arguments.

Replace: out.write(d.getFileBytes());

With: out.write(d.getFileBytes(),0,d.getFileBytes().length);


Note: According to the java doc the write method with one parameter doesn't get read.

Writes b.length bytes to this output stream.

The write method of FilterOutputStream calls its write method of three arguments with the arguments b, 0, and b.length.

Note that this method does not call the one-argument write method of its underlying stream with the single argument b.

Making this change in my own code fixed my problem.

James
  • 23
  • 6