• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAR file

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Shitij Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shitij Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind my previous post.

Need to use new BufferedReader(new InputeStreamReader(inputStream))
 
Shitij Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to delete a file from a JAR programatically?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Evil is afoot. But this tiny ad is just an ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic