Finding file inside jar or zip file  
Author Message
Raja





PostPosted: 2005-10-26 2:01:00 Top

java-programmer, Finding file inside jar or zip file 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 work. 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.

 
tedb





PostPosted: 2005-10-26 3:03:00 Top

java-programmer >> Finding file inside jar or zip file This should do it, for zip files or jar files, plus or minus typos:

import java.util.zip.*;
.
.
.
ZipFile zipfile=new ZipFile(new File("c:\\data.zip"));
for (Enumeration e=zipfile.entries(); e.hasMoreElements();){
ZipEntry ze=(ZipEntry)e.nextElement();
if (e.getName().equals("test.java")){
// found
break;
}
}

 
HalcyonWild





PostPosted: 2005-10-26 5:50:00 Top

java-programmer >> Finding file inside jar or zip file
Raja wrote:
> 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 work. It always return false.
>


Raja, what is that exclamation mark
File f = new File("jar:c:/data.zip!/test.java");

Also, does your jar file contain .java files.!??. or .class files.

 
 
Roedy Green





PostPosted: 2005-10-26 6:47:00 Top

java-programmer >> Finding file inside jar or zip file On 25 Oct 2005 11:01:10 -0700, "Raja" <email***@***.com> wrote,
quoted or indirectly quoted someone who said :

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

The File class has no knowledge of how Zip files work.

See http://mindprod.com/zip.html

Another approach is to create at URL for it with getResource and see
if it can.

See http://mindprod.com/jgloss/image.html

You also might like to read up on URL/URIs.

See http://mindprod.com/jgloss/uri.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Roedy Green





PostPosted: 2005-10-26 6:50:00 Top

java-programmer >> Finding file inside jar or zip file On 25 Oct 2005 14:50:28 -0700, "HalcyonWild" <email***@***.com>
wrote, quoted or indirectly quoted someone who said :

>Raja, what is that exclamation mark
> File f = new File("jar:c:/data.zip!/test.java");

it is the jar delimiter in an URL. See
http://mindprod.com/jgloss/image.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Roedy Green





PostPosted: 2005-10-26 6:50:00 Top

java-programmer >> Finding file inside jar or zip file On 25 Oct 2005 12:02:48 -0700, email***@***.com wrote, quoted or
indirectly quoted someone who said :

>ZipFile zipfile=new ZipFile(new File("c:\\data.zip"));
>for (Enumeration e=zipfile.entries(); e.hasMoreElements();){
> ZipEntry ze=(ZipEntry)e.nextElement();
> if (e.getName().equals("test.java")){
> // found
> break;
> }
Even easier is ZipEntry ZipFile.getZipEntry( String )

ZipFile has a HashMap to all the entries built so you go direct.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Andrew Thompson





PostPosted: 2005-10-26 7:31:00 Top

java-programmer >> Finding file inside jar or zip file HalcyonWild wrote:

> Raja wrote:
..
>>But if I have that test.java inside a zip file or jar file, how do I
>>find it's existance?
..
>>It didn't work. It always return false.
>
> Raja, what is that exclamation mark
> File f = new File("jar:c:/data.zip!/test.java");

The exlamation mark is what is used in URL's to denote a
Jar file (as opposed to a directory named 'data.zip'.
(try printing the entire URL obtained from a getResource
on a jar'd class file)

To the OP. the methods Roedy and tedb described should work
for archives all over the file-system, but sometimes there
is an easier method.

Is 'data.zip' on the classpath? If so, you can simply

URL resourceURL = this.getClass().getResource("test.java");

if the URL is not 'null', it was not found.

HTH
 
 
Rhino





PostPosted: 2005-10-26 22:20:00 Top

java-programmer >> Finding file inside jar or zip file
"Andrew Thompson" <email***@***.com> wrote in message
news:hfz7f.793$email***@***.com...
> HalcyonWild wrote:
>
> > Raja wrote:
> ..
> >>But if I have that test.java inside a zip file or jar file, how do I
> >>find it's existance?
> ..
> >>It didn't work. It always return false.
> >
> > Raja, what is that exclamation mark
> > File f = new File("jar:c:/data.zip!/test.java");
>
> The exlamation mark is what is used in URL's to denote a
> Jar file (as opposed to a directory named 'data.zip'.
> (try printing the entire URL obtained from a getResource
> on a jar'd class file)
>
> To the OP. the methods Roedy and tedb described should work
> for archives all over the file-system, but sometimes there
> is an easier method.
>
> Is 'data.zip' on the classpath? If so, you can simply
>
> URL resourceURL = this.getClass().getResource("test.java");
>
> if the URL is not 'null', it was not found.
>
Andrew, I think you meant to say if the URL -IS- null, the file was not
found.

Rhino


 
 
Andrew Thompson





PostPosted: 2005-10-26 22:33:00 Top

java-programmer >> Finding file inside jar or zip file Rhino wrote:
...
>> URL resourceURL = this.getClass().getResource("test.java");
>>
>>if the URL is not 'null', it was not found.

What Mr. Thompson MEANT to say was..

> Andrew, I think you meant to say if the URL -IS- null, the file was not
> found.

Uhh.. yeah. Oops!

It's lucky you read it (and paid attention) all the way to the end. :-)

( ..and there might have been the small matter of a missing
'/' at the front of that path as well, but ..shhHHHHhh, maybe
nobody'll notice... ;)
 
 
Raja





PostPosted: 2005-10-27 1:33:00 Top

java-programmer >> Finding file inside jar or zip file Thanks everyone for the reply.

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 finding a file inside a tar file.

 
 
Roedy Green





PostPosted: 2005-10-27 5:56:00 Top

java-programmer >> Finding file inside jar or zip file On 26 Oct 2005 10:33:04 -0700, "Raja" <email***@***.com> wrote,
quoted or indirectly quoted someone who said :

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

there is always exec.
see http://mindprod.com/jgloss/exec.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.