Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kelly Wood
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kelly Wood
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic