Avatar of alskdj80
alskdj80

asked on 

Zipping files up in Servlet

I have a problem with a servlet that I'm using to zip up alot of files into one file... but I don't think I have the path correct... I'm not sure what to do since I've tried so many combinations, hoping to find the right path...

here's my code:      filesToZip[] is an array containing filenames for the files to be zipped... and zipFileName is the name of the zip file (ie. asdf.zip)

try {
     ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName));
                                    
     // Set the compression ratio
     zos.setLevel(Deflater.DEFAULT_COMPRESSION);
                                    
    // iterate through the array of files, adding each to the zip file
     for (int i = 0; i < filesToZip.length; i++) {
            location = "/protected/files/" + filesToZip[i];
                                                                        
            // Associate a file input stream for the current file
            FileInputStream in = new FileInputStream(location); //***************
      
            // Add ZIP entry to output stream.
            zos.putNextEntry(new ZipEntry(location));
      
            int len;
                                    
            while ((len = in.read(buffer)) > 0) {
                  zos.write(buffer, 0, len);
            }
      
      
            // Close the current entry
            zos.closeEntry();
      
            // Close the current file input stream
            in.close();
      }
                              
      // Close the ZipOutPutStream
      zos.close();

} catch(Exception e) {
     System.out.println("Exception: " + e.getMessage());
}


My files are located /user/jakarta-tomcat/webapps/ROOT/protected/files/

Currently, at the place where i have labelled with alot of asterisks (//***************), an exception is thrown... so i have a feeling that they cannot find these files

Can anyone offer any suggestions?  thanks!

i usually like to offer more points, but today i only have 90...=\
JSP

Avatar of undefined
Last Comment
TimYates

8/22/2022 - Mon