This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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!
 
Bartender
Posts: 825
5
Python Ruby Java
  • 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.
 
Marshal
Posts: 25818
69
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.)

    Bookmark Topic Watch Topic
  • New Topic