Solved

Adding file to jar.zip

Posted on 2004-08-25
19
269 Views
Last Modified: 2012-08-13
What is the simplest way to add an image file to a jar.zip archive?

I don't have (and don't want) a Java SDK.  My XML file looks for an image within the jar file.  I just want to add a new image and point the XML to it.
0
Comment
Question by:mesmay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +2
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11890378
0
 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 11890384
use winzip
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11890391
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 11890392
or whatever your favourite zip utility is.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11890400
>What is the simplest way to add an image file to a jar.zip archive?
>I don't have (and don't want) a Java SDK.

You must have some application managing the archive. You must search over the documentation of this application.
(Winzip, Winrar,...Compress,...)

Bye, Giant.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11890401
Do you mean just a jar archive?

Or a zip file?

To add to a zip file, I think you need to expand the entire zip to a temp directory, add you file to that directory, then zip it all up again...  You cannot just add files to zip files :-(

http://www.developer.com/java/other/article.php/607931

From the "Updating" section

---------------

 Updating an existing zip file or jar file gets tricky, because the zip and jar classes do not provide truly random access to zip files and jar files. The ZipFile and JarFile classes have a method for getting a specific entry but not for adding an entry. The ZipInputStream and JarInputStream classes have a method for adding an entry but not for getting a specific entry. So, updating a zip file or a jar file will often require the use of a temporary file.

Here is an example program, JarUpdate (http://www.developer.com/repository/common/content/article/19990118/gm_trose_jarzip2/JarUpdate.java), that updates a specific entry in an existing jar file.

The algorithm for JarUpdate is:

   1. Get the jar name and the file name from the command-line arguments.
   2. Create file objects for the jar file and a temporary jar file.
   3. Open the jar file.
   4. Create the temporary jar file, with no manifest. (The manifest will be copied from the original jar file.)
   5. Allocate a buffer for reading file data.
   6. Open the specified file.
   7. Create an entry in the temporary jar for the specified file.
   8. Read the file data and write it to the temporary jar.
   9. Loop through the entries of the original jar file, skipping the entry that was added to the temporary jar. For each entry:
         1. Get an input stream for the entry.
         2. Read the entry data and write it to the temporary jar.
  10. Close the temporary jar file.
  11. Close the original jar file.
  12. If the temporary jar file was created without an exception, delete the original jar file and change the name the temporary file to the name of original jar file. Otherwise, delete the temporary jar file.

To update an existing zip file, you would use the algorithm above but would use the zip classes instead of the jar classes. This is left as an exercise for the reader.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11890412
If you mean non-programmatically, you can use any zip program - you don't need the SDK.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11890417
>  If you mean non-programmatically, you can use any zip program - you don't need the SDK.

Though you'd have to wonder what this question is doing in the java TA ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11890435
> you can use any zip program

already mentioned (twice)
0
 

Author Comment

by:mesmay
ID: 11890442
Okay,
I'ved done it the simple way suggested, using winzip.  But the file is not recognised.

In the XML file I have:
      <object name="distance_add" type="imagebutton" bounds="627,2,23,23"
                                      image="img/tool_getinfo.gif" group="1"/>
      <object name="distance_clear" type="imagebutton" bounds="652,2,23,23"
                                      image="img/cross_diag.gif"/>

This works fine.
BUT if I add another gif file to the img folder in the zip file using winzip and changing the XML to point to it, it does not see it.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 11890457
Is it in the correct sub-entry?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11890461
I'm confused....  what has this got to do with java in any way shape or form?

Maybe I haven't had enough coffee...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11890465
>>already mentioned (twice)

If you are referring to your comment 'use winzip' then that's not equivalent to mine imo
0
 
LVL 92

Expert Comment

by:objects
ID: 11890467
are u sure its in the right directory
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11890471
>>are u sure its in the right directory

(Already mentioned)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11890487
:-)

LOL
0
 

Author Comment

by:mesmay
ID: 11890493
TimYates,
I'm new to Java and when simply adding a file to the jar archive didn't work I assumed it may be more complicated, therefore I posted here.

Where would you suggest I have posted?

Anyway, problem solved - I just put the image file in the root instead of in the img folder.
Thanks for cofirming that it is indeed simple.
0
 
LVL 92

Expert Comment

by:objects
ID: 11890512
Glad we could help :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11890520
Didn't mean to be nasty ;-)

Good luck with it :-)  Glad you got it sorted :-)

Tim
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question