aspose file tools*
The moose likes I/O and Streams and the fly likes How to tell if a file is already in a jar? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to tell if a file is already in a jar?" Watch "How to tell if a file is already in a jar?" New topic
Author

How to tell if a file is already in a jar?

Kelly Wood
Ranch Hand

Joined: Nov 07, 2004
Posts: 54

I have a program that adds files to a jar file using JarOutputStream.putNextEntry(myJarEntry).

I keep getting the following error.

java.util.zip.ZipException: duplicate entry

Besides keeping a list of files added to the jar, is there a better way to tell if a file with the same name already exist in the jar file? I tried File.list() but it returns null. Thanks!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You can use ZipInputStream (or JarInputStream) to read the entire contents of jar file. If you put it in some kind of collection, you can always have information about which files are already in there.


The quieter you are, the more you are able to hear.
Kelly Wood
Ranch Hand

Joined: Nov 07, 2004
Posts: 54
Can I have both input and output stream active on the same file? Basically my program adds files to a jar, but if the file already exist inside then I will skip adding.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

No, that's not the right solution. Just keep track of the zip entries as you add them. In some kind of collection, as Kemal said. (A Set would probably be best.) When you go to add another entry, check the collection first to see if the zip entry is already in the archive. If it is, then don't add it again.

(You weren't planning to add new entries to an existing archive, were you? It didn't sound like it from your original post, but I just thought I would ask. Because you can't do that.)

 
 
subject: How to tell if a file is already in a jar?