downloaded zip file returns zero has 0 bytes as size

Posted by Yaw Reuben on Stack Overflow See other posts from Stack Overflow or by Yaw Reuben
Published on 2010-03-17T03:50:26Z Indexed on 2010/03/17 3:51 UTC
Read the original article Hit count: 316

I have written a Java web application that allows a user to download files from a server. These files are quite large and so are zipped together before download.

It works like this: 1. The user gets a list of files that match his/her criteria 2. If the user likes a file and wants to download he/she selects it by checking a checkbox 3. The user then clicks "download" 4. The files are then zipped and stored on a server 5. The user this then presented with a page which contains a link to the downloadable zip file 6. However on downloading the zip file the file that is downloaded is 0 bytes in size

I have checked the remote server and the zip file is being created properly, all that is left is to serve the file the user somehow, can you see where I might be going wrong, or suggest a better way to serve the zip file.

The code that creates the link is:

<%   
String zipFileURL = (String) request.getAttribute("zipFileURL"); %>  
<p><a href="<% out.print(zipFileURL); %> ">Zip File Link</a></p>

The code that creates the zipFileURL variable is:

public static String zipFiles(ArrayList<String> fileList, String contextRootPath) {
        //time-stamping
          Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
    Iterator fileListIterator = fileList.iterator();
    String zipFileURL = "";

    try {
        String ZIP_LOC = contextRootPath + "WEB-INF" + SEP + "TempZipFiles" + SEP;
        BufferedInputStream origin = null;
        zipFileURL = ZIP_LOC
        + "FITS." + timeStamp.toString().replaceAll(":", ".").replaceAll(" ", ".") + ".zip";
        FileOutputStream dest = new FileOutputStream(ZIP_LOC
              + "FITS." + timeStamp.toString().replaceAll(":", ".").replaceAll(" ", ".") + ".zip");
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
              dest));

        // out.setMethod(ZipOutputStream.DEFLATED);
        byte data[] = new byte[BUFFER];

        while(fileListIterator.hasNext()) { 

           String fileName = (String) fileListIterator.next();
           System.out.println("Adding: " + fileName);
           FileInputStream fi = new FileInputStream(fileName);
           origin = new BufferedInputStream(fi, BUFFER);
           ZipEntry entry = new ZipEntry(fileName);
           out.putNextEntry(entry);
           int count;
           while ((count = origin.read(data, 0, BUFFER)) != -1) {
              out.write(data, 0, count);
           }
           origin.close();            
       }

      out.close();
   } catch (Exception e) {
      e.printStackTrace();
   }

return zipFileURL;
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about file-download