• Post Reply Bookmark Topic Watch Topic
  • New Topic

listFiles(new FilenameFilter()) and recursion  RSS feed

 
Amy Hafer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that takes HttpServlet requests (shapeName), finds those files in a directory on the server, and zips them up in a file for HttpServlet response. Problem is now I have files in subdirectories and I don't know how to recursively search through the subdirectories. The code is one class with an annonymous inner class, FilenameFilter. I could use suggestions on how best to do the recursion within the listFiles method as coded and add the files directly to the File[] theShapes. Hoping that creation of a new class to recurse through the directory is not necessary. Thanks!


[This message has been edited by Amy Hafer (edited August 09, 2001).]
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some code that will recurse down through directories.
Ok it has a hand coded "filter", but it still does the job.

James.
 
Amy Hafer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James. I am trying it this way now. Do you have to do anything special with the file besides using the addElement(files(i)) method when you are putting the files in a Vector and sending it to a zip stream? For some reason, the file is not being found in the subdirectory (or maybe it is, it is just not being sent to the zip file).
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amy,
I am no expert on using the java.util.zip package but I would make these assumptions from reading the API:
(Also assumming you have contructed the appropriate ZipOutputStream)
1) you would iterate through your Vector retrieving each File object
2) for each file created construct a ZipEntry using the retrieved File object's getPath() method or (perhaps getAbsolutePath()) to make sure that the ZipEntry can find this file in the file system
3) add the ZipEntry to the ZipOutputStream via putNextEntry(ZipEntry e) method
Perhaps if you post your code for creating the zip file, we might be able to further determine what is going on.
James.
 
Amy Hafer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James, you are probably right that it is the way I am constructing the zip file but ZipEntry is a string so I can't use getPath there. I am guessing the problem might be in this part of the code so for the sake of brevity (and clarity) here is the try statement where I am getting the HttpServletRequest called request and passing it to the ListFilez class and getting back the Vector (the list of files). Since it does work for the directory I originally start the search in but not for the subdirectory is definitely does sound like I need to use the getPath or getAbsolutePath method. Thank you for all your help so far!

**Actually I just realized that the problem is with the ListFiles class. The way it is written, the Vector is returned empty if the only file it is looking for is in the subdirectory. If it is looking for 2 files, one in the parent directory and one in the subdirectory, it will only return the file in the parent directory. So I am doing something wrong with how or where I created the Vector.
[This message has been edited by Amy Hafer (edited September 10, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!