aspose file tools*
The moose likes Java in General and the fly likes JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JAR file" Watch "JAR file" New topic
Author

JAR file

Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
How can I programatically extract a file from a JAR?
My need is to parse a file that is part of a JAR...so I need to somehow obtain that file from the JAR, parse it, perhaps modify it, and then put it back into the jar.

Thank you.
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi,

I don't know much or I've not done that kinda programming but you can probably go through java.util.jar package. It may have everything you need.


Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
As far as I think, java.util.Zip allows you to treat a .jar file just like a .zip file. Thereof, you can use the following code snippet to parse through all the entries in that file :
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
Jar files are really just zip files, with the addition of the manifest. You can use either the java.util.zip or the java.util.jar package to handle them (you need to use the jar package only if you need manifest-specific functionality).


Ping & DNS - my free Android networking tools app
Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
Thanks. Once I obtain the entries of the zip/jar file, how can i open a specific file? Do I need to store that file somewhere on disk and then use java's i/o library to read it...or os it saved in memory?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
No, you can read it directly w/o storing it someplace in between. Use the java.util.zip.ZipFile.getInputStream(ZipEntry entry) method, which returns a stream from which to read the file contents.
Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
I actually already have a method that takes in the name of a file to parse. Parsing in that method is done using BufferedReader's readLine() method.

So, I'm not sure how the InputStream can be used here...once I get the input stream, is it possible to read the file one line at a time just how it can be done using BufferedReader?

Thanks.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Hello Shitij,
JAR files are not merely a zip file with a manifest file. A JAR file is always a valid zip stream, but not necessarily the other way around.

You want to use a JarInputStream:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/jar/JarInputStream.html


Tony Morris
Java Q&A (FAQ, Trivia)
Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
Nevermind my previous post.

Need to use new BufferedReader(new InputeStreamReader(inputStream))
Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
Is it possible to delete a file from a JAR programatically?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
Is it possible to delete a file from a JAR programatically?

Have a look at the javadocs of the java.util.zip and java.util.jar packages. If you have a JarFile/ZipFile, what can you do with it?
[ January 23, 2006: Message edited by: Ulf Dittmer ]
Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
Well, from the API it doesn't seem like I can delete a file from a jar. The only way it seems I could do this is to create a new jar and copy all the files from the original jar except the file I wish to delete.

Is that true?

Thanks.
Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
Do any of you know of an API that allows you to make a change to a file within a JAR in-place?

The java.util.zip and java.util.jar packages don't allow me to do this. The approach I need to use is to create a new jar file and copy the contents of the original jar and the edited file. For some reason, this approach is not convinient in my application...and I'd rather like to just edit the file in the existing jar itself. Any ideas?

Thanks.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I guess this attempt will fail at the filesystem level.
Consider three files, a.txt, b.txt, and c.jar in the filesystem:

aaaccccccbb

Now you change c.jar and it is getting longer in place - it would overwrite b.txt.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAR file