This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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
Bartender
Posts: 4096
21
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!