• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursively browsing archive files  RSS feed

 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to browse through all .class files in my classpath, including the ones inside archives (jar files). Since archives can contain other archives I have to go into archives recursively to go through all entries.
I am using the ZipFile class to read the archive entries, but when an entry itself is a nested archive, it seems I first have to unpack this nested archive to the file system, before I can treat it in its turn using ZipFile.

Is there a way to avoid creating temporary archive files and use ZipFile (or any other solution) to read these nested archive files without having to unpack them to the file system first?
[ March 22, 2008: Message edited by: Kjeld Sigtermans ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't have to write to disk, if you properly structure the code in a recursive fashion. The InputStream that you get for each ZipEntry can be passed to a "new JarInputStream(...)" constructor, which allows you to get inside of that jar file recursively.
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that is exactly what I was looking for, thank you (I chose ZipInputStream over JarInputStream).
But there's still one problem: for each nested step I need a new instance of ZipFile for that nested archive, because the ZipInputStream (and JarInputStream) constructors both need a zipFile and entry instance:



Creating one from entry.getName() does not seem the way to go:



Help?
Kjeld
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both the ZipInputStream and JarInputStream constructors take an InputStream as parameter, not a Zip(Jar)File. Where does the need for a Zip(Jar)File arise?

 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have packed a class file (including its package) in a file called 1.zip.
I have placed 1.zip in 2.zip.
I have placed 2.zip in 3.zip.
3.zip is in the classpath.

My code finds 3.zip in the classpath and creates a ZipFile object (in this example called zipFileFor3Zip). Its entries are evaluated; there is only one entry which is 2.zip.
At this point I create a ZipInputStream to be able to evaluate the entries of 2.zip, like so:



At this point I can iterate over the entries of 2.zip.
When doing so, I find one entry, for 1.zip.

Now I want to recurse to the point where I created a ZipInputStream, this time because I want to examine 1.zip. To recurse into the code hereabove, I pass it the entry for 1.zip ('entryFor1Zip'), but obviously I also need to prepare a ZipFile object for 2.zip this time: zipFileFor2Zip.




I noticed this is not as simple as saying:



Because when executing this last line, a ZipException is thrown: Bad file descriptor 1.zip.

OR, should I perhaps have placed a manifest file somewhere?

Best Regards,
Kjeld

[ March 22, 2008: Message edited by: Kjeld Sigtermans ]
[ March 22, 2008: Message edited by: Kjeld Sigtermans ]
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kjeld Sigtermans:
Hi,

I am trying to browse through all .class files in my classpath, including the ones inside archives (jar files). Since archives can contain other archives I have to go into archives recursively to go through all entries.
It's true that archives can contain other archives, but those "second-level" archives are not in your classpath, that's not how classpath works. Therefore you don't have to read those second-level archives recursively.
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy Easter.

Noooo...is that true? So only top level jar contents are evaluated by a JVM?
I put a lot of time in the recursive jar reading funtionality LOL. It seemed so natural to assume that jars are recursively evaluated.
I did not know this. Anyway it was good to get it working though.

Thank you both.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!