0

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!

Edited by nikk8a: Syntax correction

5
Contributors
7
Replies
25
Views
4 Years
Discussion Span
Last Post by Manjeet_1
0

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

0

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

Edited by Taywin

0

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

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.

0

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

-1

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.

Edited by nikk8a

Votes + Comments
how to add files name help mi
-1

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.