• 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

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

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic