This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Finding files inside a jar or zip file  RSS feed

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to find whether a file test.java exists in my c:\ directory, I do
File f = new File("c:\test.java");
f.exists();

But If I have that test.java inside a zip file or jar file, how do I find it's existance? I tried doing
File f = new File("jar:c:/data.zip!/test.java");
f.exists();

It didn't works. It always return false.

Does anyone have any idea on how to find the existance of a file inside a zip or jar file?

Thanks,

- Raja.
 
author
Posts: 4103
23
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassLoader.getResourceAsStream()

If the jar is part of the class path, you can read elements inside of it using the current class loader and getResourceAsStream().

As I'm sure someone will menction you can also use getResourceBundle() as a variation on this, but I prefer this one for simplicity.

As a side note there are more complicated issues if this is J2EE and/or the method reading the file is static (and therefore may be accessing the correct class loader).
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The zip file was not in the classpath. So, I tried doing
zipfile.getZipEntry(fileName) and it worked fine.

I guess this will work for jar file too. Does anyone have any idea on
tar file? i.e how to find a file inside a tar file.

Thanks.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!