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!

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

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

@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.

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.

Hi
I have a single url which contains 10 txt files.
How can i download all files together and save them in one go.

Above code would download all the files in a zip to your local system. Once you have the business logic in place to get the file names with path, you can use a MultiValueMap to store fileNames.

commented: how to add files name help mi +0
commented: hello, can you please tell what is "_response" in this code? +0

i wana download files from server by geting name of files in table i zip ....how to add files name by path (on server upload folder) please help mi

what is _response in this? Is this some library?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.