We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,007 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Download multiple files in single zip and render for download

Hello friends,

I am stuck at a point in downloading multiple files as a single zip such that - the generated zip by my code for user download seems to be not a properly formed one. While trying to open the generated zip, I get an error as - "Cannot open file: it does not appear to be a valid file"
I'd be glad if someone could review the code below and help me understand what might be missing here.

Thank you!

//Create list for file URLs - these are files from all different locations        
        List<String> filenames = new ArrayList<String>();
        //..code to add URLs to the list
        byte[] buf = new byte[2048];

        // Create the ZIP file
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream out = new ZipOutputStream(baos);

        // Compress the files
        for (int i=0; i<filenames.size(); i++) {

          FileInputStream fis = new FileInputStream(filenames.get(i).toString());
          BufferedInputStream bis = new BufferedInputStream(fis);

          // Add ZIP entry to output stream.
          File file = new File(filenames.get(i).toString());
          String entryname = file.getName();
          out.putNextEntry(new ZipEntry(entryname));

          int bytesRead;
          while ((bytesRead = bis.read(buf)) != -1) {
              out.write(buf, 0, bytesRead);
          }
          bis.close();
          fis.close();

        }


        ServletOutputStream sos = _response.getOutputStream();
        _response.setContentType("application/zip");
        _response.setHeader("Content-Disposition", "attachment; filename=\"MyZip.ZIP\"");

        sos.write(baos.toByteArray());

        out.flush();
        out.close();

        sos.flush();

Thanks in advance!

3
Contributors
4
Replies
5 Days
Discussion Span
8 Months Ago
Last Updated
5
Views
nikk8a
Newbie Poster
4 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Can you make a complete program of the posted code that will: compile, execute and show the problem?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Try to add before line 25 but outside the while loop out.closeEntry(); and see what happen?

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

@Taywin,
I added the line as you suggested, it didn't result in any change in the output. Still getting the same error on trying to open the downloaded zip.

nikk8a
Newbie Poster
4 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi All,

Finally after a lot of head banging I got the code working. And it turned out that something very minor (silly mistake on my part) was missing - as anticipated.
So, here's the working code:

    //Create list for file URLs - these are files from all different locations
    List<String> filenames = new ArrayList<String>();

    //..code to add URLs to the list
    byte[] buf = new byte[2048];

    // Create the ZIP file
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream out = new ZipOutputStream(baos);

    // Compress the files
    for (int i=0; i<filenames.size(); i++) {

    FileInputStream fis = new FileInputStream(filenames.get(i).toString());
    BufferedInputStream bis = new BufferedInputStream(fis);

    // Add ZIP entry to output stream.
    File file = new File(filenames.get(i).toString());
    String entryname = file.getName();
    out.putNextEntry(new ZipEntry(entryname));

    int bytesRead;
    while ((bytesRead = bis.read(buf)) != -1) {
    out.write(buf, 0, bytesRead);
    }

    out.closeEntry();
    bis.close();
    fis.close();
    }

    out.flush();
    baos.flush();
    out.close();
    baos.close();

    ServletOutputStream sos = _response.getOutputStream();
    _response.setContentType("application/zip");
    _response.setHeader("Content-Disposition", "attachment; filename=\"MyZip.ZIP\"");
    sos.write(baos.toByteArray());
    out.flush();
    out.close();
    sos.flush();

Line 27 and 32-35 has the added lines.

Thank you for the response.

nikk8a
Newbie Poster
4 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0751 seconds using 2.81MB