Solved

Zipping files up in Servlet

Posted on 2004-08-06
13
260 Views
Last Modified: 2008-02-20
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...=\
0
Comment
Question by:alskdj80
  • 8
  • 5
13 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 90 total points
ID: 11737438
Have you tried:

 location = System.getProperty( "tomcat.home" ) + "/webapps/ROOT/protected/files/" + filesToZip[i];
0
 
LVL 3

Author Comment

by:alskdj80
ID: 11737495
I just tried it but I get a null value for System.getProperty( "tomcat.home" )
0
 
LVL 3

Author Comment

by:alskdj80
ID: 11737503
oh, but i just tried System.getProperty( "catalina.home" ) and it worked... i'll let you know what happens
0
 
LVL 3

Author Comment

by:alskdj80
ID: 11737568
no, it still doesnt seem to find it...

basically what im doing to check is this:

location = System.getProperty("catalina.home") + "/webapps/ROOT/protected/files/" + filesToZip[i];
System.out.println("here");  //CHECK1
FileInputStream in = new FileInputStream(location); //***************
System.out.println("works");  //CHECK2

it always prints "here" but never prints "works", so i assume that location is never found...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11737599
Try:

System.out.println("here (" + location + ")" );  //CHECK1

does it print out a reasonable location?
0
 
LVL 3

Author Comment

by:alskdj80
ID: 11737622
yes, it does... it prints out:
/home/user/jakarta-tomcat/webapps/ROOT/protected/files/asdf.txt
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:alskdj80
ID: 11737634
(that is one of the filenames in the filesToZip array)
0
 
LVL 3

Author Comment

by:alskdj80
ID: 11737648
i have access to unix shell as well, so i went to the root directory and typed:
>> cd /home/user/jakarta-tomcat/webapps/ROOT/protected/files

and i got to the right directory...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11737709
What exception is it printing out?

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

Author Comment

by:alskdj80
ID: 11737793
im not sure... im not actually getting errors from the catches... but i set the error prints so i know when it gets the exception...

i just tried putting one file through the zip... a file that i placed into /user/jakarta-tomcat/asdf.txt

i put this in the code:
filesToZip = new String[1];
filesToZip[0] = "asdf.txt";

and it worked!


but when i try doing this (and also putting asdf.txt into the appropriate folder), it produces the same error again:
filesToZip[0] = "/webapps/ROOT/asdf.txt";
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11737822
what user is tomcat running as?  Does it have read access for that folder?  Does it have write access where it's trying to write the file?
0
 
LVL 3

Author Comment

by:alskdj80
ID: 11737905
hmm, i think i understand why... i had been taking the filenames from database, but apparently there was some corruption in some of the filenames...

thanks alot for helping me!  i think i can take it from here
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11738016
No worries ;-)  Glad I could give you things to do whilst you worked it out ;-)

Good luck with it :-)

Tim
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
html form inside div 3 91
Can't get intermediate Certificate to work in Tomcat 9 113
maven webservice example issue 2 122
Books that can get me started on JAVA 2 80
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now